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?
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.
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.
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.
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With