Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
hello friends i want to edit,update,delete my gridview in asp.net will u plz help me.
Posted 22-Mar-12 3:56am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi ,
 
Look at these ,
 
http://msdn.microsoft.com/en-us/library/dd548005.aspx[^]
 
Editable GridView in ASP.NET 2.0[^]
 
Hope These May be Useful..
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Before doing the SQL transaction form the gridview, you need to know some of the events presented in the data grid view.
Rest of the queries are same as you do in sql.
And dont forget to refresh the page or call the function to reload the data in grid view.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

see this link
 
CRUD Operations in GridView[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
 
    }
 
 
private void BindData()
    {
        string constr = @"Data Source=SQLEXPRESS2008;Initial Catalog=database_name;Integrated Security=True";
        string query = "SELECT 1st_column,2nd_column,3rd_column FROM Table_Name";
        SqlDataAdapter da = new SqlDataAdapter(query, constr);
        DataTable table = new DataTable();
        da.Fill(table);
        GridView1.DataSource = table;
        GridView1.DataBind();
 
    }
 
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }
 
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        e.Cancel = true;
        GridView1.EditIndex = -1;
        BindData();
    }
 
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        
        TextBox txt1st_column = (TextBox)row.FindControl("txt1st_column");	
        TextBox txt2nd_column = (TextBox)row.FindControl("txt2nd_column");
        TextBox txt3rd_column = (TextBox)row.FindControl("txt3rd_column");   
 
        int a = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string b = txt2nd_column.Text;
        string c = txt3rd_column.Text;
 
        UpdateProduct(a, b,c);
 
    }
 
private void UpdateProduct(int a, string b,string c)
    {
        try
        {
            string constr = @"Data Source=SQLEXPRESS2008;Initial Catalog=Database;Integrated Security=True";
            string query = "UPDATE Table_Name SET 2nd_column = @b, 3rd_column = @c WHERE 1st_column = @a";
 
 
            SqlConnection con = new SqlConnection(constr);
            SqlCommand com = new SqlCommand(query, con);
 
            com.Parameters.Add("@a", SqlDbType.Int).Value = 1st_column;
            com.Parameters.Add("@b", SqlDbType.NVarChar).Value = 2nd_column;
            com.Parameters.Add("@c", SqlDbType.NVarChar).Value = 3rd_column;
 
 
            con.Open();
            com.ExecuteNonQuery();
            con.Close();
 
 
            GridView1.EditIndex = -1;
            BindData();
        }
        catch (Exception ex)
        {
            throw ex;
    }
 
protected void btn_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
	try
	{
	string constr = @"Data Source=SQLEXPRESS2008;Initial Catalog=Database;Integrated Security=True";
	string query = "DELETE FROM Table_Name WHERE 1st_column=@a";
 
	SqlConnection con = new SqlConnection(constr);
	SqlCommand cmd = new SqlCommand();
 
	cmd.Parameters.Add("@a", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text)";
	cmd.Connection = con;
 
	con.Open();
	cmd.ExecuteNonQuery();
 
	con.Close();
	BindData();
	}
	catch(Exception ex)
	{
	    throw ex;
	}
  }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
 

GridViewRow row = GridView1.Rows[e.RowIndex];
string id = (((Label)row.FindControl("Name1")).Text).ToString();
string s = (((TextBox)row.FindControl("Pwd1")).Text).ToString();
string sr = (((TextBox)row.FindControl("Email1")).Text).ToString();
//string sr = ((Label)row.FindControl("Email")).Text;
sq.Open();
SqlCommand CmdSql = new SqlCommand("Update RegForm set pwd='" + s + "', Email='" + sr + "' where Name='" + id + "' ", sq);
CmdSql.ExecuteNonQuery();
GridView1.EditIndex = -1;
sq.Close();
read();
 

 
}
 
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
string id = (((Label)row.FindControl("Name")).Text).ToString();
sq.Open();
SqlCommand CmdSqls = new SqlCommand("Delete from RegForm where Name='" + id + "' ", sq);
CmdSqls.ExecuteNonQuery();
GridView1.EditIndex = -1;
sq.Close();
read();
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 12 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100