• 0

VB.NET and datagrid control


Question

Hi guys,

i hope you re all doing well.

I want to use the datagrid to enter data into a database. I want to make an empty row so that the user can enter data into the columns and on a button click that data is added into the corresponding columns of the table.

So basically i want to add empty rows in a data grid control on the form load event.

im not an expert coder so a code sample would help a lot

thanks...

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

I just did this about 30 minutes ago :D

Here's what you do: on the ASPX page:

<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" ShowFooter="True" Runat="Server">
   <columns>
      <asp:TemplateColumn>
         <ItemTemplate>
            <asp:Label EnableViewState="False" Text='<%# DataBinder.Eval(Container.DataItem, "Column1") %>' Runat="Server" />
         </ItemTemplate.
         <EditItemTemplate>
            <asp:TextBox Text='<%# DataBinder.Eval(Container.DataItem, "Column1") %>' Runat="server" />
         </EditItemTemplate>
         <FooterTemplate>
            <asp:TextBox runat="Server" />
         </FooterTemplate>
   </asp:TemplateColumn>
   <asp:TemplateColumn>
      <ItemTemplate>
         <asp:Label EnableViewState="False" Text='<%# DataBinder.Eval(Container.DataItem, "Column2") %>' Runat="Server" />
      </ItemTemplate>
      <EditItemTemplate>
         <asp:TextBox Text='<%# DataBinder.Eval(Container.DataItem, "Column2") %>' Runat="server" />
      </EditItemTemplate>
      <FooterTemplate>
         <asp:TextBox runat="Server" />
      </FooterTemplate>
   </asp:TemplateColumn>
   <asp:templatecolumn>
      <itemtemplate>
         <asp:linkbutton commandname="Edit" enableviewstate="False" text="Edit" runat="server" />
         <asp:linkbutton commandname="Delete" enableviewstate="False" text="Delete" runat="server" />
      </itemtemplate>
      <footertemplate>
         <asp:linkbutton commandname="Insert" enableviewstate="False" text="Insert" runat="server" />
      </footertemplate>
      <edititemtemplate>
         <asp:linkbutton commandname="Update" enableviewstate="False" text="Update" runat="server" />
         <asp:linkbutton commandname="Cancel" enableviewstate="False" text="Cancel" runat="server" />
      </edititemtemplate>
   </asp:templatecolumn>
</asp:DataGrid>

And then on your code behind:

Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
   If e.CommandName = "Insert" Then
      ' Do Code Here
   End If
End Sub

If you have <asp:BoundColumns> instead of <asp:TemplateColumns>, select the DataGrid, select Columns from the properties pane, and then for each column you have, click the "Convert to Template Column" link at the bottom. It'll do all but the <FooterTemplate> code for you (which is the same as the EditTemplate, just without the binding :p).

Note: I added EnableViewState="False" for all the ItemTemplate, because it greatly reduces your ViewState.

Good luck. Post back if you need more help!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.