Access a value using SelectedIndexChanged() event when a row in a GridView control is selected
There are many instances when the value in a cell is required. For example, when we need to display more information about the selected row. This example demonstrates how access a value in a GridView cell from the selected row using SelectedIndexChanged() event.<asp:gridview id="ItemsGridView" datasourceid="MyDataSource" autogeneratecolumns="true" autogenerateselectbutton="true" allowpaging="true" selectedindex="0" onselectedindexchanged="ItemsGridView_SelectedIndexChanged" OnSelectedIndexChanging="ItemsGridView_SelectedIndexChanging" runat="server"> <selectedrowstyle backcolor="BlanchedAlmond" forecolor="DarkBlue" BorderColor="Beige" BorderStyle="Dotted" BorderWidth="3" Font-Size="Medium" font-bold="true"/> </asp:gridview>
In the given code, the method 'OnSelectedIndexChanged' of the GridView control is assigned to the event handler ItemsGridView_SelectedIndexChanged.
<asp:label id="Message" forecolor="Red" runat="server"/> <asp:SqlDataSource ID="MyDataSource" ConnectionString="<%$Connectionstrings:ERPConnectionString%>" SelectCommand="SELECT ItemID,ItemName,ItemType,ClStk FROM ItemTable" runat="server"/>
screen
The SelectedIndexChanged event is raised when a row's Select button is clicked. We use the SelectedIndexChanged eventhandling method to display the currently selected row. Using the SelectedRow property, we get the currently selected row in the gridView control. Then, we access the data in the cells of the row using the statement:
row.Cells[2].Text
The above statement displays the value in the third cell of the row.
The following code is the event handler for the SelectedIndexChanged event.
<asp:gridview id="ItemsGridView" datasourceid="MyDataSource" autogeneratecolumns="true" autogenerateselectbutton="true" allowpaging="true" selectedindex="0" onselectedindexchanged="ItemsGridView_SelectedIndexChanged" OnSelectedIndexChanging="ItemsGridView_SelectedIndexChanging" runat="server"> <selectedrowstyle backcolor="BlanchedAlmond" forecolor="DarkBlue" BorderColor="Beige" BorderStyle="Dotted" BorderWidth="3" Font-Size="Medium" font-bold="true"/> </asp:gridview>
GridView Articles
- GridView AutoGenerateColumns property
- Bind GridView control with SqlDataSource control
- DataBinding in a GridView control
- Binding a GridView programmatically with dataset
- Binding a GridView programmatically with Generic List Collection
- Using fields with the GridView control
- Adding Boundfields to a GridView control
- Adding button fields to a GridView control
- Create Templatefields in a GridView control
- Access a value using SelectedIndexChanged() event when a row in a GridView is selected
- How to add a new row in the GridView control by using the GridView footer template
- Gridview datakeynames
- How to select a particular row in a GridView control using SelectedDataKey method
- Formatting GridView control with CSS
- GridView rowDataBound event
Become an Expert
Learn More
Q1. What is Azure Platform?
GridView CommandField example
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Query entity data model using linq to entities
Difference between arraylist and list collection
How to create a Web service using Visual Studio.net
FormView DataBound Event
Calling base class constructor in C#
Convert a sequence to a generic list using ToList()method
ERP Software Development
Project ideas for students
Accounting Software
Creating an ASP.Net MVC 3 application
Using assemblies in .net applications
How to implement form validation using ASP.Net 2.0 Validation Controls
Constructors in Visual Basic.Net
GridView CommandField example
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Query entity data model using linq to entities
Difference between arraylist and list collection
How to create a Web service using Visual Studio.net
FormView DataBound Event
Calling base class constructor in C#
Convert a sequence to a generic list using ToList()method
ERP Software Development
Project ideas for students
Accounting Software
Creating an ASP.Net MVC 3 application
Using assemblies in .net applications
How to implement form validation using ASP.Net 2.0 Validation Controls
Constructors in Visual Basic.Net