我正在尝试使用复选框为相应的用户创建一个包含用户名和密码文本框列表的DataTable。 代码如下:
private void createAndBindSchemaUsersDataGrid()
{
schemaUserDataGrid = new DataGridView();
this.schemaUsers.Controls.Add(this.schemaUserDataGrid);
DataTable usersTable = new DataTable();
usersTable.Columns.Add("User Name", typeof(string));
usersTable.Columns.Add("Change Password", typeof(CheckBox));
usersTable.Columns.Add("Password", typeof(TextBox));
usersTable.Columns.Add("Confirm Password", typeof(TextBox));
//Loop through the list of fetched users
for (int i = 0; i < usersList.Count; i++)
{
usersTable.Rows.Add(usersList[i], false, "", "");
}
//binding dataTable to DataGrid
schemaUserDataGrid.DataSource = new BindingSource(usersTable, null);
schemaUserDataGrid.Visible = true;
schemaUserDataGrid.Refresh();
schemaUserDataGrid.AutoGenerateColumns = true;
schemaUsers.Refresh();
schemaUserDataGrid.CurrentCellDirtyStateChanged += new EventHandler(schemaUserDataGrid_CurrentCellDirtyStateChange);
schemaUserDataGrid.CellValueChanged += new DataGridViewCellEventHandler(schemaUserDataGrid_CellValueChanged);
}
但是当这段代码运行时,我只看到标签页上的空dgv。 这里出了什么问题?
答案 0 :(得分:0)
诀窍是不要使用DataTable将复选框,文本框等添加到DataGridView。 应该使用DataGridViewCheckBoxColumn和DataGridViewTextBoxColumns,并将它们作为列添加到DataGridView。