更新gridview数据 [英] update gridview data

查看:70
本文介绍了更新gridview数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

我正在尝试更新gridview.我也写了代码,问题是当我更新一行时会更新数据库中的所有数据.

这是我的代码供您参考.

Hi Friend,

I am trying to update the gridview.I wrote the code also, the problem is when i update a single row its update all the data in database.

Here is my code for your reference.

protected void addpro_Grid2_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {
       string pid = ((TextBox)addpro_Grid2.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
       string pname = ((TextBox)addpro_Grid2.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
       addpro_Grid_Update(pid, pname);
       addpro_Grid2.EditIndex = -1;
       fill();
   }





protected void fill()
    {
        cmd = new SqlCommand("select * from masters_addproduct", con);
        da = new SqlDataAdapter(cmd);
        dt = new DataTable();
        da.Fill(dt);
        addpro_Grid2.DataSource = dt;
        addpro_Grid2.DataBind();
    }





public void addpro_Grid_Update(string pid, string pname)
    {
        cmd = new SqlCommand("update masters_addproduct set pid='" + pid + "',p_name='" + pname + "'", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }





如何使用db中的Identity列更新gridview数据.





How can i update the gridview data using identity column in db.

update masters_addproduct set p_name=''"+pname+"'' where pid=@pid"



如果我执行此代码,则会显示错误.

必须声明标量变量pid ..

如何克服这个问题.

在此先感谢.



If i execute this code it shows error.

Must declare scalar variable pid..

How to overcome this problem.

Thanks in Advance.

推荐答案

您忘了将where子句放在更新查询中

应该是

You forgot to put the where clause in the update query

it should be

cmd = new SqlCommand("update masters_addproduct set pid=''" + pid + "'',p_name=''" + pname + "'' where pid = ''" + pid + "''", con);


我想您需要在查询中添加where子句?

"update masters_addproduct set pid=''" + pid + "'',p_name=''" + pname + "'' Where column1 = " + condition1 + " And column2 = " + condition2, con);
I suppse you need to add a where clause in your query?

"update masters_addproduct set pid=''" + pid + "'',p_name=''" + pname + "'' Where column1 = " + condition1 + " And column2 = " + condition2, con);


您必须在UPDATE 查询中编写WHERE 条件.
You have to write WHERE condition in UPDATE query.
cmd = new SqlCommand("update masters_addproduct set pid=''" + pid + "'',p_name=''" + pname + "'' WHERE primarycolumnname = primaryid", con);


这篇关于更新gridview数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆