Subscribe to Subscribers

Online Members

Kapil
saveed
More...

Resources » Code Snippets » ASP.NET GridView

How to get gridview row id using asp.net


Posted Date:     Category: ASP.NET GridView    
Author: Member Level: Gold    Points: 10


In this article I have explained about How to get Gridview rowid using asp.net.Each steps I have clearly explained in this article.



In this article I have explained about How to get Gridview rowid using asp.net.Each steps I have clearly explained in this article. Just drag and drop one grid view control from tool box and place two text box control within template column.



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)
{
if (!Page.IsPostBack)
{

DataRow dtrow;
DataTable dtNewTable;
dtNewTable = new DataTable();
DataColumn dtcol, dtcol1;
dtcol = new DataColumn();
dtcol1 = new DataColumn();
dtcol.DataType = System.Type.GetType("System.Int32");
dtcol.ColumnName = "Religionid";
dtNewTable.Columns.Add(dtcol);
dtcol1.DataType = System.Type.GetType("System.String");
dtcol1.ColumnName = "ReligionName";
dtNewTable.Columns.Add(dtcol1);
int i;
for (i = 0; i < 4; i++)
{


dtrow = dtNewTable.NewRow();
dtrow["Religionid"] = i;
dtrow["ReligionName"] = "";
dtNewTable.Rows.Add(dtrow);

GridView1.DataSource = dtNewTable;
GridView1.DataBind();
}

}

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView vDataRowView = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{


e.Row.Attributes["onclick"] = "javascript:alert('RowID= "+e.Row.RowIndex.ToString()+"')";


}
}
}
<%@ 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 runat="server">
<title>Untitled Page</title>


</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BorderWidth="1px" CellPadding="4" Width="472px" OnRowDataBound="GridView1_RowDataBound">

<Columns>

<asp:TemplateField HeaderText="UserID">

<ItemTemplate>
<asp:TextBox ID="TextBox1" Text='<%# Bind("Religionid") %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserID">

<ItemTemplate>
<asp:TextBox ID="TextBox2" Text='<%# Bind("ReligionName") %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>



</Columns>



</asp:GridView>


</form>
</body>
</html>






Did you like this resource? Share it with your friends and show your love!


Responses to "How to get gridview row id using asp.net"
Author: Shivprasad H    23 Jul 2012Member Level: Bronze   Points : 0
If you are looking to access row on gridview row command then here is the code,

int rowIndex = Convert.ToInt32(e.CommandArgument);
GridViewRow grow = grvXYZ.Rows[rowIndex];



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Scrollable Gridview with Fixed Header
    Previous Resource: How to use Gridview with checkbox control using asp.net
    Return to Resources
    Post New Resource
    Category: ASP.NET GridView


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    How to get gridview row id using asp.net  .  

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2013 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.