为什么我的DataGrid没有正确填充密码?

时间:2015-01-27 04:52:02

标签: c# wpf datagrid ado.net

我有以下代码:

         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);
    }

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

1 个答案:

答案 0 :(得分:0)

只需更改此行

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

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

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