Home / ASP.NET Wiki / ASP.NET Life Cycle Overview / Data-centric UI Controls / GridView / Add, Edit, update, Delete gridview

Add, Edit, update, Delete gridview

 Rate It (0)

Thousandas operation we can do on Gridview control.

Here I as explaining how we can add new recored and edit of existing record and delete a record in Gridview.

Step1 : You can add this gridview in your aspx form.

Here some events of Gridview are fired:-

i)  OnRowCancelingEdit

ii)  OnRowDeleting

iii) OnRowEditing

iv) OnRowUpdating

Add this gridview in .aspx page

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"

DataKeyNames="cat_id" ForeColor="#333333" GridLines="None" OnRowCancelingEdit="GridView1_RowCancelingEdit"

OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">

<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

<Columns>

<asp:CommandField HeaderText="Edit-Update" ShowEditButton="True" />

<asp:BoundField DataField="cat_id" HeaderText="Category ID" ReadOnly="True" />

<asp:BoundField DataField="cat_name" HeaderText="Category Name" />

<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />

</Columns>

<RowStyle BackColor="#E3EAEB" />

<EditRowStyle BackColor="#7C6F57" />

<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />

<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />

<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

</asp:GridView>

for binding gridveiw you have to write following code:

private void BindData()

{

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["exam_moduleConnectionString"].ConnectionString);

SqlDataAdapter da = new SqlDataAdapter("SELECT cat_id, cat_name FROM quest_categories", con);

DataTable dt = new DataTable();

da.Fill(dt);

GridView1.DataSource = dt;

GridView1.DataBind();

}

 Step 2:

Write this procedure to add a new row.

as you click on button it will appear on top of gridview and you can add new record in database thru gridview.

protected void btnAdd_Click(object sender, EventArgs e)

{

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["exam_moduleConnectionString"].ConnectionString);

SqlDataAdapter da = new SqlDataAdapter("SELECT cat_id, cat_name FROM quest_categories", con);

DataTable dt = new DataTable();

da.Fill(dt);

// Here we'll add a blank row to the returned DataTable

DataRow dr = dt.NewRow();

dt.Rows.InsertAt(dr, 0);

//Creating the first row of GridView to be Editable

GridView1.EditIndex = 0;

GridView1.DataSource = dt;

GridView1.DataBind();

//Changing the Text for Inserting a New Record

((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert";

}

Step 3:

Suppose you change your mind and canel record to addthen you have to write following code:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

{

GridView1.EditIndex = e.NewEditIndex;

BindData();

}

 

Step 4:

if you are editing a row you want to some change in existing record then you have to write following code:

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

{

GridView1.EditIndex = -1;

BindData();

}

Step: 5

You wanna to add new record or you wanna add a new record then u can follow :

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

if (((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text == "Insert")

{

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["exam_moduleConnectionString"].ConnectionString);

SqlCommand cmd = new SqlCommand();

cmd.CommandText = "INSERT INTO quest_categories(cat_name) VALUES(@cat_name)";cmd.Parameters.Add("@cat_name", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[0].Cells[2].Controls[0]).Text;

cmd.Connection = con;

con.Open();

cmd.ExecuteNonQuery();

con.Close();

}

else

{

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["exam_moduleConnectionString"].ConnectionString);

SqlCommand cmd = new SqlCommand();

cmd.CommandText = "UPDATE quest_categories SET cat_name=@cat_name WHERE cat_id=@cat_id";

cmd.Parameters.Add("@cat_name", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;

cmd.Parameters.Add("@cat_id", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);

cmd.Connection = con;

con.Open();

cmd.ExecuteNonQuery();

con.Close();

 

}

 

GridView1.EditIndex = -1;

BindData();

}

Step 6:

If you wnat to delete particulare record from database in gridview then u can follow this:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["exam_moduleConnectionString"].ConnectionString);

SqlCommand cmd = new SqlCommand();

cmd.CommandText = "DELETE FROM quest_categories WHERE cat_id=@cat_id";cmd.Parameters.Add("@cat_id", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);

cmd.Connection = con;

con.Open();

cmd.ExecuteNonQuery();

con.Close();

BindData();

}

 

Revision number 1, Friday, July 09, 2010 8:41:18 PM by anjankant
This is not the most up to date version of this article. The most recent version can be found here.

Comments

I have created a web application in vs2008 with framework 2.o and use validation controls but i am not able to validate drop-down listbox havin male female option I want user could not leave it without select proper gender if so massage should be please select gender.

Can I update this wiki?

@rabharga yes any one can update the wiki but it will be moderated

Rather then using ConfigurationManager and connectionString in code everytime because everytime connection close and open used. Can we use dataadapters? If yes then how? Give some example please.

Hi, Is there any way to do copy paste from gridview?

Doin this using wizard is a 2 mins job.. but will that decrease the performance of website?

Hi I just want to know, whether there is any way to copy the whole row(with all column values) and paste into next row in a grid view. Regards, Charan

Related Articles

Migrating ASP.NET Applications to IIS 7 Integrated Mode

Introduction There had been much discussion on how to make your application compatible with the IIS7 Integrated mode. To know how to make your application compatible with IIS7 integrated mode, you first need to understand what the integrated mode is and why

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. abiruban (1)