Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Im having datagridview in the form in which im using link button in one column...im fetching data from the database...when i click the link button i want to display the data found in that cell..the data is displayed in the label...
Posted
Comments
Bala Selvanayagam 30-Sep-11 6:54am    
dg.CurrentRow.Cells[4].Value.ToString()

dg is the datagridview name, this helpfull ?
Xeshan Ahmed 3-Oct-11 5:39am    
wel here cel is static, so we have to bind a dynamic event

use the link button in a following way
ASP.NET
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Xeshan's Link Button


and in code behind define the click event and simply parse sender to link button and get its text
C#
protected void LinkButton1_Click(object sender, EventArgs e)
{
   //This is text of your link button
  //((LinkButton)sender).Text;
}
 
Share this answer
 
v3
Comments
Venkatesh Mookkan 3-Oct-11 6:09am    
Since Prem has mentioned the control as DataGridView is definitely a Window Form but you answer for ASP.NET

You might have to modify your answer!
Xeshan Ahmed 3-Oct-11 6:26am    
yup i have to thanks Venkatesh Mookkan for suggestion
XML
<asp:TemplateField HeaderText="Select">
                           <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Fetch </asp:LinkButton>

                           </asp:TemplateField>



code behind

C#
protected void LinkButton1_Click(object sender, EventArgs e)
   {
      lbl1.Text = GridView1.SelectedRow.Cells[1].Text;
      lbl2.Text = GridView1.SelectedRow.Cells[2].Text;
   }



Yogesh.
 
Share this answer
 
ASP.NET
<asp:gridview id="GridView1" runat="server" width="100%" onrowcommand="GridView1_RowCommand" onpageindexchanging="GridView1_PageIndexChanging" onselectedindexchanged="GridView1_SelectedIndexChanged" xmlns:asp="#unknown">
                                            <columns><asp:templatefield headertext="Details">
                <itemtemplate>
                    <asp:linkbutton id="lnkDet" commandname="cmdBind" commandargument="<%# ((GridViewRow) Container).RowIndex %>" runat="server" causesvalidation="false">View Details
                </itemtemplate>
                       
            </columns>

C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "cmdBind")
    {
        LinkButton lb = (LinkButton)e.CommandSource;
        int index = Convert.ToInt32(lb.CommandArgument);

        //Bind values in the text box of the pop up control
        TextBox3.Text    = GridView1.Rows[index].Cells[3].Text.ToString();
        TextBox4.Text    = GridView1.Rows[index].Cells[3].Text.ToString();
        TextBox7.Text = GridView1.Rows[index].Cells[4].Text;
        Label7.Text = GridView1.Rows[index].Cells[1].Text;
        Label9.Text=GridView1.Rows[index].Cells[1].Text;
        TextBox8.Text = GridView1.Rows[index].Cells[6].Text;
        TextBox6.Text = GridView1.Rows[index].Cells[2].Text;
        TextBox5.Text = GridView1.Rows[index].Cells[6].Text;
    }
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    //GridData();
}
 
Share this answer
 
v2
C#
label.Text=this.datagridview.CurrentRow.Cells[0].Value.ToString();

0 - be the cell element you calling
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900