【问题标题】:Why is my DataGrid not populating the password properly?为什么我的 DataGrid 没有正确填充密码?
【发布时间】:2015-01-27 04:52:01
【问题描述】:

我有以下代码:

         private void FillData() {
        try {
            // this is the adapter
            dataAdapter = new SqlDataAdapter("Select username, isnull(password,'') from Credentials", connectionString);

            SqlCommandBuilder cmb = new SqlCommandBuilder(dataAdapter);

            // populate new data and bind it.
            DataTable table = new DataTable();
            dataAdapter.Fill(table);
            bindingSource1.DataSource = table;

            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
        }
        catch (Exception e) {
            MessageBox.Show("Error " +e);
        }

    }

    private void ManualGeneratedDGF_Load(object sender, EventArgs e) {
        dataGridView1.DataSource = bindingSource1;
    }

    private void button1_Click(object sender, EventArgs e) {
        bindingSource1 = (BindingSource) dataGridView1.DataSource;
        DataTable ds = new DataTable();
        ds = (DataTable) bindingSource1.DataSource;
        dataAdapter.Update(ds);
    }

我不明白为什么当我单击“保存”按钮时上面的代码没有正确填充。我有一个用户名和密码 int eh datagrid,但是当我单击“保存”按钮时,它会正确插入用户名而不是密码?

【问题讨论】:

  • 我发现问题是“isnull(password, '')。但是,我不知道如何更改它,以便在更新时它会更新密码列?

标签: c# wpf datagrid ado.net


【解决方案1】:

只要改变这一行

dataAdapter = new SqlDataAdapter("Select username, isnull(password,'') from Credentials", connectionString);

dataAdapter = new SqlDataAdapter("Select username, isnull([password],'') from Credentials", connectionString);

或者在您使用password 的地方使用方括号,因为它是服务器的保留字。 告诉我它是否有效。

【讨论】:

  • 我试过了,没用。我认为这不是因为保留字,而是因为如果我使用 isnull(password,''),映射将被丢弃。那么如何重新映射所有内容呢?
  • 你试过NULLIF而不是ISNULL,因为我发现它here
猜你喜欢
  • 2020-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-21
  • 1970-01-01
  • 2014-05-18
  • 2017-03-05
相关资源
最近更新 更多