How to get the Values of Selected Row from a Gridview using ASP.NET
2011-08-29 14:40
711 查看
In this Article you can learn how to get the values of selected row from a Gridview and display the values in textBoxes using C# code.
Selecting a Row in Gridview
Add one Gridview and bind a Data using SqlDataSource.and Add three Textboxes to display a data in Textboxes when the GridviewRow is Selected.
Ste the Gridview AutoGenerateColumns property to 'False'.
You will need to set the Bound column in order to see the text in the cells of the grid view control
<asp:GridView runat ="Server" ID="Gridview1" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"AutoGenerateSelectButton="True" OnSelectedIndexChanged="Gridview1_SelectedIndexChanged
Width="306px" Height="137px">
<Columns >
<asp:BoundField HeaderText ="Rowid" DataField ="Rowid" />
<asp:BoundField HeaderText ="Name" DataField ="Name" />
<asp:BoundField HeaderText ="Marks" DataField ="marks" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:shakeerConnectionString %>" SelectCommand="SELECT * FROM [emp]">
</asp:SqlDataSource>
In Gridview Properties set 'AutoGenerateSelectButton=True'.See below image of Gridview after setting 'AutoGenerateSelectButton=True'.
See the Desing view of your Gridview.you will find a Hyperlink with a text as 'Select'
Selecting Row:
Selecting 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 Gridview Events double Click on SelectedIndexChanged Event and write the below code:
protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
{
txtrowid.Text = Gridview1.SelectedRow.Cells[1].Text;
txtname.Text = Gridview1.SelectedRow.Cells[2].Text;
txtmarks.Text = Gridview1.SelectedRow.Cells[3].Text; //only for boundField
}
Complete .asp source code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat ="Server" ID="Gridview1" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AutoGenerateSelectButton="True" OnSelectedIndexChanged="Gridview1_SelectedIndexChanged" left: 266px; position: GridLines="None" Width="306px" Height="137px">
<Columns >
<asp:BoundField HeaderText ="Rowid" DataField ="Rowid" />
<asp:BoundField HeaderText ="Name" DataField ="Name" />
<asp:BoundField HeaderText ="Marks" DataField ="marks" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<SelectedRowStyle BackColor="Teal" ForeColor="Maroon" Font-Bold="True" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:shakeerConnectionString %>"
SelectCommand="SELECT * FROM [emp]"></asp:SqlDataSource>
<br />
<br />
<asp:TextBox ID="txtrowid" runat="server" Style="z-index: 101; left: 365px; position: absolute;top: 251px"></asp:TextBox>
<asp:TextBox ID="txtname" runat="server" Style="z-index: 102; left: 365px; position: absolute;top: 283px"></asp:TextBox>
<asp:TextBox ID="txtmarks" runat="server" Style="z-index: 103; left: 365px; position: absolute; top: 315px"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Style="z-index: 107; left: 305px; position: absolute;top: 253px" Text="RowId"></asp:Label>
<asp:Label ID="Label2" runat="server" Style="z-index: 105; left: 303px; position: absolute;
top: 287px" Text="Name"></asp:Label>
<asp:Label ID="Label3" runat="server" Style="z-index: 106; left: 306px; position: absolute;
top: 318px" Text="Marks"></asp:Label>
</div>
</form>
</body>
</html>
In .aspx.cs code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
{
txtrowid.Text = Gridview1.SelectedRow.Cells[1].Text;
txtname.Text = Gridview1.SelectedRow.Cells[2].Text;
txtmarks.Text = Gridview1.SelectedRow.Cells[3].Text;
}
}
Selecting a Row in Gridview
Add one Gridview and bind a Data using SqlDataSource.and Add three Textboxes to display a data in Textboxes when the GridviewRow is Selected.
Ste the Gridview AutoGenerateColumns property to 'False'.
You will need to set the Bound column in order to see the text in the cells of the grid view control
<asp:GridView runat ="Server" ID="Gridview1" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"AutoGenerateSelectButton="True" OnSelectedIndexChanged="Gridview1_SelectedIndexChanged
Width="306px" Height="137px">
<Columns >
<asp:BoundField HeaderText ="Rowid" DataField ="Rowid" />
<asp:BoundField HeaderText ="Name" DataField ="Name" />
<asp:BoundField HeaderText ="Marks" DataField ="marks" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:shakeerConnectionString %>" SelectCommand="SELECT * FROM [emp]">
</asp:SqlDataSource>
In Gridview Properties set 'AutoGenerateSelectButton=True'.See below image of Gridview after setting 'AutoGenerateSelectButton=True'.
See the Desing view of your Gridview.you will find a Hyperlink with a text as 'Select'
Selecting Row:
Selecting 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 Gridview Events double Click on SelectedIndexChanged Event and write the below code:
protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
{
txtrowid.Text = Gridview1.SelectedRow.Cells[1].Text;
txtname.Text = Gridview1.SelectedRow.Cells[2].Text;
txtmarks.Text = Gridview1.SelectedRow.Cells[3].Text; //only for boundField
}
Complete .asp source code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat ="Server" ID="Gridview1" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AutoGenerateSelectButton="True" OnSelectedIndexChanged="Gridview1_SelectedIndexChanged" left: 266px; position: GridLines="None" Width="306px" Height="137px">
<Columns >
<asp:BoundField HeaderText ="Rowid" DataField ="Rowid" />
<asp:BoundField HeaderText ="Name" DataField ="Name" />
<asp:BoundField HeaderText ="Marks" DataField ="marks" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<SelectedRowStyle BackColor="Teal" ForeColor="Maroon" Font-Bold="True" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:shakeerConnectionString %>"
SelectCommand="SELECT * FROM [emp]"></asp:SqlDataSource>
<br />
<br />
<asp:TextBox ID="txtrowid" runat="server" Style="z-index: 101; left: 365px; position: absolute;top: 251px"></asp:TextBox>
<asp:TextBox ID="txtname" runat="server" Style="z-index: 102; left: 365px; position: absolute;top: 283px"></asp:TextBox>
<asp:TextBox ID="txtmarks" runat="server" Style="z-index: 103; left: 365px; position: absolute; top: 315px"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Style="z-index: 107; left: 305px; position: absolute;top: 253px" Text="RowId"></asp:Label>
<asp:Label ID="Label2" runat="server" Style="z-index: 105; left: 303px; position: absolute;
top: 287px" Text="Name"></asp:Label>
<asp:Label ID="Label3" runat="server" Style="z-index: 106; left: 306px; position: absolute;
top: 318px" Text="Marks"></asp:Label>
</div>
</form>
</body>
</html>
In .aspx.cs code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
{
txtrowid.Text = Gridview1.SelectedRow.Cells[1].Text;
txtname.Text = Gridview1.SelectedRow.Cells[2].Text;
txtmarks.Text = Gridview1.SelectedRow.Cells[3].Text;
}
}
相关文章推荐
- CodeSnip: How to Get Id of the Record Using ASP.NET and SQL Server 2000
- How do I get ASP.NET Web API to return JSON instead of XML using Chrome
- How to get the data from a cell when I click on the GridButtonColumn of the same row
- Using ASP.NET and jQuery to Pass Multiple Values from a GridView to Another Page
- How to get all Errors from ASP.Net MVC modelState?
- How to get the Current Controller Name, Action, or ID in ASP.NET MVC
- HOW TO Analyze ASP.NET Web Application Performance by Using the Performance Administration Tool
- How to get the password text in a text with password property from another process using C++ - 用C++如何从不同进程获取密码框文本
- How to get a notification from Linux when the set of network interfaces changes
- How to get DataKey, RowIndex or Row from a GridView row event
- How to configure the Virtual Path of ASP.NET Development Server to be “Root”
- FW: How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
- How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
- Using Timer to get history values from EDNA database, an error is prompted in the page : "Sys.WebForms.PageRequestManagerTimeout
- How to debug ASP.Net program without the privilege of administrator
- How to update multiple columns of one table using values from another table?
- [转][ASP.NET]How to easily insert row in GridView with SqlDataSource?
- How to read the query string params of a ASP.NET raw URL?
- How to get Hidden Column Value in GridView(ASP.net GridView如何获取隐藏列的值)
- How to get the DOM of a WebBrowser control from a window handle (VB6)