C# 在datagridview上显示错误';组合框列

C# 在datagridview上显示错误';组合框列,c#,winforms,datagridview,combobox,C#,Winforms,Datagridview,Combobox,我正在尝试为我的winform项目创建一个新的用户管理面板。基本上,我想显示一个组合框,让管理用户在gridview的role列中为其他用户确定角色。组合框内容只有3个项目,例如“管理”、“用户”、“客户端””,但给了我一组有关数据错误事件的错误 我所做的只是在gridview上单击鼠标右键。然后单击“编辑列”,在打开的面板上,我选择“角色”列并将其ColumnType属性更改为DataGridViewComboxColumn。然后,我将角色的名称添加到Items属性的集合中,每行一个 下面您

我正在尝试为我的winform项目创建一个新的用户管理面板。基本上,我想显示一个组合框,让管理用户在gridview的role列中为其他用户确定角色。组合框内容只有3个项目,例如“管理”、“用户”、“客户端””,但给了我一组有关数据错误事件的错误

我所做的只是在gridview上单击鼠标右键。然后单击“编辑列”,在打开的面板上,我选择“角色”列并将其ColumnType属性更改为DataGridViewComboxColumn。然后,我将角色的名称添加到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;
            }
        }
    }
}


您的
DataGridView
数据绑定了吗?如果是,则显示其
数据源
。您必须确保列
角色
中单元格的基础数据应包含/存在于列
角色
组合框的
项中。例如,如果您的组合框的项只包含
a、b、c
,但基础单元格可以有一些不同的值,如
d
->,则会引发格式异常。我的gridview的DataSource属性是userBindingSource,所以基本上您是说,我的数据库表的数据类型的角色列是nvarchar(50)。它可以有一些不同于我的组合框的项目。这给了我一个错误?是的,我在前面的评论中给出的例子清楚地证明了这一点。你必须收集所有可能的值来为你的组合框构建
。很抱歉,我不明白它应该如何从数据库中收集。我只想在combobox中显示a、b和c,然后用其他用户数据记录其中一个。我是否应该创建另一个只包含用户角色类型的数据库表?您的
role
列应该只包含表中支持/设计的角色,当然,当您设计数据库表时,应该有一些类似
role
的表,您应该从该表中提取数据以填充
组合框。所有可能的值(角色类型)都应包含在您获得的集合中。否则,您的数据库设计是错误的。