【问题标题】:How can I insert a row from datagridview to Database如何将一行从 datagridview 插入到数据库
【发布时间】:2015-07-08 10:12:39
【问题描述】:

有没有办法在winforms中从datagridview插入一行到数据库?

这就是我创建网格视图的方式:

        this.dataGridView1.AllowUserToAddRows = true;
        this.dataGridView1.AllowUserToDeleteRows = true;
        var query = from fe in tm.RDP0 select fe;
        testList = query.ToList();
        dataGridView1.DataSource = testList.ToList();

这就是我向 dataGridview 添加新行的方式

        RDP0 newrow = new RDP0();
        newrow.NO = 1 + dataGridView1.Rows.Count;
        newrow.CUSTID = 1000 + dataGridView1.Rows.Count;
        testList.Add(newrow);
        dataGridView1.DataSource = null;
        dataGridView1.DataSource = testList;

在 datagridview 上填充新创建的空行后,我想将其插入到我的数据库中。

tm.SaveChanges();  

因为上面的 SaveChanges() 不起作用。

任何帮助将不胜感激。

【问题讨论】:

    标签: c# database winforms datagridview insert


    【解决方案1】:

    不再需要帮助。我找到了解决方案。

                int index = dataGridView1.Rows.Count;
                RDP0 newefes = new EFESRDP0();
                newefes.NO = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
                newefes.CUSTID = Convert.ToInt32(dataGridView1.CurrentRow.Cells[1].Value);
    
                newefes.IPADDRESS = Convert.ToString(dataGridView1.CurrentRow.Cells[3].Value);
                newefes.USER = Convert.ToString(dataGridView1.CurrentRow.Cells[4].Value);                
    
                db.RDP0s.InsertOnSubmit(newefes);
                db.SubmitChanges();
    

    通过 Linq to Sql 方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多