落雪成白草未殃 2016-09-15 05:22 采纳率: 100%
浏览 3458
已采纳

C#datagridview一次提交多行修改数据

我做好了修改C#datagridview的功能,但是只能每次修改一行,然后选择修改按钮
这样才能保存,如果同时修改多行,就只能保存最后一次修改的结果,可是我想修改多行,然后按修改按钮,能够同时修改多行数据。以下是我修改按钮中的代码:
private void button2_Click_1(object sender, EventArgs e)
{
//计划名称和计划时间
String jihuamingcheng = textBox1.Text;
String jihuashijian = dateTimePicker1.Value.Date.ToLongDateString();
//保存计划

        using (SqlConnection sqlcon = new SqlConnection(strcon))
        {
            sqlcon.Open();
            string id = this.dataGridView1.CurrentRow.Cells[0].Value + "";
            string riqi = this.dataGridView1.CurrentRow.Cells[1].Value + "";
            string neirong = this.dataGridView1.CurrentRow.Cells[2].Value + "";
            string wanchengdu = this.dataGridView1.CurrentRow.Cells[3].Value + "";


          //  string sql = "update " + jihuamingcheng + " set 创建时间='" + jihuashijian + "',内容='" + neirong + "',完成度='" + wanchengdu + "' where 项目号='" + id + "'";
           // SqlCommand command = new SqlCommand(sql, sqlcon);
           // myda = new SqlDataAdapter(command);
            //myset.Tables.Clear();
           // myda.Fill(myset, "" + jihuamingcheng + "");
            //dataGridView1.DataSource = myset.Tables["" + jihuamingcheng + ""];


            if (this.dataGridView1.DataSource != null)
            {
                DialogResult result = MessageBox.Show("确定将此次盘库数据保存至数据库?(存储前请务必核对准确)", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (result == DialogResult.OK)
                {
                    for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
                    {

                        string cmdInsert = String.Empty;
                        cmdInsert = "INSERT INTO " + jihuamingcheng + "(项目号,创建时间,内容,完成度)";
                        cmdInsert += "," + this.dataGridView1.Rows[i].Cells[2].Value + "";
                        cmdInsert += "," + this.dataGridView1.Rows[i].Cells[3].Value + "";

                    }


                }




            }
        }
        //刷新表


        using (SqlConnection sqlcon = new SqlConnection(strcon))
        {
            sqlcon.Open();


            string sql = "select * from "+jihuamingcheng+"";
            SqlCommand command = new SqlCommand(sql, sqlcon);
            myda = new SqlDataAdapter(command);
            myset.Tables.Clear();
            myda.Fill(myset, ""+jihuamingcheng+"");
            dataGridView1.DataSource = myset.Tables[""+jihuamingcheng+""];
        }






    }
  • 写回答

2条回答 默认 最新

  • 微wx笑 Java领域优质创作者 2016-09-16 01:37
    关注

    1、使用dataGridView本身的功能,与数据源绑定,将更新同步到数据表;
    2、自己编写代码检测修改了哪些行,更新到数据表;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)