Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a column with buttons in to a gridview in asp.net?

Tags:

As my gridview is populating I want to add an extra column with some buttons in but I can't seem to figure out how, or what might be the best way. Can anyone get me started?

like image 498
NibblyPig Avatar asked Sep 28 '09 08:09

NibblyPig


People also ask

How to add Button in GridView column in c#?

Add(dr); dataGridViewSoftware. DataSource = dt; The text appears but button never shows up. What kind of application you are targeting, You don't have to add the button in your DataTable, instead you have to add a column to your gridview with button.

How do I add a dynamic button to a grid view?

Solution 1 You should add dynamic controls in RowCreated[^] event of the GridView. This will create controls at DataBinding and as well as when rebuilding a page on PostBack.

What is Templatefield in asp net?

Template field allows to use controls inside the Gridview. Two main attributes of template field is header template and item template. Header template allows to add column header text and item template allows to add HTML or Asp controls in it.


1 Answers

Use a Template Column

<asp:GridView ID="GridView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSource1"     OnRowCommand="GridView1_OnRowCommand">     <Columns>         <asp:BoundField DataField="name" HeaderText="Name" />         <asp:BoundField DataField="email" HeaderText="Email" />         <asp:TemplateField ShowHeader="False">             <ItemTemplate>                 <asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="SendMail"                     Text="SendMail" CommandArgument='<%# Eval("id") %>' />             </ItemTemplate>         </asp:TemplateField>     </Columns> </asp:GridView> 
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e) {     if (e.CommandName != "SendMail") return;     int id = Convert.ToInt32(e.CommandArgument);     // do something } 
like image 61
Arthur Avatar answered Sep 24 '22 16:09

Arthur