Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView in ASP.Net -- Selecting the correct row

I have a page that includes a GridView in it. That GridView is paged with 10 items at a time. Normally, I want the user to select the item from the GridView and populate the FormView. This works well.

I also want to support a query parameter ?ID=n where the page will load the specified item.

How do I tell the DataGrid or the data source which item to set as the data context?

I want the DataGrid to go to the proper page and select the item, showing the specified item in the FormView.

I can't figure out how to do this other than limiting the data source to the specific item, which is confusing to the user.

Any thoughts?

like image 957
Brian Genisio Avatar asked Aug 31 '09 01:08

Brian Genisio


People also ask

How to Get selected row in GridView ASP net?

Selecting Row If you need any particular item in that row you can easily select it using the cells property. In the Gridview, double-Click on the SelectedIndexChanged Event and write the following code: protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

How do you find data from the grid?

We can get all the Grid Data in DataSource property of Grid in code behind. So, you can get all the Grid data in server edit event using DataSource property of Grid. Please refer to the below help document and code example. var data = this.

What is RowDataBound event in GridView?

The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the row, whenever this event occurs.


1 Answers

If you set the DataKey field of the GridView to contain the primary key, there is this CodeProject article on how to set the selected index of a gridview, based on the key value of the record, using an extension method:

public static void SetRowValueValueByKey(this GridView GridView, string DataKeyValue)
{
    int intSelectedIndex = 0;
    int intPageIndex = 0;
    int intGridViewPages = GridView.PageCount;

    // Loop thru each page in the GridView
    for (int intPage = 0; intPage < intGridViewPages; intPage++)
    {
        // Set the current GridView page
        GridView.PageIndex = intPage;
        // Bind the GridView to the current page
        GridView.DataBind();
        // Loop thru each DataKey in the GridView
        for (int i = 0; i < GridView.DataKeys.Count; i++)
        {
            if (Convert.ToString(GridView.DataKeys[i].Value) == DataKeyValue)
            {
                // If it is a match set the variables and exit
                intSelectedIndex = i;
                intPageIndex = intPage;
                break;
            }
        }
    }

    // Set the GridView to the values found
    GridView.PageIndex = intPageIndex;
    GridView.SelectedIndex = intSelectedIndex;
    GridView.DataBind();
}
like image 53
staterium Avatar answered Sep 20 '22 18:09

staterium