【问题标题】:Update not working from grid view (not able to get the error )更新无法从网格视图工作(无法得到错误)
【发布时间】:2016-04-01 19:49:45
【问题描述】:

我正在尝试将表格从工具的 gridview 更新到 SQL 数据库。问题是它没有在数据库中更新。

下面是我更新数据库的按钮点击代码: 在调试代码时,我发现数据表 DT 只获取源值而不是网格视图中的更新值....

网格视图中是否有任何属性可以接受这些更改并更新 DT 表?

    public partial class BusinessRules : Form
    {
    //Declaration Part
    private SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=AnimalProductsCoSD;Integrated Security=True");

    private string sqlconn;  // query and sql connection

    private SqlDataAdapter SDA = new SqlDataAdapter();

    DataTable DT = new DataTable();
    SqlCommandBuilder scb = new SqlCommandBuilder();

   private void button_retreive_Click(object sender, EventArgs e)
    {

        string commandText = "CoSD.RetreiveBusinessRulesTool";
        SqlCommand cmd = new SqlCommand(commandText, con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@BusinessType", SqlDbType.NVarChar, 60).Value = comboBox_BusinessType.Text;
        cmd.Parameters.Add("@CommodityGroup", SqlDbType.VarChar, 60).Value = comboBox_group.Text;
        try
        {

            con.Open();
            SDA.SelectCommand = cmd;
            DT = new DataTable();
            SDA.Fill(DT);
            int count1 = DT.Rows.Count;
            if (DT.Rows.Count > 0)
            {
                dataGridView.DataSource = DT;
                dataGridView.Columns[0].DefaultCellStyle.ForeColor = Color.Gray;
                dataGridView.Columns[0].ReadOnly = true;           
            }
            else
            {
                MessageBox.Show("No Business Rules Found");


            }
        }
        catch (SqlException ex)
        {
            MessageBox.Show("Error : " + ex.Message);
        }
        finally
        {
            con.Close();
        }

    }

private void button_update_Click(object sender, EventArgs e)
    {
        try
        {
            if (MessageBox.Show("Do you really want to Update these values?", "Confirm Update", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                scb = new SqlCommandBuilder(SDA);
                SDA.Update(DT);
                // confirm
                MessageBox.Show("Updates successfully submitted to CoSD");

            }

            else
            {
                return;
            }

        }

        catch (Exception ex)
        {
            MessageBox.Show("Error : " + ex.Message);
        }


}   

【问题讨论】:

  • 1. 为什么保存的时候要设置grid的数据源? 2. 如何创建SDA 数据适配器?请将其代码添加到问题中。 3. 如何填写和编辑DT 数据表。请将其代码添加到问题中。
  • @RezaAghaei...您好我已经编辑了问题请检查代码现在...
  • 您可能会发现this post 很有帮助。
  • @RezaAghaei...谢谢...我试过了,但没用...问题是从存储 SP 的输出到数据表,然后我正在尝试使用 gridview 中显示的列更新表...显示的代码不起作用...
  • 我还使用存储过程测试了链接的答案,它工作正常。

标签: c# .net winforms datagridview windows-applications


【解决方案1】:

在试试,放这个

 scb = new SqlCommandBuilder(sda);
            sda.Update(dt);

在您的初始化程序调用中

  SqlDataAdapter sda= new SqlDataAdapter("SELECT * FROM someWhere", connectionString);
    DataTable dt = new DataTable();

问题是您在提交更改之前刷新了datagridview,从而删除了输入的任何内容

**** 编辑 ****

这正是我不久前做的一个项目中的代码的样子:

namespace TowerSearch
{
    public partial class EditParts : Form
    {
        const string conString = ConString.conString;
        static DataClasses1DataContext PartsLog = new      DataClasses1DataContext(conString);
        static Table<Part> listOfParts = PartsLog.GetTable<Part>();

        SqlDataAdapter sda;
        SqlCommandBuilder scb;
        DataTable dt;

        public EditParts()
        {
            InitializeComponent();
        }

        //Load and refresh the dataGridView
        private void showData()
        {
            SqlConnection con = new SqlConnection(conString);
            sda = new SqlDataAdapter("SELECT * FROM Parts", con);

            dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
        }

        private void EditParts_Load(object sender, EventArgs e)
        {
           showData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                dataGridView1.Refresh();
                scb = new SqlCommandBuilder(sda);
                sda.Update(dt);

                MessageBox.Show("Saved");
                showData();
            }
            catch (Exception ee)
            {
                MessageBox.Show("There is an error in the data!\nCheck if there are any blank spots besides Quantity.");
            }  
        }
    }
}

这绝对有效,因此请尝试使用显示数据的代码。我建议先逐字复制,看看是否可行。

**** 编辑 2 ****

如果您还没有设法获得它,您可以尝试的另一件事是添加一个 bindingSource。为此,请将 bindingSource 拖到您的 dataGridView 上,然后将 DataSource 选项设置为您不想显示的 DB 表

希望有帮助!

【讨论】:

  • 感谢您的输入...但是如果删除这 2 行代码 dataGridView.Refresh(); dataGridView.DataSource = DT;我仍然无法更新数据库中的表。
  • 我添加了更多示例代码,所以就这样尝试吧
  • 我只是有一个简单的问题......在 DT(数据表)中,该 DT 是否需要看起来类似于我们需要更新的表?我的意思是在 gridview 中显示的 DT 是否需要后端数据库中的所有列?
  • dt 发生的事情是它是一个完全独立的临时表。因此,更新的是 dt 而不是 db。当您使用 dt 更新 sda 时,您实际上是将 dt 中的任何内容添加到实际数据库中。它从 datagridview 中获取列。
  • 所以gridview显示部分的列数较少不会影响这个更新语句。这就是你的答案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-07
  • 2012-06-16
  • 2023-04-04
  • 2013-07-04
  • 1970-01-01
  • 2015-08-31
  • 1970-01-01
相关资源
最近更新 更多