none
Datagrid with check box

    Question

  • I have updated the Silverlight 2 Release Candidate in my machine and have used the datagrid. I have 100 rows in my datagrid with checkboxes..if I checked the first & second rows checkboxes, it is automatically checking someother checkboxes in the grid rows..How can i fix this issue?

    Thanks

    Sankar 

     

    Monday, October 13, 2008 2:56 AM

Answers

  • I had faced this same problem, So I've replaced the template checkbox column with DatagridCheckBoxColumn, Now things are working well.

     <data:DataGridCheckBoxColumn x:Name="AllowCheckbox" Binding="{Binding Allow, Mode=TwoWay}" Width="50"></data:DataGridCheckBoxColumn>

     here "allow" is the boolean value while binding..Please change the readonly property false for this checkbox column..

     

    Wednesday, October 15, 2008 10:17 AM

All replies

  •  Check what you're databinding (ItemsSource) to. It sounds to me like they are databound to the same objects, and you are seeing the results of that common databinding.

    Monday, October 13, 2008 3:52 PM
  • Thanks for your reply,

     I have used Itemsource to bind datagrid and did not bind any values for checkbox. I have just added checkbox column using "

    DataTemplate", When I am checking the checkbox, it is automatically checking the other rows checkboxes :-(. Now I have replaced with the Image for the checkboxes, Still I am getting the same issue. In this image co;lumn while I am clicking the image i'm changing the status of the Image like (Tick/untick) for checkboxes..

    Tuesday, October 14, 2008 12:57 AM
  • Hi Shankar,

    Please check this link http://silverlight.net/forums/t/29287.aspx

    Its not a solution but a reason for the problem 

     

    Tuesday, October 14, 2008 1:33 AM
  • Thanks, I got the reason for this issue, At this point, how can we fix this issue?. . Yi-Lun Luo - MSFT - he mentioned that "Instead of checking the index of the row, you can check the index of the data source." What does he mean here?

     

    Tuesday, October 14, 2008 1:41 AM
  • I think he is suggesting to use the datasource which is used to bind the control.I guess the datasource has to be an Observable collection.But still the end user will get confused if he scrolls the control..

    Tuesday, October 14, 2008 1:54 AM
  • Thanks, I got the reason for this issue, At this point, how can we fix this issue?. . Yi-Lun Luo - MSFT - he mentioned that "Instead of checking the index of the row, you can check the index of the data source." What does he mean here?


    That means you need to bind the checkbox to a data field in your DataSource.  Do not manipulate the UI control in the DataGrid, because the control will get reused, but manipulate the data bind to that control.

     

     

    Tuesday, October 14, 2008 12:44 PM
  •  Yes, you must bind the checkbox column to something or the checkboxes will be nothing more than brain-dead controls.  It is the data that they are bound to that you care about, not the controls themselves.  You need to do something like this:

    IN XAML:

    <data:DataGrid Name="myGrid" AutoGenerateColumns="False">

        <data:DataGrid.Columns>

            <data:DataGridCheckBoxColumn Binding="{Binding Chosen, Mode=TwoWay}" />

            <data:DataGridTextColumn Binding="{Binding Name}" />

        </data:DataGrid.Columns>

    </data:DataGrid>

     

    IN CODE:

    // Define a type for your backing store

    public class Option {

        public bool Chosen { get; set; }

        public string Name { get; set; }

    }

    // Define a List or Observable Collection to hold the data (make sure it has wide-enough scope, page scope is good)

    List<Option> optionsList = new List<Option>();

     

    // TODO: Fill in the optionsList with your options here (can be from a WebService, LINQ, etc.)

     

    // At the appropriate time (Page Loaded event or when you get the data from a WebService or whatever):

     

    myGrid.ItemsSource = optionsList;

     

    // Now when you want to find out what the user has selected simply iterate through optionsList and do what you want with the information, the DataGrid will have updated your List as the user clicked the checkboxes in the DataGrid

     

    foreach (Option o in optionsList) {

        bool sel = o.Chosen;

       // do something with sel

    }

     

    // You could also use LINQ to find a particular option:

     

    bool isBlue = (from o in optionsList where o.Name == "Blue" select o.Chosen).FirstOrDefault();

     

    If you want to know immediately when a checkbox is selected then you'll need to implement what I call a SuperObservableCollection rather than a List or ObservableCollection. I'll be doing a write-up on how to do that in another thread, and I'll post a link to it here when it's up.

    Tuesday, October 14, 2008 12:45 PM
  • Hi,

        I am using a Template column for Checkbox and i am binding to the datasource with the code given below..

    <CheckBox x:Name="chkName" IsChecked="{Binding NameReference}"></CheckBox>

    Checked status of the checkbox will be determined by NameReference which has either true or false..When the page is loaded it doesnt have any problem..

    But i am using that checkbox for multiple delete option .So if the user checks something  he wont be able to find which one he checked after scrolling .how to overcome this issue..

    Wednesday, October 15, 2008 2:47 AM
  • I had faced this same problem, So I've replaced the template checkbox column with DatagridCheckBoxColumn, Now things are working well.

     <data:DataGridCheckBoxColumn x:Name="AllowCheckbox" Binding="{Binding Allow, Mode=TwoWay}" Width="50"></data:DataGridCheckBoxColumn>

     here "allow" is the boolean value while binding..Please change the readonly property false for this checkbox column..

     

    Wednesday, October 15, 2008 10:17 AM
  • Hi

           I am facing one more similar issue... I am coloring the row based on certain condition.If the user scrolls he will endup in confusion as the row colors are altered..

         Thx Sankar for ur information..

     

    Thursday, October 16, 2008 12:29 AM
  • I have a checkbox in each row of datagrid  and i user selects 2 check boxs I need to get the id of the each selected row. How to achive it?

    Monday, July 13, 2009 9:08 AM
  • As mentioned above, you should have a boolean property in your datasource to which you can bind the checkbox to.

    When the event for deletion is fired (say "Delete" button click), you can loop through the collection and get the items marked for deletion.

    Thanks

    Jac

    Monday, July 13, 2009 9:28 AM
  • Hi

    Thanks for Reply. I solved it for check box. Now i have a radio button in each field and when i select a radio button I need to get the row index. I tried selecteditem of the datagrid, but it  did was not working for me?

    Any Help?

    Monday, July 13, 2009 10:26 AM
  •  friends this links has what what you exactly need

    Monday, August 03, 2009 4:37 AM
  • Hi Rajkumar,

                      Same problem i have faced. in my case i am binding the Tag value for that checkbox. If i select more than one CheckBox then how can i get the Tag values.Here is my Source code for that.

    <data:DataGrid x:Name="dgActivityType"  AutoGenerateColumns="False" CanUserResizeColumns="False" IsReadOnly="True"  HorizontalAlignment="Center">

    <data:DataGrid.Columns>

    <data:DataGridTemplateColumn Header="Pick">

    <data:DataGridTemplateColumn.CellTemplate>

    <DataTemplate>

    <CheckBox x:Name="RowFilterButton" Tag="{Binding ProjectResourceID}" VerticalAlignment="Center" HorizontalAlignment="Center"  />

    </DataTemplate>

    </data:DataGridTemplateColumn.CellTemplate>

    <data:DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="160"/>

    </data:DataGrid.Columns>

    </data:DataGrid>

    And i am using the WCF service for binding the DataGrid.How can i get that Tag values?.

    thanks,

    kargo. 

     

    Tuesday, October 06, 2009 6:22 AM
  • Friend  Rajkumar,

    i hope this must work. It works for me

     

    /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

    <MyCtrls:DataGrid x:Name="MydataGrid" AutoGenerateColumns="True"   Height="377"   > <!--  SelectionChanged="MydataGrid_SelectionChanged"   ItemsSource="{Binding ElementName=RiaProduct, Path=Data}"-->

                   <MyCtrls:DataGrid.Columns>

                        <MyCtrls:DataGridTemplateColumn >

                            <MyCtrls:DataGridTemplateColumn.HeaderStyle >

                                <Style TargetType="dataprimitives:DataGridColumnHeader">

                                    <Setter Property="Template">

                                        <Setter.Value>

                                            <ControlTemplate TargetType="dataprimitives:DataGridColumnHeader">

                                               

                                                <CheckBox Click="CheckBX_Click" x:Name="CheckBX" Content="Check" HorizontalAlignment="Center" VerticalAlignment="Center" >

                                                </CheckBox>

                                            </ControlTemplate>

                                        </Setter.Value>

                                    </Setter>

                                </Style>

                            </MyCtrls:DataGridTemplateColumn.HeaderStyle>

                            <MyCtrls:DataGridTemplateColumn.CellTemplate>

                                <DataTemplate>

                                    <CheckBox x:Name="chk"  HorizontalAlignment="Center" VerticalAlignment="Center" Click="chk_Click">

                                        <CheckBox.IsChecked>

                                            <Binding Path="Check" Mode="TwoWay" />

                                        </CheckBox.IsChecked>

                                    </CheckBox>

                                </DataTemplate>

                            </MyCtrls:DataGridTemplateColumn.CellTemplate>

                        </MyCtrls:DataGridTemplateColumn>

                        <MyCtrls:DataGridTextColumn Header="ModifiedDate" Binding="{Binding Mode=TwoWay, Path=ModifiedDate}"/>

                        <MyCtrls:DataGridTextColumn Header="Name" Binding="{Binding Mode=TwoWay, Path=Name}"/>

                        <MyCtrls:DataGridTextColumn Header="ProductID" Binding="{Binding Mode=TwoWay, Path=ProductID}"/>

                        <MyCtrls:DataGridTextColumn Header="ProductNumber" Binding="{Binding Mode=TwoWay, Path=ProductNumber}"/>

                        <MyCtrls:DataGridTextColumn Header="ReorderPoint" Binding="{Binding Mode=TwoWay, Path=ReorderPoint}"/>

                        <MyCtrls:DataGridTextColumn Header="rowguid" Binding="{Binding Mode=TwoWay, Path=rowguid}"/>

                        <MyCtrls:DataGridTextColumn Header="SellStartDate" Binding="{Binding Mode=TwoWay, Path=SellStartDate}"/>

                        <MyCtrls:DataGridTextColumn Header="StandardCost" Binding="{Binding Mode=TwoWay, Path=StandardCost}"/>

                    </MyCtrls:DataGrid.Columns>

                </MyCtrls:DataGrid>

    Tuesday, October 06, 2009 6:33 AM
  • Hi Raj,

             Thanks for your reply.But how can i get a Checked row Tag values in CodeBehind?.

    Thanks,

    kargo.

    Monday, October 12, 2009 12:40 AM
  • Try this:

    private void chk_Checked(object sender, RoutedEventArgs e)

    {

    CheckBox cb = (CheckBox)sender;

    string tag = cb.Tag.ToString();

    }

    Hope that helps.

    Monday, October 12, 2009 4:09 AM
  • Try this:

    private void chk_Checked(object sender, RoutedEventArgs e)

    {

    CheckBox cb = (CheckBox)sender;

    string tag = cb.Tag.ToString();

    }

    Hope that helps.

     

     

    friend your's code send to me

    private void CheckBX_Click(object sender, RoutedEventArgs e)
            //{
            //    for (int i = 0; i < 5; i++)
            //    {
            //        MydataGrid.ScrollIntoView(this._Product, MydataGrid.Columns[0]);
            //        MydataGrid.SelectedIndex = i;
            //        CheckBox Check = this.MydataGrid.Columns[0].GetCellContent(this.MydataGrid.SelectedItem) as CheckBox;
            //        Check.IsChecked = true;
            //    }
            //}

    Monday, October 12, 2009 4:23 AM
  • ??

    I didnt get you

    Monday, October 12, 2009 4:45 AM
  • Hi,

         as your code i can get only one tag value and i did this already.But when i am click the button i want to get the Tag ids if i select more than one CheckBox.How can i get this?

    thanks,

    kargo.

    Tuesday, October 20, 2009 12:41 AM
  • Use ObservableCollection and Linq query to get the tag id's from that collection.

    Let me know if you have any problem.

    Tuesday, October 20, 2009 12:45 AM
  • Hi varshav,

    I tried like the following on Button click.

    int res = 0;

    var chkBox = sender as CheckBox;

    var dataGridrow = DataGridRow.GetRowContainingElement(chkBox);

    for (int r = 0; r < ((ObservableCollection<ResourceActivities>)dgActivityType.ItemsSource).Count; r++)

    {

    var my = dgActivityType.Columns[0].GetCellContent(dataGridrow) as CheckBox;if (my.IsChecked == true)

    {

    res +=
    int.Parse(my.Tag.ToString());

    }

    }

    but the checkbox has return the null value.Is there any other way to get a tag values for selected checkbox on button click?.

    thanks,

    kargo.

    Thursday, October 22, 2009 2:35 AM
  • Hi Kargo,

    You can use ObservableCollection and get all the list of checked rows from the datagrid itemsSource which is nothing but ObservableCollection.

    Let me know if you have any problem.

    Thursday, October 22, 2009 2:39 AM
  • Hi Varshav,

                    Then how can we get?. I dont know how to do.can you have any sample for that?.

    thanks,

    kargo.

    Thursday, October 22, 2009 3:45 AM
  • Ok, Try this:

    public class myData : INotifyPropertyChanged

    {

    public event PropertyChangedEventHandler PropertyChanged;

    public long ID { get; set; }

    public string Name { get; set; }

    private bool _ischecked = false;

    public bool IsRegionChecked

    {

    get { return _ischecked; }

    set

    {

    _ischecked =
    value;

    if (PropertyChanged != null)

    PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));

    }

    }

    }

    ObservableCollection<myData> obsData = new ObservableCollection<myData>();

    Datagrid1.itemsSource = obsData;

    //this will give you the list of all checked items

    obsData.Where(a => a.IsChecked == true).ToList<myData>();

    HTH Smile

    Thursday, October 22, 2009 4:13 AM
  • Hi Varshav,

                Thanks for ur code.I did like following to get a more than one tag.

    private void RowFilterButton_Checked(object sender, RoutedEventArgs e)

    {

    CheckBox id = sender as CheckBox;

    string test = id.Tag.ToString();

    if (proIdChk.Contains(test))

    {

    }

    else if (proIdChk != "")

    {

    proIdChk = proIdChk +
    "," + id.Tag.ToString();

    }

    else

    {

    proIdChk = id.Tag.ToString();

    }

    }

     

    thanks,

    kargo.

    Friday, October 23, 2009 3:48 AM
  • Is it working fine?

    Friday, October 23, 2009 4:31 AM
  • Yes its working fine.

    Friday, October 23, 2009 5:27 AM
  • If any of the post helped you then mark it as answer, it helps other users.

    Thanks Smile

    Friday, October 23, 2009 5:51 AM
  • Use the property Ischecked and binding as twoway will solve the problem.

    <DataTemplate>
                                    <CheckBox IsChecked='{Binding IsChecked, Mode=TwoWay}' Click="chbSelect_Click" Name="chbSelect" Width="Auto"/>
                                </DataTemplate>

    Monday, May 30, 2011 10:49 AM