Loop through SfDataGrid Rows
Hello,
I have a SfDataGrid with few Columns.
This Grid is filled through a DataTable.
I have added a unbounded column (CheckBox Column with name Sel) to allow User to check that Row.
One of the Column is allowed to edit and add value to each Row.
So far, so good.
Now, I need to loop through the SfDataGrid to check which Rows are Checked in Sel and which Rows has Values in editable column.
Here's how I managed to do it in Winforms:
foreach (DataGridViewRow row in data.Rows)
{
int ID = Convert.ToInt32(row.Cells[1].Value); // Getting my ID from DataGrid
string myValue = row.Cells[6].Value; // Getting the Values from my editable cell in datagrid
var ctx = new DbContext();
DataGridViewCheckBoxCell Sel = row.Cells[0] as DataGridViewCheckBoxCell; //Getting my CheckBox
if ((bool)row.Cells["Sel"].EditedFormattedValue) // Getting if my CheckBox is Checked
{
try
{
var query = ctx.tblStuff.FirstOrDefault(c => c.SID == ID); // Querying my Database to prepare to Update something
But, I can transform this to work with SfDataGrid.
Any help?
Thanks.
EDIT: I tested my DataGrid and cannot Select my Added CheckBox... "Cannot set Sel". What's wrong? What I'm missing here? The CheckBox has beed added through designer, not by code.
EDIT2: Well, even adding GridCheckBoxColumn by code, I get the same problem. CheckBox canot be selected and get this error on Program.cs.
SIGN IN To post a reply.
3 Replies
AK
Adhikesevan Kothandaraman
Syncfusion Team
August 9, 2018 12:56 PM UTC
Hi Carlos,
Thanks for using Syncfusion products.
|
I need to loop through the SfDataGrid to check which Rows are Checked in Sel and which Rows has Values in editable column. |
You can use the Records collection to loop through the records in the SfDataGrid. It will loop through all the data rows of the SfDataGrid. Here we provided the sample with the DataRowView. You can cast Record.Data with the underlying data object.
Code Sample:
| |
|
Adding the CheckBox column to get the checked values. |
You can add the checkbox column to the SfDataGrid by adding the Boolean column to the DataSource. It will automatically create and adds the CheckBox column to the SfDataGrid.
Code Sample:
Screenshot:
Note: SfDataGrid doesn’t have the default support to load the checkbox column as the unbound column. If you want to work with the checkbox column to the grid, It needs to be bounded to the DataSource.
|
Regards,
Adhi
CF
Carlos Ferreira
August 10, 2018 03:55 PM UTC
Thanks for your help.
I could manage adding the CheckBox and convert my code to work with SfDataGrid and loop through the grid.
But, I cannot select each CheckBox row by row. The only way I can select my CheckBox is select the Header Checkbox. So, it would be all selected or none selected at all.
I've attached an image so you could see how it shows in SfDataGrid.
So, here's the code.
The OnLoad Form Event load the DataSource from DataTable and adds new Column to SfDataGrid.
DataTable dt = new DataTable();
data.DataSource = BindSource();
//If I remove this, the DataGrid cannot load dt.Columns.Add("Sel", typeof(bool));
data.Columns.Add(new GridCheckBoxColumn()
{
MappingName = "Sel",
HeaderText = "Sel",
AllowThreeState = false,
AllowText = true,
AllowCheckBoxOnHeader = true
});
dt.Columns.Add("Sel", typeof(bool));
Then, with your help, I can loop trrough the rows to check which Rows are Checked (Sel = true) and Ordem has Value and update my database:
var ctx = new DbContext();
var records = data.View.Records;
foreach (var record in records)
{
var dataRowView = record.Data as DataRowView;
if (dataRowView != null)
{
var selected = dataRowView.Row["Sel"];
if (selected.GetType() != typeof(DBNull) && (bool)selected)
{
try
{
var Ordem = dataRowView.Row["Ordem"];
int ID = Convert.ToInt32(dataRowView.Row["AvariaPID"]);
var query = ctx.tblStuff.FirstOrDefault(c => c.PID == ID);
if (ChkAtualizar.Checked & DataDe.Value != null) //This are 1 CheckBox and 1 DateTime outside DataGrid
{
query.Ordem = Ordem.ToString().Trim();
query.DataDefinitiva = DataDe.Value;
query.Estado = "Definitiva";
query.UpdatedBy = UserInfo.CurrentLoggedUser;
query.UpdatedOn = DateTime.Now;
}
So, this is working, but I need to select each Row 1 by one and not all or nothing!
Can you point me in right direction?
Thanks.
EDIT: Weel, I forgot to give Edit permission for the Sel Column since I locked my DataGrid to Edit. Now it works.
Thanks.
NK
Neelakandan Kannan
Syncfusion Team
August 13, 2018 10:28 AM UTC
Hi Carlos,
Thanks for your update.
We are glad to know that your reported problem has resolved. Please let us know if you have any other concerns.
Regards,
Neelakandan
SIGN IN To post a reply.
- 3 Replies
- 3 Participants
-
CF Carlos Ferreira
- Aug 8, 2018 08:37 PM UTC
- Aug 13, 2018 10:28 AM UTC