DataGriView 中发生以下异常
DataGriView 中发生以下异常:system.ArgumentException:DataGridViewComboBoxCell 值无效。要替换此默认对话框,请处理DataError事件!!源代码如下:
private void FormPower_Load(object sender, EventArgs e)
{
DBDataGrid();
}
public void DBDataGrid()
{
string SqlText = "select 用户编号 as 用户编号,用户名 as 用户名,权限 as 权限 from 用户";
DataAccess DA = new DataAccess();
this.dt = DA.ExeSQLdt(SqlText);
this.dataGrid1.Columns.Clear();
//combobox数据源。。。
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("权限", typeof(string)));
DataRow row = dt.NewRow();
row[0] = "系统管理员";
dt.Rows.Add(row);
DataRow row2= dt.NewRow();
row2[0] = "普通用户";
dt.Rows.Add(row2);
dt.AcceptChanges();
//DataGridView列
DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();
cbc.DataSource = dt;
cbc.DisplayMember = "权限";
cbc.ValueMember = "权限";
cbc.DataPropertyName = "权限";
cbc.HeaderText = "权限";
DataGridViewTextBoxColumn cid = new DataGridViewTextBoxColumn();
cid.HeaderText = "用户编号";
cid.DataPropertyName = "用户编号";
DataGridViewTextBoxColumn cname = new DataGridViewTextBoxColumn();
cname.HeaderText = "用户名";
cname.DataPropertyName = "用户名";
this.dataGrid1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { cid, cname, cbc });
//DataGridView数据源
DataTable dtSource = new DataTable();
dtSource.Columns.Add("用户编号", typeof(Int32));
dtSource.Columns.Add("用户名", typeof(string));
dtSource.Columns.Add("权限", typeof(string));
foreach (DataRow forrow in this.dt.Rows)
{
DataRow r1 = dtSource.NewRow();
r1[0] = forrow[0];
r1[1] = forrow[1];
r1[2] = forrow[2];
dtSource.Rows.Add(r1);
dtSource.AcceptChanges();
}
this.dataGrid1.DataSource = dtSource;
this.dataGrid1.Columns[0].ReadOnly = true;
this.dataGrid1.Columns[1].ReadOnly = true;
if (this.dataGrid1.Rows.Count != 0)
{
for (int i = 0; i < this.dataGrid1.Rows.Count; )
{
this.dataGrid1.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Pink;
i += 2;
}
}
}
private void dataGrid1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(dataGrid1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
string SqlText = "update 用户 set 权限 = '" + dataGrid1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() + "' where 用户编号 =" + this.dataGrid1.Rows[e.RowIndex].Cells[0].Value;
DataAccess DA = new DataAccess();
DA.ExeSQL(SqlText);
DBDataGrid();
} --------------------编程问答-------------------- DataGridViewComboBox赋值之前必须在Item里面有这个值
--------------------编程问答--------------------
具体有啥做才行呢??谢谢!!
补充:.NET技术 , C#