无法看到动态创建的DataGridView
本文关键字:创建 DataGridView 动态 | 更新日期: 2023-09-27 18:05:42
我正在尝试使用复选框为相应的用户创建一个具有用户名和密码文本框列表的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。这里出了什么问题?
技巧是不要使用DataTable来添加复选框,文本框等到DataGridView。应该使用DataGridViewCheckBoxColumn和DataGridViewTextBoxColumns,而不是将它们作为列添加到DataGridView。