C# .NET - button click event in datagridviewbutton column

Asked By vigneshvari - on 03-May-09 01:13 AM

can any on help me to generate a button event when datagridview button column is clicked.when i click the button , the form should be navigated to another form taking an id value(student id or employee id)

thanks

vigneshvari

reply

Perry replied to vigneshvari - on 03-May-09 04:11 AM
Please do not post multiple question on same question. I think, San222 has given exact answer what you are looking for at
http://www.eggheadcafe.com/community/aspnet/2/10093214/button-click-event-in-dat.aspx

Regards.

here code

Ravenet Rasaiyah replied to vigneshvari - on 03-May-09 05:04 AM
Hi

here complete code

HTML Page

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
            DataSourceID="SqlDataSource1">
            <Columns>             
                <asp:TemplateField HeaderText="Show Details">
                    <ItemTemplate>
                        <asp:Button ID="btnDetails" runat="server" OnClick="Details_Click" Text="Detail" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
      


code behind Code:
protected void Details_Click(object sender, System.EventArgs e)
{
//
GridViewRow gridRow = null;
string strID = null;
string strName = null;
//
gridRow = ((Button)sender).Parent.Parent;
strID = gridRow.Cells(0).Text;
strName = gridRow.Cells(1).Text;


}

thank you

suggestion and question

Perry replied to Ravenet Rasaiyah on 03-May-09 05:17 AM
I am wondering, why you always reply ASP.net with C# whenever someone asked question on C#? Please read the question and tell me is there anything that lead you to write ASP code. If user don't know anything about ASP.net what will happen?

Regards.
ohh
Ravenet Rasaiyah replied to Perry on 03-May-09 06:26 AM
Hi Paresh Rajput,


I think this is first i made mistake, so sorry for that.

thanks for ur reply
vigneshvari - replied to Perry on 03-May-09 08:44 AM
end of post
thank for your reply
vigneshvari - replied to Ravenet Rasaiyah on 03-May-09 08:50 AM
end of post
Vasanthakumar D replied to vigneshvari - on 04-May-09 01:10 AM

Hi,

use the below code...


private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == Button column index)
            {
                Form2 from = new Form2();
                from.strDet = dataGridView1.Rows[e.RowIndex].Cells[Id column Index].Value.ToString();
                from.Show();
            }
        }
This answer already given at http://www.eggheadcafe.com/community/aspnet/2/10093214/button-click-event-in-dat.aspx
Perry replied to Vasanthakumar D on 04-May-09 02:24 AM
end of post
thanks... I didnot see that post....
Vasanthakumar D replied to Perry on 04-May-09 02:28 AM
end of post
Re :: Button click event in datagrid button column
Shailendrasinh Parmar replied to vigneshvari - on 04-May-09 04:04 AM

For that you have to use OnItemCommand event of the datagrid. See the following DataGrid declaration with Template Column and Id.

<asp:DataGrid ID="DataGrid1" runat="server" OnItemCommand="DataGrid1_ItemCommand">

<Columns>

<asp:BoundColumn DataField="ID" Visible="false"></asp:BoundColumn>

<asp:TemplateColumn ItemStyle-Width="20px" ItemStyle-HorizontalAlign="center">

<ItemTemplate>

<asp:Button ID="btnEdit" runat="server" CommandName="Edit" />

</ItemTemplate>

</asp:TemplateColumn>

</Columns>

</asp:DataGrid>


And on clicking the Edit button you will have the following code in ItemCommand Event

protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)

{

if (e.CommandName.ToString() == "Edit")

{

Response.Redirect("YourPageName.aspx?id=" + e.Item.Cells[0].Text);

}

}


Hope this helps.

Thank u
vigneshvari - replied to Vasanthakumar D on 04-May-09 10:11 AM

hi

thanks for help


vigneshvari