0

i have a datagrid that load the data from my database. in my datagrid, i also included the , so that i can checked or unchecked the row.

here is the picture. enter image description here

here is my xaml.

<DataGrid AutoGenerateColumns="False" Height="258" HorizontalAlignment="Left" Margin="12,0,0,12" 
              Name="dataGrid1" VerticalAlignment="Bottom" Width="479" ItemsSource="{Binding data}" 
              AlternatingRowBackground="#FFC4B0B0">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding nCateogryID}" Header="No." IsReadOnly="True" Width="30" />
            <DataGridTextColumn Binding="{Binding sCategoryDesc}" Header="Nama Kategori" IsReadOnly="True" Width="160" />
            <DataGridTextColumn Binding="{Binding sCategoryItems}" Header="Keterangan Kategori" IsReadOnly="True" Width="170" />
            <DataGridCheckBoxColumn Header="Checked?" />
        </DataGrid.Columns>
    </DataGrid>

May question is: How can i know which data already checked whenever i hit the 'Button'? I also do search on google, but found nothing.

i think like this. whenever i click 'Button', i will start to check for each row. how can i do it? any sample for that?

i also think to find another solution, how about using the event handler? thats mean whenever i click for the row and checked, i will trigger the event and check if is it checked or not. How can i do it?

For me, using the event handler is more achieveable because it's much faster rather i have to checked, and press button. lets say if i have only 10 rows, that's not a big problem. How about if i have 1000rows? if i check one by one, it will be troublesome. waste alot of time. any idea? thanks.

Or maybe use the datagrid_selectionchanged. How can i check the checkbox has been checked or not?

because if we wait until user press button, then we check is checked or not, i think its a slow process. you have to check for each row. if my row only 10rows, maybe its okay. how about if i have 1000 rows need to check? thanks. :)

0

2 Answers 2

1
for(int i=0;i<DataGrid.items.count-1;i++)
{
     CheckBox chkBx = (CheckBox)DataGrid.items[i].Cells[3].FindControl("EmpId") ;
     if( chkBx !=null && chkBx.Checked )
      {
        Response.Write(DataGrid.items[i].Cells[1].Text.ToString() );
      }
}

Here "EmpId" is checkbox name(control name) and i am using value of first cells for write. You can change the cell index as you want to use.

3
  • hi. thanks for you answer.. but i can't find the "FindControl" statements. and Empld is my header checkbox name? Oct 10, 2012 at 4:59
  • hi, i have change the code .Cells[3] was missing between items[i] and FindControl so you was unabel to find "FindControl" statement. "Empld" is your ItemTemplate control's(checkbox) Id. Oct 11, 2012 at 9:01
  • hi.. i already did it with my way. thanks for your answer. i will mark as the correct answer. thanks @Vikash Sinha Oct 16, 2012 at 9:08
0

In the database you have a field for Checked ? If not, you should extend the class object for your items and add a new property, named IsChecked for example, and then through binding you can check / uncheck the items. In your case "data" is a collection of some custom objects. Extend that custom object and add to it a new property. After this, you can enumerate through the source collection of your datagrid and you can find which ones are checked. Something like this:

var checkedItems = data.Where(x => x.IsChecked == true).ToList();
1
  • hi, thanks for your answer. don't have. but for the first time i load the data to my datagrid, all row is unchecked. then i can choose which data i want to check. after that i press button and i can get the detail data for each row. Oct 10, 2012 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.