Forum guidelines
AuthorQuestion
get value of gridview cell on button click
Posted By Muhammad younas on 13 Oct 2012
hi
i have a gridview and in every row i have a button and i want to get the value of cell of that row of which button is clicked.

i cant use rowcomand event. i want to get cell value in buton click event.


younas
AuthorReply
Re: get value of gridview cell on button click
Posted By Sumit Kumar Sinha on 13 Oct 2012  
You can try this :- 

.aspx 

                   <asp:Button 
ID="lnk" runat="server" commandname="DataCommand" commandargument="<%# Eval("ID" )%>" text="Detail Information"/>


.aspx.cs 

Protected Void Grid1_RowCommand(object sender, GridViewEventArgs e) {  if(e.CommandName=="DataCommand")      {       // Get the value of command argument         var value= e.CommandArgument;       // Do whatever operation you want.       } }
                                         

Sumit Kumar Sinha
Re: get value of gridview cell on button click
Posted By Satyapriya Nayak on 13 Oct 2012  
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Display_selected_records._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">
    <div>
     <asp:GridView ID="GridView1" runat="server" AutoGenerateSelectButton="true"  AutoGenerateColumns="true"
        BackColor="#CCFFCC" Font-Bold="False" ForeColor="Maroon"
            onselectedindexchanged="GridView1_SelectedIndexChanged">
        <AlternatingRowStyle BackColor="#FFCC66" />
    </asp:GridView>
    <br />
    <asp:Label ID="lb1" runat="server" Text="ID : " Font-Bold="True" Width="100px"></asp:Label>
    <asp:Label ID="lbl_id" runat="server" ForeColor="#CC3300"></asp:Label>
    <br />
    <br />
    <asp:Label ID="lb3" runat="server" Text="NAME : " Font-Bold="True"
        Width="100px"></asp:Label>
    <asp:Label ID="lbl_name" runat="server" ForeColor="#CC3300"></asp:Label>
    <br />
    <br />
    <asp:Label ID="lb5" runat="server" Text="ADDRESS : " Font-Bold="True"
        Width="100px"></asp:Label>
    <asp:Label ID="lbl_address" runat="server" ForeColor="#CC3300"></asp:Label>
    <br />
    <br />
    <asp:Label ID="lb7" runat="server" Text="MARK : " Font-Bold="True"
        Width="100px"></asp:Label>
    <asp:Label ID="lbl_marks" runat="server" ForeColor="#CC3300"></asp:Label>
    <br />
    <br />
    <asp:Label ID="lb9" runat="server" Text="YEAR : " Font-Bold="True"
        Width="100px"></asp:Label>
    <asp:Label ID="lbl_year" runat="server" ForeColor="#CC3300"></asp:Label>
    </div>
    </form>
</body>
</html>



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Display_selected_records
{
    public partial class _Default : System.Web.UI.Page
    {
        string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        string str;
        SqlCommand com;
        SqlDataAdapter sqlda;
        DataSet ds;

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbl_id.Text = GridView1.SelectedRow.Cells[1].Text;
            lbl_name.Text = GridView1.SelectedRow.Cells[2].Text;
            lbl_address.Text = GridView1.SelectedRow.Cells[3].Text;
            lbl_marks.Text = GridView1.SelectedRow.Cells[4].Text;
            lbl_year.Text = GridView1.SelectedRow.Cells[5].Text;
        }

        void bindgrid()
        {
            SqlConnection con = new SqlConnection(strConnString);
            con.Open();
            str = "select * from student";
            com = new SqlCommand(str, con);
            sqlda = new SqlDataAdapter(com);
            ds = new DataSet();
            sqlda.Fill(ds, "student");
            GridView1.DataSource = ds;
            GridView1.DataMember = "student";
            GridView1.DataBind();
            con.Close();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindgrid();
            }
        }
    }
}


If this post helps you mark it as answer
Thanks

Re: get value of gridview cell on button click
Posted By Muhammad younas on 15 Oct 2012  
selectedindexchanged event cant be fired if i m not changing index of grid-view. i have a button in template field of grid-view, i want get value of row on button_click event when button is clicked of that row
younas
Aspose File APIs

SPONSORED BY

JetBrains ReSharper Ultimate
.NET code analysis, refactorings, navigation, unit testing, code coverage and profiling in Visual Studio, a bonus for C++ devs, bundled at 50% off.