How to get subset of rows displayed in current page of DataGrid?

G

Guest

I have a datagrid in my web control class that I am trying to get the current
rows displayed for. I have enabled paging on the datagrid so if the user is
currently on page 3 of 8, and if I have allowed for the table to show only 5
rows at a time, for example, I want to get the 5 rows that are displayed on
page 3 of 8. How do I do this???? Thanks!
 
G

Guest

Carolyn Vo said:
I have a datagrid in my web control class that I am trying to get the current
rows displayed for. I have enabled paging on the datagrid so if the user is
currently on page 3 of 8, and if I have allowed for the table to show only 5
rows at a time, for example, I want to get the 5 rows that are displayed on
page 3 of 8. How do I do this???? Thanks!

Hi Carolyn,

I think this post is better suited for
microsoft.public.dotnet.framewirk.aspnet

I have not clearly understood your problem but i guess you want to retrieve
the rows of a DataGrid which are displayed on the current page of datagrid.

If this is the problem, than i say this is not a problem as when you check
your datagrid on post back your datagrid will be having only those rows which
are displayed on the current page. As viewstate holds only the current page
of the datagrid not the entire datagrid.

Also your datagrid may be getting modified if you are calling DataBind()
again on postbacks, before processing the subset of rows you are interested
in.

This code snippet will give you better idea on how to select rows from
datagrid:
<CODE>

lblList.Text = "";
foreach(DataGridItem item in grdEmployee.Items)
{
if( item.ItemType == ListItemType.Item || item.ItemType ==
ListItemType.AlternatingItem)
// this is the row you are interested in, do your processing
lblList.Text += item.Cells[0].Text + " | " + item.Cells[1].Text + "<br>";
}

</CODE>
 
G

Guest

Hi Rahul,

Thanks for the post. I am trying to get the list of rows that will be
displayed in the page actually so that I can get the first row in the list
and set it to be "selected" in the list when the page comes up. Even if the
user paginates or sorts, when the page comes back I want the current subset
of rows shown to have the first row selected in the list so that its details
can be shown at the bottom of my page. So I need to know what the first item
is in the list to be shown in the datagrid BEFORE a postback is performed.
Does that help?

Carolyn
 
G

Guest

Hi Carolyn,

As I suggested in the last reply, you can get the first DataGridItem from
your DataGrid and use it to retrieve the row values. You may add some primary
key value as first column in your datagrid, on postback use this field to
show the detals of selected row i.e. first row initially.

In the code where you bind your datagrid with data source, retrieve the
first DataGridItem after binding, this will give you the row for which you
can show the details.

One thing more, if you want to handle the events server side then tha code
must *postback* and only after a postback occurrs you will get the control to
execute your code. Only thing you can control, in this case, is *sequence of
actions*. So, just check for the first item in datagrid when it is bound with
datasource.

If you are still facing problem then post some code example which will
clarify your problem and we can suggest more specific solution.

Hope it will solve your problem.
 

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

Top