By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,899 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,899 IT Pros & Developers. It's quick & easy.

Looping through datagrid rows

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

Thanks...

Dan
Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
L#
On Thu, 5 Feb 2004 17:13:03 -0500, "Dan" <da*@dontspamme.com> wrote:
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).
--
Ludwig
mailto:ludwig_(nospamplease)stuyck@pandora(nospamp lease).be
Nov 15 '05 #2

P: n/a
Dan
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?
"L#" <ludwig_(nospamplease)stuyck@pandora(nospamplease) .be> wrote in message
news:vm********************************@4ax.com...
On Thu, 5 Feb 2004 17:13:03 -0500, "Dan" <da*@dontspamme.com> wrote:
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).
--
Ludwig
mailto:ludwig_(nospamplease)stuyck@pandora(nospamp lease).be

Nov 15 '05 #3

P: n/a
L#
On Thu, 5 Feb 2004 17:40:55 -0500, "Dan" <da*@dontspamme.com> wrote:
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[i];
arrSelectedRows.Add(drow);
}
}
return arrSelectedRows;
}
--
Ludwig
mailto:ludwig_(nospamplease)stuyck@pandora(nospamp lease).be
Nov 15 '05 #4

P: n/a
Dan
Thanks!

"L#" <ludwig_(nospamplease)stuyck@pandora(nospamplease) .be> wrote in message
news:is********************************@4ax.com...
On Thu, 5 Feb 2004 17:40:55 -0500, "Dan" <da*@dontspamme.com> wrote:
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 throughthe 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[i];
arrSelectedRows.Add(drow);
}
}
return arrSelectedRows;
}
--
Ludwig
mailto:ludwig_(nospamplease)stuyck@pandora(nospamp lease).be

Nov 15 '05 #5

P: n/a
If the grid is sorted then this code won't work. Is there a solution
for this problem with grid sorting enabled?

Thank you.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #6

P: n/a
L#
On Thu, 05 Feb 2004 15:59:34 -0800, Koushik K <ko*******@hotmail.com>
wrote:
If the grid is sorted then this code won't work. Is there a solution
for this problem with grid sorting enabled?

Thank you.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


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[i].Row;
arrSelectedRows.Add(drow);
}
}
return arrSelectedRows;
}
--
Ludwig
mailto:ludwig_(nospamplease)stuyck@pandora(nospamp lease).be
Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.