Click here to Skip to main content
15,911,306 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my web page I had Gridview. I am bind datas at runtime using databind() method.
Then I have delete and edit buttons on the grid. When i click the buttons the row is deleted or edited. how to do it

I am using following coding for load datas
Bindgrid()
 connect.cnopen();
        SqlDataAdapter da = new SqlDataAdapter("select ITEM,QTY,RATE,Amount FROM TempSales where Billno='" + TXTBILLNO.Text + "'", connect.cn());
        DataSet ds=new DataSet();
        da.Fill(ds);
        GridView1.DataSource=ds;
        GridView1.DataBind();

I give the following coding for delete Row but it is not working
C#
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
   {
       Label Control = (Label)GridView1.Rows[e.RowIndex].FindControl("ITEM");
        connect.cnopen();
       SqlCommand cmd = new SqlCommand("delete from Tempsales where Item='" + Control.Text + "'", connect.cn());
       Bindgrid();
   
   }
Posted
Comments
Mohd Imran Saifi 20-Mar-12 8:56am    
what error dou you get
Mohd Imran Saifi 20-Mar-12 8:57am    
fetch row index at selected event of gridview.
and delete data according to that row index

1 solution

C#
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
   {
       Label Control = (Label)GridView1.Rows[e.RowIndex].FindControl("ITEM");
        connect.cnopen();
       SqlCommand cmd = new SqlCommand("delete from Tempsales where Item='" + Control.Text + "'", connect.cn());
   cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
            GridView1.dataSource = null;

       Bindgrid();
   
   }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900