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