You must Sign In to post a response.
  • Category: ASP.NET

    How to get the gridview cell value in asp.net

    Gridview as follows

    Link Course Code Days Startdt Enddt

    Click here ASM CC 5 10 May 2015 15 May 2015
    21 May 2015 25 May 2015

    When i click the link button in gridview i want to get Course(ASM) for that i written code as follows

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
    for(int i= 0; i < gvRank.Rows.Count; i++)
    {
    string Course = gvRank.SelectedRow.Cells[1].Text;
    Session["course"] = Course;
    {
    Response.Redirect("Eligibility.aspx");
    }
    }
    }

    But When i debug and check in below line as follows

    string Course = gvRank.SelectedRow.Cells[1].Text;

    In the above line course name is not retrived.

    please help me what is the problem in my above code
  • #764883
    hi

    check particular row get the value for your requirement
    because for loop it is getting all records in your gridview so.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #764884
    Hi Rao,

    That's depend upon the template which you used in design time, we have different templates available for gridview control like BoundField, TemplateField....

    As you said that is link button, so using BoundField you can't able to design LinkButton, you must and should use TemplateField for design the LinkButton.

    For calling TemplateField item in button click event you may follow the below code snippet.


    Protected void btnClick(object sender, EventArgs e)
    {
    foreach(GridviewRow row in gv.Rows)
    {
    LinkButton lnk=(LinkButton)row.FindControl("lnkCourse");

    }
    }


    Hope this will be helpful to you.. If you still having doubts, then please let me know.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #764885
    Hai Rao,
    You can simply assign the value of the id to the link button so that when the data will be filled to the gridview, it will be binded to the linkbutton automatically and then you can retrieve it easily.
    Else you have to check the controls and values in the current cell. I think the value you can get in the second control as below:

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
    for(int i= 0; i < gvRank.Rows.Count; i++)
    {
    string Course = gvRank.SelectedRow.Cells[1].Controls[1].ToString();
    Session["course"] = Course;
    {
    Response.Redirect("Eligibility.aspx");
    }
    }

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #764902
    'SelectedRow' will not help[ you here, if your linkbutton is on gridview then you need to fire 'onRowCommand' event, OR you can use selected index changed event as follows
    protected void OnSelectedIndexChanged(object sender, EventArgs e)
    {
    //Accessing BoundField Column
    string name = GridView1.SelectedRow.Cells[0].Text;
    //Accessing TemplateField Column controls
    string country = (GridView1.SelectedRow.FindControl("lblCountry") as Label).Text;

    lblValues.Text = "<b>Name:</b> " + name + " <b>Country:</b> " + country;
    }

    see below link
    http://www.aspsnippets.com/Articles/How-to-get-Selected-Row-cell-value-from-GridView-in-ASPNet.aspx

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #764903
    Hi,
    Try following code:
    protected void lnkButton_Click(object sender, EventArgs e)
    {
    LinkButton lnkDemo = (sender) as LinkButton;
    Int32 iRowCount = Convert.ToInt32(lnkDemo.ClientID.Split('_')[lnkDemo.ClientID.Split('_').Length - 1]);//ID is generated as gvDemo_lnkButton_0
    string szCode = gvDemo.Rows[iRowCount].Cells[1].Text;
    }
    And with reference to your question "http://www.dotnetspider.com/forum/344131-Link-button-should-be-visible-false.aspx", you have a textbox for "Course" right?? If yes then replace the last line from above event with this one:
    TextBox txtDemo = gvDemo.Rows[iRowCount].FindControl("DemoTextBox") as TextBox;
    string szCourse = txtDemo.Text;

    Hope it helps.
    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com

  • #764918
    Please check below code,


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    string strVal = e.Row.Cells[1].Text;//Change the cell number according to your requirement.
    }
    }


    Regards,
    Asheej T K
    Microsoft MVP[ASP.NET/IIS]
    DotNetSpider MVM

  • #764934
    Hi
    Rao

    If you need link button navigate some page means try this code


    DataTable dt = new DataTable();
    DataRow dr;
    dt.Columns.Add("Course");
    dt.Columns.Add("Grade");
    dt.Columns.Add("DOJ");
    dt.Columns.Add("DOP");
    dr = dt.NewRow();
    dr[0] = "ASM";
    dr[1] = "BB";
    dr[2] = DateTime.Now;
    dr[3] = DateTime.Now;
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr[0] = "";
    dr[1] = "CC";
    dr[2] = DateTime.Now;
    dr[3] = DateTime.Now;
    dt.Rows.Add(dr);


    dr = dt.NewRow();
    dr[0] = "BSM";
    dr[1] = "ZZ";
    dr[2] = DateTime.Now;
    dr[3] = DateTime.Now;
    dt.Rows.Add(dr);


    Grd1.DataSource=dt;
    Grd1.DataBind();


    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    TableCell statusCell = e.Row.Cells[1];
    Label lblCourse = e.Row.FindControl("lblCourse") as Label;
    LinkButton lnk = e.Row.FindControl("lnk1") as LinkButton;
    Label lblGrade = e.Row.FindControl("lbl2") as Label;
    if (lblCourse.Text !="")
    {
    lnk.Attributes["onclick"] = "javascript:window.open('WebForm3.aspx?Grade=" + lblGrade.Text + "')";
    }
    else
    {
    lnk.Visible = false;
    }
    }


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #767401
    Nice Article. I have no idea about grid view cell. But after reading it I got idea about how to get the grid view cell value in asp.net. Really, this is helpful for asp.net users. Those have any idea regarding Asp.net can understand easily by reading and also can give comment. As a web hosting user I hosted my website through Myasp.net. Thanks a lot.


Sign In to post your comments