• We just launched and are currently in beta. Join us as we build and grow the community.

Insert, Delete, Update in GridView using ASP.NET

WorthyMugs

Tactical Genius
W Rep
0
0
0
Rep
0
W Vouches
0
0
0
Vouches
0
Posts
137
Likes
91
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
1. As we don't want to use datasource controls. Please delete "ObjectDataSource1" control from the webform.

2. Delete DataSourceID="ObjectDataSource1" from GridView1. This should remove the dependency of GridVIew1 on ObjectDataSource1 control.

3. From the code behind file, delete lbInsert_Click() event handler method.

4. In the "FooterTemplate" of "EmployeeId" TemplateField, please delete OnClick="lbInsert_Click", as we no longer have this event handler method.

5. Delete "CommandField" column from GridView1

6. Now, include a template field in the place of CommandField. This template field is used to display Edit, Update, Cancel and Delete link buttons. We don't want delete and cancel buttons to cause validation, so set CausesValidaion property of these buttons to false.
  1. <asp:TemplateField>
  2. <ItemTemplate>
  3. <asp:LinkButton ID="lbEdit" CommandArgument='<%

    # Eval(

    "EmployeeId"

    )

    %>

    ' CommandName="EditRow" ForeColor="#8C4510" runat="server">Edit</asp:LinkButton>
  4. <asp:LinkButton ID="lbDelete" CommandArgument='<%

    # Eval(

    "EmployeeId"

    )

    %>

    ' CommandName="DeleteRow" ForeColor="#8C4510" runat="server" CausesValidation="false">Delete</asp:LinkButton>
  5. </ItemTemplate>
  6. <EditItemTemplate>
  7. <asp:LinkButton ID="lbUpdate" CommandArgument='<%

    # Eval(

    "EmployeeId"

    )

    %>

    ' CommandName="UpdateRow" ForeColor="#8C4510" runat="server">Update</asp:LinkButton>
  8. <asp:LinkButton ID="lbCancel" CommandArgument='<%

    # Eval(

    "EmployeeId"

    )

    %>

    ' CommandName="CancelUpdate" ForeColor="#8C4510" runat="server" CausesValidation="false">Cancel</asp:LinkButton>
  9. </EditItemTemplate>
  10. </asp:TemplateField>

7. Copy and paste the following private method. This method binds employee data with gridview1 control.
  1. private

    void BindGridViewData(

    )
  2. {
  3. GridView1.DataSource

    =

    EmployeeDataAccessLayer.GetAllEmployees

    (

    )

    ;
  4. GridView1.DataBind

    (

    )

    ;
  5. }

8. Call BindGridViewData() in Page_Load() event.
  1. protected void Page_Load(

    object sender, EventArgs e)
  2. {
  3. if

    (

    !

    IsPostBack)
  4. {
  5. BindGridViewData(

    )

    ;
  6. }
  7. }

9. Finally generate GridView1_RowCommand() event handler method. Copy and Paste the following code.
  1. protected void GridView1_RowCommand(

    object sender, GridViewCommandEventArgs e)
  2. {
  3. if

    (

    e.CommandName

    ==

    "EditRow"

    )
  4. {
  5. int

    rowIndex =

    (

    (

    GridViewRow)

    (

    (

    LinkButton)

    e.CommandSource

    )

    .NamingContainer

    )

    .RowIndex

    ;
  6. GridView1.EditIndex

    =

    rowIndex;
  7. BindGridViewData(

    )

    ;
  8. }
  9. else

    if

    (

    e.CommandName

    ==

    "DeleteRow"

    )
  10. {
  11. EmployeeDataAccessLayer.DeleteEmployee

    (

    Convert.ToInt32

    (

    e.CommandArgument

    )

    )

    ;
  12. BindGridViewData(

    )

    ;
  13. }
  14. else

    if

    (

    e.CommandName

    ==

    "CancelUpdate"

    )
  15. {
  16. GridView1.EditIndex

    =

    -1

    ;
  17. BindGridViewData(

    )

    ;
  18. }
  19. else

    if

    (

    e.CommandName

    ==

    "UpdateRow"

    )
  20. {
  21. int

    rowIndex =

    (

    (

    GridViewRow)

    (

    (

    LinkButton)

    e.CommandSource

    )

    .NamingContainer

    )

    .RowIndex

    ;

  22. int

    employeeId =

    Convert.ToInt32

    (

    e.CommandArgument

    )

    ;
  23. string

    name =

    (

    (

    TextBox)

    GridView1.Rows

    [

    rowIndex]

    .FindControl

    (

    "TextBox1"

    )

    )

    .Text

    ;
  24. string

    gender =

    (

    (

    DropDownList)

    GridView1.Rows

    [

    rowIndex]

    .FindControl

    (

    "DropDownList1"

    )

    )

    .SelectedValue

    ;
  25. string

    city =

    (

    (

    TextBox)

    GridView1.Rows

    [

    rowIndex]

    .FindControl

    (

    "TextBox3"

    )

    )

    .Text

    ;

  26. EmployeeDataAccessLayer.UpdateEmployee

    (

    employeeId, name, gender, city)

    ;

  27. GridView1.EditIndex

    =

    -1

    ;
  28. BindGridViewData(

    )

    ;
  29. }
  30. else

    if

    (

    e.CommandName

    ==

    "InsertRow"

    )
  31. {
  32. string

    name =

    (

    (

    TextBox)

    GridView1.FooterRow

    .FindControl

    (

    "txtName"

    )

    )

    .Text

    ;
  33. string

    gender =

    (

    (

    DropDownList)

    GridView1.FooterRow

    .FindControl

    (

    "ddlGender"

    )

    )

    .SelectedValue

    ;
  34. string

    city =

    (

    (

    TextBox)

    GridView1.FooterRow

    .FindControl

    (

    "txtCity"

    )

    )

    .Text

    ;

  35. EmployeeDataAccessLayer.InsertEmployee

    (

    name, gender, city)

    ;

  36. BindGridViewData(

    )

    ;
  37. }
  38. }

10. In the FooterTemplate of EmployeeId TemplateField, set CommandName property lbInsert link button to "InsertRow"
  1. <

    asp:

    LinkButton ID=

    "lbInsert"

    CommandName=

    "InsertRow"

    ForeColor=

    "#8C4510"

    ValidationGroup=

    "Insert"

    runat=

    "server"

    >

    Insert</

    asp:

    LinkButton>

11. If you want to show a confirmation dialog box, before a row is deleted, include javascript confirm() function, using "OnClientClick" attribute of LinkButton "lbDelete".
  1. <asp:LinkButton ID="lbDelete" CommandArgument='<%

    # Eval(

    "EmployeeId"

    )

    %>

    ' CommandName="DeleteRow" ForeColor="#8C4510" runat="server" CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this row');">Delete</asp:LinkButton>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

 

438,740

315,860

315,869

Top