Introduction:
In this article I will show you
how you can use the ObjectDataSource with the GridView control to do
editing, updating, deleting and adding new records. There are several ways to
perform these operations, I am using the simplest approach. The project files
are also attached with this article so please feel free to download them.
| This application was
implemented in Visual Studio.NET 2005 Professional. |
Creating the User Interface:
Let's first see the user interface
that we are going to be using in this article.

As you can see in the image above our GridView
contains five columns. UserID, FirstName and LastName are template columns and
Edit and Delete columns are Command Columns which are added using smart tag
option (You can view the smart tag if you right click on the GridView control).
In the footer I have added TextBoxes for the
FirstName and LastName as I will be inserting new records. The Add button simply
adds the new records to the database.
Below is the complete HTML code of the GridView
control:
|
<asp:GridView
ID="GridView1"
runat="server"
AutoGenerateColumns="False"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing"
DataKeyNames="ID"
OnRowUpdating="GridView1_RowUpdating"
OnRowUpdated="GridView1_RowUpdated"
OnRowDeleting="GridView1_RowDeleting"
ShowFooter="True"
OnRowCommand="GridView1_RowCommand"
DataSourceID="objUser">
<Columns>
<asp:TemplateField
HeaderText="UserID">
<ItemTemplate>
<asp:Label
ID="lblUserID"
runat="server"
Text='<%#
Bind("ID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Button
ID="Btn_Add"
runat="server"
CommandName="AddUser"
Text="Add"
/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField
HeaderText="FirstName">
<ItemTemplate>
<asp:Label
ID="lblFirstName"
Text='<%#
Eval("FirstName") %>'
runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox
ID="txtFirstName"
Text='<%#
Eval("FirstName") %>'
|