Silverlight / WPF - checkbox is in checked mode in next page in datagrid

Asked By gopal krish on 06-Jun-12 02:52 AM
hi..
i have placed a checkbox inside datagrid as a datatemplate.

i checked 2 rows (1,2nd rows)in my first page and clicked second page it shows the rows are checked (1,2nd rows) but i didnt checked the second row.

it appears 3rd as well.
how to handle this.. 

also i want to know if i checked 2 rows in 1st page then went to 2 nd and checked 3 rows then i clicked  a button outside the grid , here i want to get checked row details.

how to get the checked row details



need ur suggestions..

regards
gopal.s
Chintan Vaghela replied to gopal krish on 06-Jun-12 03:24 AM

Hi Frndz,

 

Functionality ::  store previous page checkbox checked value in gridview

 

Make two function

 

First for Remember Old Checkbox Value

 

Second for Repopulate Checkbox Old Checked value

 

 

 

  private void RememberOldValues()

    {

      ArrayList categoryIDList = new ArrayList();

      int index = -1;

      foreach (GridViewRow row in grdAccDetails.Rows)

      {

        index = Convert.ToInt32(grdAccDetails.DataKeys[row.RowIndex].Value);

        bool result = ((CheckBox)row.FindControl("chkList")).Checked;

 

        // Check in the Session

        if (Session["CHECKED_ITEMS"] != null)

          categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];

        categoryIDList.Remove(index);

        if (result)

        {

          categoryIDList.Add(index);

        }

 

      }

      if (categoryIDList != null && categoryIDList.Count > 0)

        Session["CHECKED_ITEMS"] = categoryIDList;

    }

 

 

    private void RePopulateValues()

    {

      ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];

      if (categoryIDList != null && categoryIDList.Count > 0)

      {

        foreach (GridViewRow row in grdAccDetails.Rows)

        {

          int index = Convert.ToInt32(grdAccDetails.DataKeys[row.RowIndex].Value);

          if (categoryIDList.Contains(index))

          {

            CheckBox myCheckBox = (CheckBox)row.FindControl("chkList");

            myCheckBox.Checked = true;

          }

        }

      }

    }

 

 

Now call this function on Page Index Change Event

 

First Call RememberSaveValue After then bind grid, after then call Repopulate checked value on PageIndex Change.

 

protected void grdAccDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

      RememberOldValues();

      grdAccDetails.PageIndex = e.NewPageIndex;

 

      DataTable dtMain = BINDDataTable();

      grdAccDetails.DataSource = dtMain;

      grdAccDetails.DataBind();

 

      RePopulateValues();

    }

 

 

 

 

Hope this helpful!

Thanks

 

 

 

 

Neha Garg replied to gopal krish on 06-Jun-12 03:39 AM

Hi..

See the below code:

Xaml Code:

<data:DataGrid x:Name="dgPerson" Margin="8" Grid.Column="1" IsReadOnly="True" 

           LoadingRow="dgPerson_LoadingRow" Grid.Row="1" AutoGenerateColumns="False">

        <data:DataGrid.Columns>

          <data:DataGridTemplateColumn Width="80" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}">

            <data:DataGridTemplateColumn.CellTemplate>

              <DataTemplate>

                <CheckBox x:Name="chkPerson" Click="chkPerson_Click"

                    VerticalAlignment="Center" IsChecked="false" HorizontalAlignment="Center" HorizontalContentAlignment="Center"/>

              </DataTemplate>

            </data:DataGridTemplateColumn.CellTemplate>

          </data:DataGridTemplateColumn>

          <data:DataGridTextColumn Header="Name" Binding="{Binding Name}" MinWidth="120" Width="SizeToCells" CanUserReorder="True" CanUserSort="True"/>

          <data:DataGridTextColumn Header="Age" Binding="{Binding Age}" MinWidth="75" Width="SizeToCells" CanUserReorder="True" CanUserSort="True"/>

          <data:DataGridTextColumn Header="Country" Binding="{Binding Country}" MinWidth="120" Width="SizeToCells" CanUserReorder="True" CanUserSort="True"/>

          <data:DataGridTextColumn Header="Gender" Binding="{Binding Gender}" MinWidth="120" Width="SizeToCells" CanUserReorder="True" CanUserSort="True"/>

          <data:DataGridTextColumn Header="Email Id" Binding="{Binding EmailId}" MinWidth="120" Width="SizeToCells" CanUserReorder="True" CanUserSort="True"/>

          <data:DataGridTextColumn Header="Joined On" Binding="{Binding JoinedOn}" MinWidth="120" Width="SizeToCells" CanUserReorder="True" CanUserSort="True"/>

        </data:DataGrid.Columns>

      </data:DataGrid>

 

Xaml.cs


private void dgPerson_LoadingRow(object sender, DataGridRowEventArgs e)

      {

        CheckBox chk = (CheckBox)this.dgPerson.Columns[0].GetCellContent(e.Row);

        chk.Click += new RoutedEventHandler(chkPerson_Click);

        Person p = chk.DataContext as Person;

        chk.IsChecked = personList.Contains(p);

      }

 

      private void chkPerson_Click(object sender, RoutedEventArgs e)

      {

        CheckBox chk = sender as CheckBox;

        bool check = chk.IsChecked.Value;

        Person p = chk.DataContext as Person;

        if (check)

        {

          if (!personList.Contains(p))

            personList.Add(p);

        }

        else

        {

          personList.Remove(p);

        }

      }