Edit and Update Record in GridView Without C# Code

Introduction

This article explains how to edit and update records using a GridView without writing a single line of code behind C# code.

Step 1

First of all create a table using the following scripts in SQL Server:

  1. CREATE TABLE [dbo].[tblStudents](  
  2. [Id] [int] IDENTITY(1,1) NOT NULL,  
  3. [StudentName] [varchar](50) NOT NULL,  
  4. [RollNo] [varchar](20) NOT NULL,  
  5. [Add] [varchar](50) NULL,  
  6. [MobileNo] [varchar](10) NOT NULL,  
  7. CONSTRAINT [PK_tblStudents] PRIMARY KEY CLUSTERED   
  8. (  
  9. [Id] ASC  
  10. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,   
  11. IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,  
  12. ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  13. ON [PRIMARY]  
Step 2

Now open Visual Studio and create a new project and provide the name, whatever you want. Add a page to your project and drag and drop a GridView.

webform

Step 3

Click on the new data source and:

database

Step 4

Select the proper database and click on ok.

Select the database

Step 5

Enter proper information and click on ok then another window will open like:

save connection

Step 6

Click on "Next" and:

advanced SQL generation option

Step 7

Select a table name and click on the advanced button, then check those two checkboxes and click on ok and go to next.

test query

Now test the query and click on the Finish button.

Step 8

Now check all the checkboxes that you require.

datagridview

 

  • For Edit operation check Enable Editing.
  • Delete operation check Enable Deleting.
  • Sorting enable shorting and so on.

Now if you go to the source it will look like:

GridView =>

  1. <asp:GridView ID="GridView1" runat="server" AllowPaging="True"   
  2.             AllowSorting="True" AutoGenerateColumns="False" BackColor="White"   
  3.             BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4"   
  4.             DataKeyNames="Id" DataSourceID="SqlDataSource1" GridLines="Horizontal">  
  5.             <Columns>  
  6.                 <asp:CommandField ShowDeleteButton="True" ShowEditButton="True"   
  7.                     ShowSelectButton="True" HeaderText="Action" />  
  8.                 <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False"   
  9.                     ReadOnly="True" SortExpression="Id" />  
  10.                 <asp:BoundField DataField="StudentName" HeaderText="StudentName"   
  11.                     SortExpression="StudentName" />  
  12.                 <asp:BoundField DataField="RollNo" HeaderText="RollNo"   
  13.                     SortExpression="RollNo" />  
  14.                 <asp:BoundField DataField="Add" HeaderText="Add" SortExpression="Add" />  
  15.                 <asp:BoundField DataField="MobileNo" HeaderText="MobileNo"   
  16.                     SortExpression="MobileNo" />  
  17.             </Columns>  
  18.             <FooterStyle BackColor="White" ForeColor="#333333" />  
  19.             <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />  
  20.             <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />  
  21.             <RowStyle BackColor="White" ForeColor="#333333" />  
  22.             <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />  
  23.             <SortedAscendingCellStyle BackColor="#F7F7F7" />  
  24.             <SortedAscendingHeaderStyle BackColor="#487575" />  
  25.             <SortedDescendingCellStyle BackColor="#E5E5E5" />  
  26.             <SortedDescendingHeaderStyle BackColor="#275353" />  
  27.         </asp:GridView>  
And SqlDataSource =>
  1. <asp:SqlDataSource ID="SqlDataSource1" runat="server"   
  2.             ConflictDetection="CompareAllValues"   
  3.             ConnectionString="<%$ ConnectionStrings:manishDBConnectionString2 %>"   
  4.             DeleteCommand="DELETE FROM [tblStudents] WHERE [Id] = @original_Id AND [StudentName] = @original_StudentName AND [RollNo] = @original_RollNo AND (([Add] = @original_Add) OR ([Add] IS NULL AND @original_Add IS NULL)) AND [MobileNo] = @original_MobileNo"   
  5.             InsertCommand="INSERT INTO [tblStudents] ([StudentName], [RollNo], [Add], [MobileNo]) VALUES (@StudentName, @RollNo, @Add, @MobileNo)"   
  6.             OldValuesParameterFormatString="original_{0}"   
  7.             SelectCommand="SELECT * FROM [tblStudents]"   
  8.             UpdateCommand="UPDATE [tblStudents] SET [StudentName] = @StudentName, [RollNo] = @RollNo, [Add] = @Add, [MobileNo] = @MobileNo WHERE [Id] = @original_Id AND [StudentName] = @original_StudentName AND [RollNo] = @original_RollNo AND (([Add] = @original_Add) OR ([Add] IS NULL AND @original_Add IS NULL)) AND [MobileNo] = @original_MobileNo">  
  9.             <DeleteParameters>  
  10.                 <asp:Parameter Name="original_Id" Type="Int32" />  
  11.                 <asp:Parameter Name="original_StudentName" Type="String" />  
  12.                 <asp:Parameter Name="original_RollNo" Type="String" />  
  13.                 <asp:Parameter Name="original_Add" Type="String" />  
  14.                 <asp:Parameter Name="original_MobileNo" Type="String" />  
  15.             </DeleteParameters>  
  16.             <InsertParameters>  
  17.                 <asp:Parameter Name="StudentName" Type="String" />  
  18.                 <asp:Parameter Name="RollNo" Type="String" />  
  19.                 <asp:Parameter Name="Add" Type="String" />  
  20.                 <asp:Parameter Name="MobileNo" Type="String" />  
  21.             </InsertParameters>  
  22.             <UpdateParameters>  
  23.                 <asp:Parameter Name="StudentName" Type="String" />  
  24.                 <asp:Parameter Name="RollNo" Type="String" />  
  25.                 <asp:Parameter Name="Add" Type="String" />  
  26.                 <asp:Parameter Name="MobileNo" Type="String" />  
  27.                 <asp:Parameter Name="original_Id" Type="Int32" />  
  28.                 <asp:Parameter Name="original_StudentName" Type="String" />  
  29.                 <asp:Parameter Name="original_RollNo" Type="String" />  
  30.                 <asp:Parameter Name="original_Add" Type="String" />  
  31.                 <asp:Parameter Name="original_MobileNo" Type="String" />  
  32.             </UpdateParameters>  
  33.         </asp:SqlDataSource>  
You can change the data source's select, update and delete query as your need.

Now run the page using Ctrl+F5; it will look like:

datagridview option

Now click on the edit or delete button and modify the data.

show detail

Here you can see I have not written a single line of cs code in code behind.

Summary

In this illustration we learned how to update and delete database records using a GridView without C# code. Please provide your valuable comments about this article.

 


Similar Articles