private void InitializeeZDataGridView() { // 创建一个表格并添加到窗口中 _eZdgv = new eZDataGridView() { // Size = new Size(650, 550), Location = new Point(100, 0), // 基本配置 KeyDelete = true, ManipulateRows = true, ShowRowNumber = true, SupportPaste = true, // AllowUserToAddRows = true, AutoGenerateColumns = false, }; this.Controls.Add(_eZdgv); // 创建与表格相绑定的数据源 _persons = new BindingList <Person> { AllowNew = true }; ConstructeZDataGridView(_eZdgv); }
private void ConstructeZDataGridView(eZDataGridView eZdgv) { eZdgv.DataSource = _persons; // 创建数据列并绑定到数据源 ---------------------------------------------- // ------------------------- var column = new DataGridViewTextBoxColumn(); column.DataPropertyName = "Id"; column.Name = "序号(只读)"; eZdgv.Columns.Add(column); // ------------------------- column = new DataGridViewTextBoxColumn(); column.DataPropertyName = "Name"; column.Name = "名称"; eZdgv.Columns.Add(column); // ------------------------- DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn(); combo.DataSource = Enum.GetValues(typeof(Gender)); combo.DataPropertyName = "Gender"; combo.Name = "性别"; eZdgv.Columns.Add(combo); // 如果要设置对应单元格的值为某枚举项:combo.Item(combo.Index,行号).Value = Gender.Male; // ------------------------- column = new DataGridViewTextBoxColumn(); column.DataPropertyName = "Age"; column.Name = "年龄"; column.ValueType = typeof(int); // 限定此列的数据类型必须为整数值 eZdgv.Columns.Add(column); // ------------------------- // Initialize and add a check box column. DataGridViewCheckBoxColumn columnCheck = new DataGridViewCheckBoxColumn() { Name = "Dead", DataPropertyName = "Dead", HeaderText = "死亡", }; eZdgv.Columns.Add(columnCheck); // 如果要设置对应单元格的值为某枚举项:combo.Item(combo.Index,行号).Value = true; // ------------------------- // 为DataGridView控件中添加一列,此列与DataSource没有任何绑定关系 // 注意,添加此列后,DataGridView.DataSource的值并不会发生改变。 DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn() { Name = "Details", HeaderText = "详细", Text = "View Details", // Use the Text property for the button text for all cells rather // than using each cell's value as the text for its own button. UseColumnTextForButtonValue = true, }; eZdgv.Columns.Insert(0, buttonColumn); // 事件绑定 ------------------------------------------------------------- _eZdgv.DataError += EZdgvOnDataError; // 响应表格中的数据类型不匹配等出错的情况 _eZdgv.CellContentClick += EZdgvOnCellContentClick; // 响应表格中的按钮按下事件 _eZdgv.CurrentCellDirtyStateChanged += EZdgvOnCurrentCellDirtyStateChanged; // 在表格中Checkbox的值发生改变时立即作出响应 // 如果数据行所对应的实例对象有无参数的构造函数,则AddingNew事件不是必须的。 _persons.AddingNew += PersonsOnAddingNew; }