i have one Gridview (Gridview1) i want to make this Grid editable. For this i have set :

RowEditing = TRUE
RowUpdating = TRUE

After this i have got Edit/Delete button on the extreme Left of the Gridview followed by my Autogenerated columns from the database(Column0 , Column1 ,Column2).

I have written following code in Code Behind :

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Fillgrid();

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
            for(i=0 ; i < GridView1.Rows.Count-1 ; i ++)
         {

        SqlCommand cmd_update = new SqlCommand(" update myproducts set name = '" + GridView1.Rows[i].Cells[2].Text + "' where id='" + GridView1.Rows[i].Cells[1] + "'");

        }
        GridView1.EditIndex = -1;
        Fillgrid();
      }
}

 public void Fillgrid()
    {
        SqlDataReader reader = null;
        SqlCommand command = new SqlCommand("Select * from myproducts",con);
        con.Open();
        reader = command.ExecuteReader(CommandBehavior.CloseConnection);
        GridView1.DataSource = reader;
        GridView1.DataBind();


    }

When i click on the Edit label text , my row becomes editable , however when i click on update...nuthing happens..i mean the values do not get updated.

I even inserted a breakpoint on : protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
to check what happens on the click of Update however i was surprised to c that this event is actually not called n hence is not executed !!

Where am i goin wrong....plz suggest....improvement in the above code shall b appreciated.

cya
Rohan

How did you load the script for the first time.

You must have forgotten to add GridView1_RowUpdating event to the GridView.

I guess there is something wrong with the body of GridView1_RowUpdating method.

When you clik the edit button ,only the row in which you clicked Edit button becomes editable.

Am i right..

So, i guess you should write the code to update only that row.

Answer to my above questions and i will solve the problem quickly.

I am online now and free.

I am waiting.

Hii,
I right click on the Gridview and in the properties i have set the Autogenerated Edit/Delete button to True .

In the smart Tag i have added the Rowupdating event...as u can c in the code i have posted above.....

Yes i am updating only that row which becomes editable.

I have columns like id , name , price ....
tell me one thing.....
My gridview structure is like :
Edit/Delete ID NAME PRICE

So my id column is my cell[0] or cell[1] ?????

u can check my update query also ....

i am not understanding how to proceed....

help plz

cya
Rohan

How did you load the script for the first time.

You must have forgotten to add GridView1_RowUpdating event to the GridView.

I guess there is something wrong with the body of GridView1_RowUpdating method.

When you clik the edit button ,only the row in which you clicked Edit button becomes editable.

Am i right..

So, i guess you should write the code to update only that row.

Answer to my above questions and i will solve the problem quickly.

I am online now and free.

I am waiting.

Whenever you post a thread in a daniweb and you are expecting a reply. Also log on to you msn or gmail, or yahoo messenger so that you know someone has replied to your thread.

This way you can quickly get your problem solved and other free developers can give time to solve your problems.

Well, this is the way i do.

or

As you wish.

am wating for ur reply.......... :)

Whenever you post a thread in a daniweb and you are expecting a reply. Also log on to you msn or gmail, or yahoo messenger so that you know someone has replied to your thread.

This way you can quickly get your problem solved and other free developers can give time to solve your problems.

Well, this is the way i do.

or

As you wish.

Well you able to fire the Gridview1_RowUpdating event.

And you are updating all the rows in the GrieView1_RowUpdating method.
You are using loop to iterate through rows.

I think this is the wrong way.

Wait, i am simulating the same kind of conditon on my computer .
and i will tellyou.

okie...am wating : )
plz give me ur ID gmail or yahoo msg..so dat v can hv ol discussion :)

Well you able to fire the Gridview1_RowUpdating event.

And you are updating all the rows in the GrieView1_RowUpdating method.
You are using loop to iterate through rows.

I think this is the wrong way.

Wait, i am simulating the same kind of conditon on my computer .
and i will tellyou.

Were you able to fire the GridView1_RowUpdating event. If not then Check you GridView defintion in aspx page to associate onrowUpdating attribute of GridView.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" 
            AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" 
            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" >
        </asp:GridView>

My Gmail Id is: randhir.kr.yadav@gamil.com

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.