首页 新闻 会员 周边

在DataGridView中修改数据问题

0
悬赏园豆:30 [已关闭问题] 关闭于 2011-06-18 15:40

用DataSet填充DataGridView,当DataGridView中数据被修改时,
SqlCommandBuilder builder = new SqlCommandBuilder(dataAdapter);
使用dataAdapter.update(dataSet,"ds")
但是ds查询之前是经过包装的数据(数据可能是从外键表查出之类),如果我在页面的DataGridView中更新一定会失败,怎么办?这时候该怎么更新啊,希望各位大虾帮忙解答!谢谢~

问题补充:

谢谢各位的回答,这个问题因为当时催得紧,就采用了另外的方式解决,前几天忙起来这个就被搁置了,没有及时回应很抱歉,各位的答案我会试试,就把分数散给大家了,谢谢各位!

亚亚小怪兽的主页 亚亚小怪兽 | 初学一级 | 园豆:180
提问于:2011-06-04 18:35
< >
分享
所有回答(3)
0

你可以设定几个事件,不同的数据更新对应着不同的事件,在事件代码里面去区分,然后更新数据库的数据,就是你要知道他修改的数据在那个表里面,或者你可以看他更新的是哪个字段,然后再看字段对应的是哪个表里面的东西,再用odbc去直接更新数据库!恩,就是只有思路!

C#菜子 | 园豆:215 (菜鸟二级) | 2011-06-05 10:50
0

1.

建两个表 tb_Card,tb_City

public class CustomerAdapter
    {
        public static SqlDataAdapter CreateCustomerAdapter(SqlConnection connection)
        {
            SqlDataAdapter adapter = new SqlDataAdapter();
            //create the SelectCommand
            SqlCommand command = new SqlCommand("select tb_City.Province,tb_City.City, tb_Card.CardName from tb_City , tb_Card", connection);
                       adapter.SelectCommand = command;
            //Create the insertCommand
            command = new SqlCommand("insert into tb_City(Province,City)values(@Province,@City) insert into tb_Card(CardName)values(@CardName)",connection);
            command.Parameters.Add("@Province", SqlDbType.Text,15, "Province");
            command.Parameters.Add("@City", SqlDbType.Text, 15, "City");
            command.Parameters.Add("@CardName", SqlDbType.Text, 15, "CardName");

//Province,City属于tb_City

//CardName属于tb_Card
            adapter.InsertCommand = command;
            return adapter;
        }
    
    }

2.

public class GridViewClass
    {
        private static SqlConnection con;
        private static DataSet ds;
        private static SqlDataAdapter da;
        public static DataSet ReadData()
        {
            con = new SqlConnection("server=(local);database=db_04;uid=sa;pwd=");
            con.Open();
            da = CustomerAdapter.CreateCustomerAdapter(con);
            ds = new DataSet();
            da.Fill(ds);
            con.Close();
            return ds;
        }

        public static void AddData()
        {
            DataRow dr = ds.Tables[0].NewRow();
            dr["Province"] = "yunnan";
            dr["City"] = "kunming";
            dr["CardName"] = "123888";
            ds.Tables[0].Rows.Add(dr);
            da.Update(ds);
          
        }

}

3.

 public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Repeater1.DataSource = GridViewClass.ReadData();
            Repeater1.DataBind();
        }

                protected void Button1_Click(object sender, EventArgs e)
        {
            GridViewClass.AddData();
            Repeater1.DataSource = GridViewClass.ReadData();
        }
    }

yxf2011 | 园豆:6 (初学一级) | 2011-06-06 15:20
0

可以设置个右键菜单,选中哪行以后点右键,进行修改,方法自己写,最后在重新掉一下数据源就OK了

王元勋 | 园豆:413 (菜鸟二级) | 2011-06-07 13:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册