DataGridItem Collection / DataGrid Paging Question

G

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;
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;
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??
 
B

Brendan K

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.
 
G

Guest

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
 
G

GD

Exactly, I solved this by iterating through the data source.

Thank you both for the replies
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top