none
如何限制dataGridView的取值 RRS feed

  • 问题

  • 我的dataGridView绑定了一个table。
    在dataGridWiew进行编辑时,我想把它限制在我提供的几个值之内。(比如说,在编辑第1列的某一个单元格时,只能对我提供的几个值,“R”“”E“”D“进行选择)
    这个怎么实现呢?
    谢谢。
    2009年7月16日 5:57

答案

  • 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";
    
                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年7月16日 9:36
    2009年7月16日 7:54
    版主

全部回复

  • 可以使用下拉列表或者使用正则表达式 http://www.cnblogs.com/UouHt/archive/2009/02/16/1391687.html
    知识改变命运,奋斗成就人生!
    2009年7月16日 6:02
    版主
  • 用下拉列表框可以么?
    答案900, 目标五颗星 Aurvana Air, 音乐真的是随风飘来的 凉宫春日 永无止境的八月 你到底什么时候结束阿。。。。
    2009年7月16日 6:19
    版主
  • 用下拉列表框可以么?
    答案900, 目标五颗星 Aurvana Air, 音乐真的是随风飘来的 凉宫春日 永无止境的八月 你到底什么时候结束阿。。。。

    我就是想用这个方法。但是知道怎么处理。刚刚看了半天的combox还是一头雾水。
    希望斑竹能给个例子。谢谢~
    2009年7月16日 6:40
  • 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";
    
                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年7月16日 9:36
    2009年7月16日 7:54
    版主