Wednesday, January 27, 2010

Getting Data From Selected Cell of a GridView in ASP.net

To display or fetch data of a cell from GridView use the fallowing steps:

1.Bind the Gridview with Data.
2. Enable Selection in Gridview by using its smart task panel.
3.Double click on the GridView so that it generates the fallowing event:


protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }


4. Now write the fallowing code inside the SelectedIndexChanged event


protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = GridView1.SelectedRow;
        Label1.Text = row.Cells[3].Text;
    }


The first line fetches the Data of the Selected Row from the GridView and
the second line displays the 3rd cell data on a Label.

5. Specify the Index of the Cell in Cells[] which you want to display.

6.When you Run the page Select Button is generated with each Row. Press the Select button of the corresponding Row and you will see the respective Cell data on the Label.

































Here in the example you see the design page with GridView and a Label.
In the next picture you see the Contact column of 3rd row being displayed on the Label.

No comments:

Post a Comment