开发者

What is an efficient way of setting the page of an ASP.net GridView to the page containing a given row?

I have a web page with an ASP.net GridView (with paging). The GridView has over 10,000 rows. Each row has an "ID" field.

This is an oversimplification, but suppose I want the user to be able to type an arbitry row ID into a text field and be taken to the GridView with the page index automatically set to the index of the page that contains the row with the specified ID.

I have tried the following, but it doesn't work:

        D开发者_JAVA百科ataSet ds = ReportManager.GetDevices(AdHocQuery);
        lblRecordCount.Text = String.Format("{0:#,0}", ds.Tables[0].Rows.Count);
        string sortDirection = AdHocQuery.SortDirection == "Descending" ? " DESC" : " ASC";
        DataView dv = new DataView(ds.Tables[0], string.Empty, AdHocQuery.SortColumnName + " " + sortDirection, DataViewRowState.CurrentRows);
        gvAsset.DataSource = dv;
        gvAsset.DataBind();

        // If there was a device ID passed in the Query String, then change the page to the page that contains the given ID.

        if (!string.IsNullOrEmpty(DeviceId))
        {
            // First, find the row number of the given device Id
            int rowNumber = 0;
            int i;
            for (i = 0; i < dv.Table.Rows.Count; i++)
            {
                DataRow row = dv.Table.Rows[i];
                if (row.Field<Guid>("deviceId").ToString().ToUpper() == DeviceId.ToUpper())
                {
                    rowNumber = i;
                    break;
                }
            }

            // The page index is going to be the row Number divided by the Page size
            // For example, if the page size is 10...
            // row 5 is on page 0,
            // row 15 is on page 1,
            // row 28 is on page 2, and so on
            int pageIndex = rowNumber / gvAsset.PageSize;
            gvAsset.PageIndex = pageIndex;

This code compiles, but it consistently shows the wrong page of the GridView.

How can I code this so that the correct page is shown?


I believe you need to call DataBind() in order for your change to the PageIndex property to be in place.


Call gvAsset.PageIndex = pageIndex before binding the grid


You can try something like this. It worked in my situation.

//assuming there are SomeIDs from 1 to 100 in the database
//assuming pagesize of gridview is 10

DataTable table = GetSomeDataFunction(); //your data retrieval method

for (int i = 0; i < table.Rows.Count; i++)
{
    if (table.Rows[i].Field<int>("SomeID") == 11) //11 = ID you're looking for
    {
        GridView1.PageIndex = (i / GridView1.PageSize);
        break;
    }
}

GridView1.DataSource = table;
GridView1.DataBind();


I have found the answer to my own question. After searching around for answers, I discovered that if I want to enumerate thru the list of rows in the code-behind, I need to call the DataView.GetEnumerator() function. Here is what the code looks like:

        DataSet ds = ReportManager.GetDevices(AdHocQuery);
        lblRecordCount.Text = String.Format("{0:#,0}", ds.Tables[0].Rows.Count);
        string sortDirection = AdHocQuery.SortDirection == "Descending" ? " DESC" : " ASC";
        DataView dv = new DataView(ds.Tables[0], string.Empty, AdHocQuery.SortColumnName + " " + sortDirection, DataViewRowState.CurrentRows);
        gvAsset.DataSource = dv;
        gvAsset.DataBind();

        // If there was a device ID passed in the Query String, then change the page to the page that contains the given ID.
        if (!string.IsNullOrEmpty(DeviceId))
        {
            // First, find the row number of the given device Id
            int rowNumber = 0;
            int i = 0;

            System.Collections.IEnumerator iterator = dv.GetEnumerator();
            while (iterator.MoveNext())
            {
                DataRowView drv = (DataRowView)iterator.Current;
                DataRow row = drv.Row;
                if (row.Field<Guid>("deviceid").ToString().ToUpper() == DeviceId.ToUpper())
                {
                    rowNumber = i;
                    break;
                }
                i++;
            }                

            // The page index is going to be the row Number divided by the Page size
            // For example, if the page size is 10...
            // row 5 is on page 0,
            // row 15 is on page 1,
            // row 28 is on page 2, and so on
            int pageIndex = rowNumber / gvAsset.PageSize;
            gvAsset.PageIndex = pageIndex;
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜