none
出现异常:DataGridViewComboBoxColumn值无效 RRS feed

  • 问题

  • 现有一张 物理数据表 "Question"结构如下:
    Id                         int                 主键 标识列 种子:1 增量:1  Not Null;
    Question               varchar(50)                                             Not Null;
    Answer                 varchar(50)                                             Not Null;
    Level                    int                  约束为:只能为 1或2或3        Not Null;

    通过DataTable 将该物理数据源 设定到 一个 桌面程序的 DataGridView 控件 dgv(实例名)中,
    为dgv 创建 四个列 分别用于显示数据源 Id,Question ,Answer,Level 
    名称分别为: ColId(设为 ReadOnly=True)
                     ColQue
                     ColAns
                     以上三个列的ColumnType值为 DataGridViewTextBoxColumn
                     ColLev 的ColumnType值为
    DataGridViewComboBoxColumn
    如何 在 窗体 Load 的时候 将物理数据表Question显示在dgv的这四个列中,并且ColLev单元格的下拉列表值
    为 1,2,3(这样使用户修改ColLev单元格的值的时候限定在 1或2或3中)
                    要求是既让用户知道每条记录的当前Level是多少,又限定用户对Level的修改限定在1或2或3中,
               尝试:在ColLev.Items.AddRange(new string[] { "1","2","3" });并且ColLev.DataPropertyName="Level ";
               结果:出现异常:
    DataGridViewComboBoxColumn值无效
    请问还有哪位能帮忙解决以上这个问题啊?不胜感激!!!!!
    • 已移动 肖小勇Moderator 2009年9月8日 5:14 .Net Framework 一般性问题 (发件人:Visual C#)
    2009年9月8日 3:39

答案

  • 你好!
         DataGridViewComboBoxCell里的值必须要和绑定的数据里的其中的一个值相同,不相同会报这个异常的
    周雪峰
    2009年9月8日 4:24
    版主
  • 之前写的一个示例,包括了你描述的功能
    ------------------------------------------
    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 X.WinFormsApp
    {
        public partial class X200906170939 : Form
        {
            private DataSet fEmployee;
            public DataSet Employee
            {
                get
                {
                    if (fEmployee == null)
                    {
                        fEmployee = new DataSet();
                        fEmployee.Tables.Add(new DataTable("Employee"));
    
                        fEmployee.Tables[0].Columns.Add(new DataColumn("EmployeeKey", typeof(Int32)));
                        fEmployee.Tables[0].Columns.Add(new DataColumn("DepartmentKey", typeof(Int32)));
                        fEmployee.Tables[0].Columns.Add(new DataColumn("Name", typeof(String)));
                        fEmployee.Tables[0].Columns.Add(new DataColumn("ModifyDate", typeof(DateTime)));
    
                        fEmployee.Tables[0].Rows.Add(new object[] { 1, 1, "John", DateTime.Now });
                        fEmployee.Tables[0].Rows.Add(new object[] { 2, 1, "Tomi", DateTime.Now });
                        fEmployee.Tables[0].Rows.Add(new object[] { 3, 2, "X.XY", DateTime.Now });
                        fEmployee.Tables[0].Rows.Add(new object[] { 4, 2, "Sam", DateTime.Now });
                        fEmployee.Tables[0].Rows.Add(new object[] { 5, 3, "Mary", DateTime.Now });
                        fEmployee.Tables[0].Rows.Add(new object[] { 6, 3, "Chirs", DateTime.Now });
                    }
    
                    return fEmployee;
                }
            }
    
            private DataSet fDepartment;
            public DataSet Department
            {
                get
                {
                    if (fDepartment == null)
                    {
                        fDepartment = new DataSet();
                        fDepartment.Tables.Add(new DataTable("Department"));
    
                        fDepartment.Tables[0].Columns.Add(new DataColumn("DepartmentKey", typeof(Int32)));
                        fDepartment.Tables[0].Columns.Add(new DataColumn("Name", typeof(String)));
    
                        fDepartment.Tables[0].Rows.Add(new object[] { 1, "A"});
                        fDepartment.Tables[0].Rows.Add(new object[] { 1, "B" });
                        fDepartment.Tables[0].Rows.Add(new object[] { 2, "C" });
                        fDepartment.Tables[0].Rows.Add(new object[] { 2, "D" });
                        fDepartment.Tables[0].Rows.Add(new object[] { 3, "E" });
                        fDepartment.Tables[0].Rows.Add(new object[] { 3, "F" });
                    }
    
                    return fDepartment;
                }
            }
    
            public X200906170939()
            {
                InitializeComponent();
                this.InitControls();
            }
    
            private void InitControls()
            {
                Button button = new Button();
                button.Dock = DockStyle.Top;
                button.Text = "保存XML";
                button.Click += new EventHandler(button_Click);
                this.Controls.Add(button);
    
                DataGridView fDataGridView = new DataGridView();
                fDataGridView.Dock = DockStyle.Fill;
                fDataGridView.AutoGenerateColumns = false;
    
                DataGridViewColumn fColumn1 = new DataGridViewColumn(new DataGridViewTextBoxCell());
                fColumn1.HeaderText = "EmployeeKey";
                fColumn1.DataPropertyName = "EmployeeKey";
    
                DataGridViewColumn fColumn2 = new DataGridViewColumn(new DataGridViewTextBoxCell());
                fColumn2.HeaderText = "Name";
                fColumn2.DataPropertyName = "Name";
    
                //DataGridViewLinkColumn fColumn = new DataGridViewLinkColumn();
                //fColumn.DataPropertyName = "你要绑定的字段名";
                //DataGridView1.Columns.Add(fColumn);
                DataGridViewComboBoxColumn fColumn3 = new DataGridViewComboBoxColumn();
                BindingSource fBindingSource = new BindingSource(this.Department, "Department");
                fColumn3.DataSource = fBindingSource;
                fColumn3.DisplayMember = "Name";
                fColumn3.ValueMember = "DepartmentKey";
                fColumn3.DataPropertyName = "DepartmentKey";
    
                fDataGridView.Columns.Add(fColumn1);
                fDataGridView.Columns.Add(fColumn2);
                fDataGridView.Columns.Add(fColumn3);
    
                fDataGridView.DataSource = this.Employee.Tables[0];
                this.Controls.Add(fDataGridView);
            }
    
            void button_Click(object sender, EventArgs e)
            {
                using (SaveFileDialog dialog = new SaveFileDialog())
                {
                    dialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";   
                    if (dialog.ShowDialog() == DialogResult.OK)
                        this.Employee.WriteXml(dialog.FileName);
                }
            }
        }
    }
    


    知识改变命运,奋斗成就人生!
    2009年9月8日 5:03
    版主

全部回复

  • 你好!
         DataGridViewComboBoxCell里的值必须要和绑定的数据里的其中的一个值相同,不相同会报这个异常的
    周雪峰
    2009年9月8日 4:24
    版主
  • 你好!
         你的问题我已经在另外一个帖子中做了回复,请不要重复发帖!
          多谢你的合作!
    周雪峰
    2009年9月8日 4:29
    版主
  • 请问针对以上案例 你有什么具体实施办法吗?

    2009年9月8日 4:35
  • 之前写的一个示例,包括了你描述的功能
    ------------------------------------------
    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 X.WinFormsApp
    {
        public partial class X200906170939 : Form
        {
            private DataSet fEmployee;
            public DataSet Employee
            {
                get
                {
                    if (fEmployee == null)
                    {
                        fEmployee = new DataSet();
                        fEmployee.Tables.Add(new DataTable("Employee"));
    
                        fEmployee.Tables[0].Columns.Add(new DataColumn("EmployeeKey", typeof(Int32)));
                        fEmployee.Tables[0].Columns.Add(new DataColumn("DepartmentKey", typeof(Int32)));
                        fEmployee.Tables[0].Columns.Add(new DataColumn("Name", typeof(String)));
                        fEmployee.Tables[0].Columns.Add(new DataColumn("ModifyDate", typeof(DateTime)));
    
                        fEmployee.Tables[0].Rows.Add(new object[] { 1, 1, "John", DateTime.Now });
                        fEmployee.Tables[0].Rows.Add(new object[] { 2, 1, "Tomi", DateTime.Now });
                        fEmployee.Tables[0].Rows.Add(new object[] { 3, 2, "X.XY", DateTime.Now });
                        fEmployee.Tables[0].Rows.Add(new object[] { 4, 2, "Sam", DateTime.Now });
                        fEmployee.Tables[0].Rows.Add(new object[] { 5, 3, "Mary", DateTime.Now });
                        fEmployee.Tables[0].Rows.Add(new object[] { 6, 3, "Chirs", DateTime.Now });
                    }
    
                    return fEmployee;
                }
            }
    
            private DataSet fDepartment;
            public DataSet Department
            {
                get
                {
                    if (fDepartment == null)
                    {
                        fDepartment = new DataSet();
                        fDepartment.Tables.Add(new DataTable("Department"));
    
                        fDepartment.Tables[0].Columns.Add(new DataColumn("DepartmentKey", typeof(Int32)));
                        fDepartment.Tables[0].Columns.Add(new DataColumn("Name", typeof(String)));
    
                        fDepartment.Tables[0].Rows.Add(new object[] { 1, "A"});
                        fDepartment.Tables[0].Rows.Add(new object[] { 1, "B" });
                        fDepartment.Tables[0].Rows.Add(new object[] { 2, "C" });
                        fDepartment.Tables[0].Rows.Add(new object[] { 2, "D" });
                        fDepartment.Tables[0].Rows.Add(new object[] { 3, "E" });
                        fDepartment.Tables[0].Rows.Add(new object[] { 3, "F" });
                    }
    
                    return fDepartment;
                }
            }
    
            public X200906170939()
            {
                InitializeComponent();
                this.InitControls();
            }
    
            private void InitControls()
            {
                Button button = new Button();
                button.Dock = DockStyle.Top;
                button.Text = "保存XML";
                button.Click += new EventHandler(button_Click);
                this.Controls.Add(button);
    
                DataGridView fDataGridView = new DataGridView();
                fDataGridView.Dock = DockStyle.Fill;
                fDataGridView.AutoGenerateColumns = false;
    
                DataGridViewColumn fColumn1 = new DataGridViewColumn(new DataGridViewTextBoxCell());
                fColumn1.HeaderText = "EmployeeKey";
                fColumn1.DataPropertyName = "EmployeeKey";
    
                DataGridViewColumn fColumn2 = new DataGridViewColumn(new DataGridViewTextBoxCell());
                fColumn2.HeaderText = "Name";
                fColumn2.DataPropertyName = "Name";
    
                //DataGridViewLinkColumn fColumn = new DataGridViewLinkColumn();
                //fColumn.DataPropertyName = "你要绑定的字段名";
                //DataGridView1.Columns.Add(fColumn);
                DataGridViewComboBoxColumn fColumn3 = new DataGridViewComboBoxColumn();
                BindingSource fBindingSource = new BindingSource(this.Department, "Department");
                fColumn3.DataSource = fBindingSource;
                fColumn3.DisplayMember = "Name";
                fColumn3.ValueMember = "DepartmentKey";
                fColumn3.DataPropertyName = "DepartmentKey";
    
                fDataGridView.Columns.Add(fColumn1);
                fDataGridView.Columns.Add(fColumn2);
                fDataGridView.Columns.Add(fColumn3);
    
                fDataGridView.DataSource = this.Employee.Tables[0];
                this.Controls.Add(fDataGridView);
            }
    
            void button_Click(object sender, EventArgs e)
            {
                using (SaveFileDialog dialog = new SaveFileDialog())
                {
                    dialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";   
                    if (dialog.ShowDialog() == DialogResult.OK)
                        this.Employee.WriteXml(dialog.FileName);
                }
            }
        }
    }
    


    知识改变命运,奋斗成就人生!
    2009年9月8日 5:03
    版主
  • 感谢你的回复!对我的帮助将很大。

    2009年9月8日 5:41