Looping through datagrid rows

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

I'd like to loop through selected datagrid rows and extract specified
columns from those rows. How would I do that?

Thanks...

Dan
 
I'd like to loop through selected datagrid rows and extract specified
columns from those rows. How would I do that?

Thanks...

Dan

Don't try to loop through the datagrid rows, but loop through the
datatable rows instead (so use the datasource of your datagrid instead
of the datagrid itself).
 
How can I determine whether the row is selected in the datagrid? For
example, I click on rows 3, 7, and 9 of the datagrid. When I loop through
the dataset rows, how do I determine that they are selected?
 
How can I determine whether the row is selected in the datagrid? For
example, I click on rows 3, 7, and 9 of the datagrid. When I loop through
the dataset rows, how do I determine that they are selected?

Here's a little method that does the trick:

/// <summary>
/// Returns an arraylist with datarows that are selected in the
/// datagrid.
/// </summary>
/// <param name="datagrid">Datagrid.</param>
/// <returns>Arraylist with datarows that are selected in the
/// datagrid</returns>

public ArrayList GetSelectedRows(DataGrid datagrid)
{
ArrayList arrSelectedRows = new ArrayList();
DataSet dset = (DataSet)datagrid.DataSource;
for (int i=0; i<dset.Tables[0].Rows.Count; i++)
{
if (datagrid.IsSelected(i))
{
DataRow drow = dset.Tables[0].Rows;
arrSelectedRows.Add(drow);
}
}
return arrSelectedRows;
}
 
Thanks!

L# said:
How can I determine whether the row is selected in the datagrid? For
example, I click on rows 3, 7, and 9 of the datagrid. When I loop through
the dataset rows, how do I determine that they are selected?

Here's a little method that does the trick:

/// <summary>
/// Returns an arraylist with datarows that are selected in the
/// datagrid.
/// </summary>
/// <param name="datagrid">Datagrid.</param>
/// <returns>Arraylist with datarows that are selected in the
/// datagrid</returns>

public ArrayList GetSelectedRows(DataGrid datagrid)
{
ArrayList arrSelectedRows = new ArrayList();
DataSet dset = (DataSet)datagrid.DataSource;
for (int i=0; i<dset.Tables[0].Rows.Count; i++)
{
if (datagrid.IsSelected(i))
{
DataRow drow = dset.Tables[0].Rows;
arrSelectedRows.Add(drow);
}
}
return arrSelectedRows;
}
 
If the grid is sorted then this code won't work. Is there a solution
for this problem with grid sorting enabled?

Thank you.
 
If the grid is sorted then this code won't work. Is there a solution
for this problem with grid sorting enabled?

Thank you.

I made a little modification. The datatable itself is not sorted, but
the default dataview is sorted if the grid is sorted:

public ArrayList GetSelectedRows(DataGrid datagrid)
{
ArrayList arrSelectedRows = new ArrayList();
DataSet dset = (DataSet)datagrid.DataSource;
for (int i=0; i<dset.Tables[0].Rows.Count; i++)
{
if (datagrid.IsSelected(i))
{
DataRow drow =
dset.Tables[0].DefaultView.Row;
arrSelectedRows.Add(drow);
}
}
return arrSelectedRows;
}
 
Back
Top