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

DataGridItem Collection / DataGrid Paging Question

P: n/a
GD
Got a simple ASPX page (.NET v1.1) with a DataGrid displaying a list of
widgets. If the page has a QueryString called WidgetId specific, I need
to automatically mark the specific DataGridItem as selected.

Pretty simple, here's what I did:

for (int i=0; i<dgWidgets.Items.Count; i++)
{
DataGridItem oDataGridItem = dgWidgets.Items[i];
if (oDataGridItem.Cells[0].Text == WidgetId)
{
dgWidgets.SelectedIndex = i;
dgWidgets_OnSelectedIndexChanged(this, new EventArgs());
return;
}
}

Works like a charm ... However, if the DataGridItem I want to select is
not on the first page of the DataGrid, I run into problems. So, if I
modify the above code slightly to account for DataGrid Paging, here's
what I come up with.

for (int i=0; i<dgWidgets.Items.Count; i++)
{
DataGridItem oDataGridItem = dgWidgets.Items[i];
if (oDataGridItem.Cells[0].Text == WidgetId)
{
dgWidgets.SelectedIndex = i;
if (i > dgWidgets.PageSize)
{
int iPageIndex =
Convert.ToInt32(Math.Ceiling(i/dgWidgets.PageSize));
dgWidgets.CurrentPageIndex = iPageIndex;
dgWidgets_OnPageIndexChanged(this, new
DataGridPageChangedEventArgs(this, iPageIndex));
}
dgWidgets_OnSelectedIndexChanged(this, new EventArgs());
return;
}
}

However, when stepping through this, I realized that when paging is
enabled, and I'm iterating through the DataGridItems collection, the
collection only contains the Items in the "current page"

Sort of a catch 22 here ... I need to calculate the proper page index
that an Item sits in, but have no access to the complete collection ...

Maybe use the index of the DataTable instead??

Feb 18 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I suggest storing the WidgetIDs on page load in an arraylist or collection.
Then when the page reloads because of paging walk the collection or
arraylist and automatically mark the widgets.

ie

foreach(Widgets widgetid in StoredWidgets)
{
//grid code... to mark selected widgets.
}

Hope this helps.

"GD" <ge**********@gmail.com> wrote in message
news:11********************@g47g2000cwa.googlegrou ps.com...
Got a simple ASPX page (.NET v1.1) with a DataGrid displaying a list of
widgets. If the page has a QueryString called WidgetId specific, I need
to automatically mark the specific DataGridItem as selected.

Pretty simple, here's what I did:

for (int i=0; i<dgWidgets.Items.Count; i++)
{
DataGridItem oDataGridItem = dgWidgets.Items[i];
if (oDataGridItem.Cells[0].Text == WidgetId)
{
dgWidgets.SelectedIndex = i;
dgWidgets_OnSelectedIndexChanged(this, new EventArgs());
return;
}
}

Works like a charm ... However, if the DataGridItem I want to select is
not on the first page of the DataGrid, I run into problems. So, if I
modify the above code slightly to account for DataGrid Paging, here's
what I come up with.

for (int i=0; i<dgWidgets.Items.Count; i++)
{
DataGridItem oDataGridItem = dgWidgets.Items[i];
if (oDataGridItem.Cells[0].Text == WidgetId)
{
dgWidgets.SelectedIndex = i;
if (i > dgWidgets.PageSize)
{
int iPageIndex =
Convert.ToInt32(Math.Ceiling(i/dgWidgets.PageSize));
dgWidgets.CurrentPageIndex = iPageIndex;
dgWidgets_OnPageIndexChanged(this, new
DataGridPageChangedEventArgs(this, iPageIndex));
}
dgWidgets_OnSelectedIndexChanged(this, new EventArgs());
return;
}
}

However, when stepping through this, I realized that when paging is
enabled, and I'm iterating through the DataGridItems collection, the
collection only contains the Items in the "current page"

Sort of a catch 22 here ... I need to calculate the proper page index
that an Item sits in, but have no access to the complete collection ...

Maybe use the index of the DataTable instead??


Feb 18 '06 #2

P: n/a
HI GD,

When in paging, if you loop thru Datagrid Items, it only accesses items in
current page. Hence, it’s better to loop thru its data source, and map to
proper pageIndex and item (row) index.

HTH
"GD" wrote:
Got a simple ASPX page (.NET v1.1) with a DataGrid displaying a list of
widgets. If the page has a QueryString called WidgetId specific, I need
to automatically mark the specific DataGridItem as selected.

Pretty simple, here's what I did:

for (int i=0; i<dgWidgets.Items.Count; i++)
{
DataGridItem oDataGridItem = dgWidgets.Items[i];
if (oDataGridItem.Cells[0].Text == WidgetId)
{
dgWidgets.SelectedIndex = i;
dgWidgets_OnSelectedIndexChanged(this, new EventArgs());
return;
}
}

Works like a charm ... However, if the DataGridItem I want to select is
not on the first page of the DataGrid, I run into problems. So, if I
modify the above code slightly to account for DataGrid Paging, here's
what I come up with.

for (int i=0; i<dgWidgets.Items.Count; i++)
{
DataGridItem oDataGridItem = dgWidgets.Items[i];
if (oDataGridItem.Cells[0].Text == WidgetId)
{
dgWidgets.SelectedIndex = i;
if (i > dgWidgets.PageSize)
{
int iPageIndex =
Convert.ToInt32(Math.Ceiling(i/dgWidgets.PageSize));
dgWidgets.CurrentPageIndex = iPageIndex;
dgWidgets_OnPageIndexChanged(this, new
DataGridPageChangedEventArgs(this, iPageIndex));
}
dgWidgets_OnSelectedIndexChanged(this, new EventArgs());
return;
}
}

However, when stepping through this, I realized that when paging is
enabled, and I'm iterating through the DataGridItems collection, the
collection only contains the Items in the "current page"

Sort of a catch 22 here ... I need to calculate the proper page index
that an Item sits in, but have no access to the complete collection ...

Maybe use the index of the DataTable instead??

Feb 19 '06 #3

P: n/a
GD
Exactly, I solved this by iterating through the data source.

Thank you both for the replies

Feb 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.