How to Insert, Update, Delete From Grid View in Visual Studio 2013

Before creating this application, you need to create a database and table for creating connection.

Introduction

Today I am describing how to insert, delete and update values in the database from a Grid View.

Binding Database to Grid View

First you should bind all records (depending on you how many records you want to show) from the database into the Grid View. The following is the example of binding the records:

<asp:TemplateField>

     <HeaderTemplate>ID</HeaderTemplate>

     <ItemTemplate><asp:Label ID="LblStuID" runat="server" Text='<%# Bind("Stu_ID")%>'></asp:Label></ItemTemplate>

</asp:TemplateField>

 

<asp:TemplateField>

     <HeaderTemplate>Name</HeaderTemplate>

     <ItemTemplate><asp:Label ID="LblStuName" runat="server" Text='<%# Bind("Stu_Name") %>'></asp:Label></ItemTemplate>

          <EditItemTemplate><asp:TextBox ID="TxtEditStuName" runat="server"></asp:TextBox></EditItemTemplate>

     <FooterTemplate><asp:TextBox ID="TxtStuName" runat="server"></asp:TextBox></FooterTemplate>

</asp:TemplateField>

 

<asp:TemplateField>

    <HeaderTemplate>Gender</HeaderTemplate>

    <ItemTemplate><asp:Label ID="LblStuGender" runat="server" Text='<%# Bind("Stu_Gender")%>'></asp:Label></ItemTemplate>

          <EditItemTemplate>

              <asp:DropDownList ID="DDLEditStuGender" runat="server">

                    <asp:ListItem>--Select--</asp:ListItem>

                    <asp:ListItem>Male</asp:ListItem>

                    <asp:ListItem>Female</asp:ListItem>

              </asp:DropDownList>

          </EditItemTemplate>

    <FooterTemplate>

        <asp:DropDownList ID="DDLStuGender" runat="server">

            <asp:ListItem>--Select--</asp:ListItem>

            <asp:ListItem>Male</asp:ListItem>

            <asp:ListItem>Female</asp:ListItem>

        </asp:DropDownList>

    </FooterTemplate>

</asp:TemplateField>

 

<asp:TemplateField>

    <HeaderTemplate>Graduation</HeaderTemplate>

    <ItemTemplate><asp:Label ID="LblStuGraduation" runat="server" Text='<%# Bind("Stu_Graduation")%>'></asp:Label></ItemTemplate>

         <EditItemTemplate><asp:TextBox ID="TxtEditStuGraduation" runat="server"></asp:TextBox></EditItemTemplate>

    <FooterTemplate><asp:TextBox ID="TxtStuGraduation" runat="server"></asp:TextBox></FooterTemplate>

</asp:TemplateField>

 

<asp:TemplateField>

    <HeaderTemplate>Post Graduation</HeaderTemplate>

    <ItemTemplate><asp:Label ID="LblStuPostGraduation" runat="server" Text='<%# Bind("Stu_PostGraduation")%>'></asp:Label></ItemTemplate>

        <EditItemTemplate><asp:TextBox ID="TxtEditStuPostGraduation" runat="server"></asp:TextBox></EditItemTemplate>

    <FooterTemplate><asp:TextBox ID="TxtStuPostGraduation" runat="server"></asp:TextBox></FooterTemplate>

</asp:TemplateField>

 

<asp:TemplateField>

     <HeaderTemplate>Mobile No</HeaderTemplate>

     <ItemTemplate><asp:Label ID="LblStuMobileNo" runat="server" Text='<%# Bind("Stu_MobileNo")%>'></asp:Label></ItemTemplate>

         <EditItemTemplate><asp:TextBox ID="TxtEditStuMobileNo" runat="server"></asp:TextBox></EditItemTemplate>

     <FooterTemplate><asp:TextBox ID="TxtStuMobileNo" runat="server"></asp:TextBox></FooterTemplate>

</asp:TemplateField>

 

<asp:TemplateField>

    <HeaderTemplate>Email ID</HeaderTemplate>

    <ItemTemplate><asp:Label ID="LblStuEmailID" runat="server" Text='<%# Bind("Stu_EmailID")%>'></asp:Label></ItemTemplate>

        <EditItemTemplate><asp:TextBox ID="TxtEditStuEmailID" runat="server"></asp:TextBox></EditItemTemplate>

    <FooterTemplate><asp:TextBox ID="TxtStuEmailID" runat="server"></asp:TextBox></FooterTemplate>

</asp:TemplateField>

Getting Value from Database in Grid View

For getting all records (depending on the Source View) you need to retrieve records from the database.

protected void Page_Load(object sender, EventArgs e)

{

    if (!IsPostBack)

    {

        Fill_Grid();

    }

}

Fill_Grid definition is:

void Fill_Grid()

{

    DataGridView.DataSource = obj.Get_Data();

    DataGridView.DataBind();

}

Get_Data definition is:

public DataTable Get_Data()

{

    adap = new SqlDataAdapter("select * from Tbl_Student",con);

    dt = new DataTable();

    adap.Fill(dt);

    return dt;

}

Insert Value from Grid View

Now, after binding the records, to insert a record you need to create an Insert Event in the code file.

Syntax for creating an event:

protected void Page_Load(object sender, EventArgs e)

{

    Button BtnInsert = new Button();

    BtnInsert.Click += BtnInsert_Click;

}

Now, in the Insert Event you need to take all values from the front end and proceed to insert into the database.

protected void BtnInsert_Click(object sender, EventArgs e)

{

    InsertValues();

}

void InsertValues()

{

    TextBox Name = DataGridView.FooterRow.FindControl("TxtStuName") as TextBox;

    DropDownList Gender = DataGridView.FooterRow.FindControl("DDLStuGender") as DropDownList;

    TextBox Grad = DataGridView.FooterRow.FindControl("TxtStuGraduation") as TextBox;

    TextBox PostGrad = DataGridView.FooterRow.FindControl("TxtStuPostGraduation") as TextBox;

    TextBox Mobile = DataGridView.FooterRow.FindControl("TxtStuMobileNo") as TextBox;

    TextBox Email = DataGridView.FooterRow.FindControl("TxtStuEmailID") as TextBox;

    string Message = obj.Insert_Data(Name, Gender.SelectedItem, Grad, PostGrad, Mobile, Email);

    Fill_Grid();

    Literal1.Text = Message;

}

The Insert_Data definition is:

public string Insert_Data(TextBox Name, ListItem listItem, TextBox Grad, TextBox PostGrad, TextBox Mobile, TextBox Email)

{

    cmd = new SqlCommand("Insert into Tbl_Student values('" + Name.Text + "','" + listItem.Text + "','" + Grad.Text + "','" + PostGrad.Text + "'," + Mobile.Text + ",'" + Email.Text + "')", con);

    con.Open();

    int temp = cmd.ExecuteNonQuery();

    con.Close();

    return "Inserted Sucessfully";

}

 

Update Value from Grid View

If you want to update the value then write the following code.

At first, you need to create the OnRowUpdating Event from the event window of the GridView:

protected void DataGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

    Label ID =             DataGridView.Rows[e.RowIndex].FindControl("LblStuID") as Label;

    TextBox Name =         DataGridView.Rows[e.RowIndex].FindControl("TxtEditStuName") as TextBox;

    DropDownList Gender =  DataGridView.Rows[e.RowIndex].FindControl("DDLEditStuGender") as DropDownList;

    TextBox Grad =         DataGridView.Rows[e.RowIndex].FindControl("TxtEditStuGraduation") as TextBox;

    TextBox PostGrad =     DataGridView.Rows[e.RowIndex].FindControl("TxtEditStuPostGraduation") as TextBox;

    TextBox Mobile =       DataGridView.Rows[e.RowIndex].FindControl("TxtEditStuMobileNo") as TextBox;

    TextBox Email =        DataGridView.Rows[e.RowIndex].FindControl("TxtEditStuEmailID") as TextBox;

 

    string Message = obj.Updata_Data(ID, Name, Gender.SelectedItem, Grad, PostGrad, Mobile, Email);

    Fill_Grid();

    Literal1.Text = Message;

}

 

Update_Data definition is:

public string Updata_Data(Label ID, TextBox Name, ListItem listItem, TextBox Grad, TextBox PostGrad, TextBox Mobile, TextBox Email)   

{

    cmd = new SqlCommand("Update Tbl_Student set Stu_Name='" + Name.Text + "', Stu_Gender='" + listItem.Text + "', Stu_Graduation='" + Grad.Text + "', Stu_PostGraduation='" + PostGrad.Text + "', Stu_MobileNo=" + Mobile.Text + ", Stu_EmailID='"+Email.Text+"' where Stu_ID=" +       ID.Text + "", con);

    con.Open();

    cmd.ExecuteNonQuery();

    con.Close();

    return "Updated Sucessfully";

}

Delete Value from Grid View

If you want to delete the value then write the following code.

At first, you need to create the OnRowDeleting Event from the event window of the GridView:

protected void DataGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

    Label Stu_ID = (Label)DataGridView.Rows[e.RowIndex].FindControl("LblStuID");

    int id = Convert.ToInt32(Stu_ID.Text);

    string Message = obj.Delete_Data(id);

    Fill_Grid();

    Literal1.Text = Message;

}

The Delete_Data definition is:

public string Delete_Data(int id)

{

    con.Open();

    cmd = new SqlCommand("Delete from Tbl_Student where Stu_ID=" + id + "", con);

    cmd.ExecuteNonQuery();

    con.Close();

    return "Deleted Sucessfully";

}

 


Similar Articles