我正在尝试为我的 winform 项目创建一个新的用户管理面板。基本上我想显示一个组合框,让管理用户在gridview的角色列中确定其他用户的角色。组合框内容只有 3 个项目,例如“ Admin ”、“ User ”、“ Client ”,但在DataError 事件上给了我一组错误。
我所做的只是右键单击gridview。然后单击“编辑列”,在打开的面板上,我选择“角色”列并将其 ColumnType 属性更改为DataGridViewComboBoxColumn。然后我将角色名称添加到Items属性的集合中,每行一个。
您可以在下面看到我的代码、winform 设计和错误消息。我怎样才能让它工作而不给出错误消息?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace XXX
{
public partial class UserManager : Form
{
public UserManager()
{
InitializeComponent();
}
private void UserManager_Load(object sender, EventArgs e)
{
panel1.BackColor = Color.FromArgb(100, 88, 55, 55);
// TODO: This line of code loads data into the 'xXXDataSet.users' table. You can move, or remove it, as needed.
this.usersTableAdapter.Fill(this.xXXDataSet.users);
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError)
{
MessageBox.Show("Error happened " + anError.Context.ToString());
if (anError.Context == DataGridViewDataErrorContexts.Commit)
{
MessageBox.Show("Commit error");
}
if (anError.Context == DataGridViewDataErrorContexts.CurrentCellChange)
{
MessageBox.Show("Cell change");
}
if (anError.Context == DataGridViewDataErrorContexts.Parsing)
{
MessageBox.Show("parsing error");
}
if (anError.Context == DataGridViewDataErrorContexts.LeaveControl)
{
MessageBox.Show("leave control error");
}
if ((anError.Exception) is ConstraintException)
{
DataGridView view = (DataGridView)sender;
view.Rows[anError.RowIndex].ErrorText = "an error";
view.Rows[anError.RowIndex].Cells[anError.ColumnIndex].ErrorText = "an error";
anError.ThrowException = false;
}
}
}
}