This example demonstrates using a .NET GridView with row commands for adding, updating, and deleting entries.
Here is the code snippet showcasing a SQL database integration:
<asp:GridView ID="gv" RunAt="Server" DataSourceID="sqlGrid" DataKeyNames="RowID" AllowPaging="False" AutoGenerateColumns="false" EnableModelValidation="True" AutoGenerateEditButton="False" AutoGenerateDeleteButton="False" GridLines="None" BorderWidth="0">
<Columns>
<asp:BoundField HeaderText="Col1" DataField="Col1" SortExpression="Col1"/>
<asp:BoundField runat="Server" HeaderText="Col2" DataField="Col2" SortExpression="Col2"/>
<asp:CommandField HeaderText="Edit" ShowEditButton="True"/>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True"/>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sqlGrid" RunAt="Server" SelectCommand="spGrid" SelectCommandType="StoredProcedure" UpdateCommand="spGridUpdate" UpdateCommandType="StoredProcedure" DeleteCommand="spGridDelete" DeleteCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="Col1" Type="String" />
<asp:Parameter Name="Col2" Type="String" />
<asp:Parameter Name="RowID" Type="Int32" DefaultValue="0" />
</UpdateParameters>
</asp:SqlDataSource>
To allow direct input in the grid, utilize TemplateFields to insert text boxes within cells. Saving updates the cell contents in the database.
Add Empty Row
<asp:TemplateField HeaderText="Col1" SortExpression="Col1">
<ItemTemplate>
<asp:TexBox ID="txt1" runat="server"></asp:TexBox >
<asp:TexBox ID="txt2" runat="server"></asp:TexBox >
</ItemTemplate>
</asp:TemplateField>
IMPLEMENTATION STEPS
- Set up database, tables, and columns.
- Create stored procedures for database CRUD operations.
- Construct GridView with template fields and textboxes.
- Fetch data into the GridView from stored procedures.
- Utilize sqlDataSource and row commands for updating gridview data.