如何使用更新按钮更新datagridview [英] How to update datagridview using update button

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

问题描述

我想使用下面的代码更新datagridview中的特定列,但它不起作用。



我尝试过:



i want to update perticular columns in datagridview using below code but it is not working.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;

private void Form2_Load(object sender, EventArgs e)
{
  da = new OleDbDataAdapter("select * from Spldetails",con1);
  ds = new System.Data.DataSet();
  da.Fill(ds, "spldetails");
  dataGridView1.DataSource = ds.Tables[0];
}

private void button5_Click(object sender, EventArgs e)
        {
            using (OleDbConnection con1 = new OleDbConnection(con))
            {
                
 
                    con1.Open();
                    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                    {
                        cmd = new OleDbCommand();
                        cmd.Connection = con1;
                        string query2 = "Update Spldetails set Status = @sts,Comment =@cmnt,Approvedhrs =@aphr,Reviewer ='" + Environment.UserName + "',Revieweddate = Now where ID =@id ";
                        cmd.Parameters.AddWithValue("@id", dataGridView1.Rows[i].Cells[0].Value);
                        cmd.Parameters.AddWithValue("@sts", dataGridView1.Rows[i].Cells[10].Value);
                        cmd.Parameters.AddWithValue("@cmnt", dataGridView1.Rows[i].Cells[11].Value);
                        cmd.Parameters.AddWithValue("@aphr", dataGridView1.Rows[i].Cells[13].Value);
                        //cmd.Parameters.AddWithValue("@AfterMC", dataGridView1.Rows[i].Cells[15].Value);

                      
                        cmd.CommandText = query2;
                        cmd.ExecuteNonQuery();

                    }

推荐答案

我认为问题是:您使用DataSource属性以及何时使用DataSource ,你不能覆盖DataGridView单元格。我认为你应该更新DataSource并解决问题..
I think the problem is: you use the DataSource property and when you use a DataSource, you cant overwrite the DataGridView Cells. I think you should update the DataSource and the problem is solved..


这篇关于如何使用更新按钮更新datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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