从网格视图更新无法正常工作(无法获取错误)

时间:2016-03-29 20:56:51

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

我正在尝试将工具的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 个答案:

答案 0 :(得分: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.");
            }  
        }
    }
}

这绝对有效,所以请尝试使用show data进行编码。我建议先将其逐字复制,看它是否有效。

**** 编辑 2 ****

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

希望有所帮助!