How To Get The Selected Row In A GridView Using ASP.NET
Có thể bạn quan tâm
In this article, you will learn how to get the selected row in a GridView and display the values in TextBoxes using ASP.NET. A GridView allows us to select only a single row at a time. The sample makes use of the database. We will be pulling data from the UserDetail table.
Creating Table in SQL Server Database
Now create a table named UserDetail with the columns UserID and UserName. The table looks as below.

Now insert data into the table.

First of all drag the GridView control from the Data controls menu. It will add the GridView control's HTML source code as given above.
- <asp:GridViewID="GridView2"runat="server"></asp:GridView>
.aspx source code
- <%@ Page Language="C#"AutoEventWireup="true"CodeBehind="WebForm1.aspx.cs"Inherits="WebApplication120.WebForm1" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- <headrunat="server">
- <title></title>
- </head>
- <body>
- <formid="form1"runat="server">
- <div>
- UserID:
- <asp:TextBoxID="TextBoxUserID"runat="server"></asp:TextBox>
- <br/>
- UserName:
- <asp:TextBoxID="TextBoxUserName"runat="server"></asp:TextBox>
- <br/>
- <br/>
- <asp:GridViewID="GridView1"runat="server"AutoGenerateSelectButton="True"OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
- BackColor="#DEBA84"BorderColor="#DEBA84"BorderStyle="None"BorderWidth="1px"
- CellPadding="3"CellSpacing="2"AutoGenerateColumns="False">
- <FooterStyleBackColor="#F7DFB5"ForeColor="#8C4510"/>
- <HeaderStyleBackColor="#A55129"Font-Bold="True"ForeColor="White"/>
- <PagerStyleForeColor="#8C4510"HorizontalAlign="Center"/>
- <RowStyleBackColor="#FFF7E7"ForeColor="#8C4510"/>
- <SelectedRowStyleBackColor="#738A9C"Font-Bold="True"ForeColor="White"/>
- <SortedAscendingCellStyleBackColor="#FFF1D4"/>
- <SortedAscendingHeaderStyleBackColor="#B95C30"/>
- <SortedDescendingCellStyleBackColor="#F1E5CE"/>
- <SortedDescendingHeaderStyleBackColor="#93451F"/>
- <Columns>
- <asp:BoundFieldHeaderText="UserID"DataField="UserID"/>
- <asp:BoundFieldHeaderText="UserName"DataField="UserName"/>
- </Columns>
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>

See the Design view of your GridView. You will find a Hyperlink with a text as 'Select'.

Now add the following namespace.
- using System.Data.SqlClient;
- using System.Data;
- string strConnection = "Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;";
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data.SqlClient;
- using System.Data;
- namespace WebApplication120
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protectedvoid Page_Load(object sender, EventArgs e)
- {
- show();
- }
- privatevoid show()
- {
- {
- SqlConnection con = new SqlConnection("Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;");
- string strSQL = "Select * from UserDetail";
- SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);
- DataSet ds = new DataSet();
- dt.Fill(ds, "UserDetail");
- con.Close();
- GridView1.DataSource = ds;
- GridView1.DataBind();
- }
- }
- }
- }

Selecting Row Selecting the row event is fired when you make a click on the select link. 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:
- protectedvoid GridView1_SelectedIndexChanged(object sender, EventArgs e)
- {
- TextBoxUserID.Text = GridView1.SelectedRow.Cells[1].Text;
- TextBoxUserName.Text = GridView1.SelectedRow.Cells[2].Text;
- }

Some Helpful Resources
- Get the values of selected row from a Gridview in TextBox using AJAX
- Change Rows Background color in GridView with selected Criteria
- Developing a Multi-Select ASP.NET GridView using JQuery
- Swapping GridView rows Up and Down
Từ khóa » Visual Basic Gridview Select Row
-
Select The Row In Data Grid View - - Stack Overflow
-
DataGridView - Select Row Programatically - MSDN - Microsoft
-
Get The Selected Cells, Rows, And Columns In DataGridView Control
-
VB.NET - Get Selected Row Values From DataGridView ... - YouTube
-
VB.NET - How To Set Selected Row Values From DataGridView Into ...
-
Get The Selected Row In Datagridview - - Daniweb
-
“how To Get Value Of Selected Row From Datagridview In ” Code ...
-
Copy Selected Rows From One Datagridview To Another VB.NET
-
Display DataGridView Selected Row In TextBoxes In Windows ...
-
GridView.SelectRow(Int32) Method | WinForms Controls
-
Selecting Rows Programmatically In Datagridview
-
VB.NET - Get Selected Row Values From DataGridView To InputBox ...
-
DataGridView : Active-row Versus Selected-row - Bytes
-
How Do You Get The Values Of Selected Row From A DataGridView In ...