Add ComboBox in the WinForm's DataGrid

xiaoxiao2021-03-06  40

WinForm's DataGrid To display the effect of ComboBox, you must write a class that inherits from DataGridTextBoxColumn, and makes the ComboBox function, and add this type of object to the DataGrid, the practice is as follows:

DataGridcomboBoxColumn Combocol = New DataGridComboBoxColumn (); Combocol.MappingName = "RegionID"; Combocol.Headertext = "Region"; combocol.width = 120;

ComboCol.ColumnComboBox.DataSource = ds1.Tables [ "Region"] DefaultView;. ComboCol.ColumnComboBox.DisplayMember = "RegionDescription"; ComboCol.ColumnComboBox.ValueMember = "RegionID";

TableStyle.PreferredRowHeight = Combocol.columncomboBox.Height 2; TableStyle.GridColumnStyles.Add (Combocol);

DataGrid1.tables.add (TableStyle);

The writings of the DataGridComboBoxColumn are as follows:

public class DataGridComboBoxColumn: DataGridTextBoxColumn {public NoKeyUpCombo ColumnComboBox; private System.Windows.Forms.CurrencyManager _source; private int _rowNum; private bool _isEditing; public static int _RowCount; public DataGridComboBoxColumn (): base () {_source = null; _isEditing = false; _RowCount = -1; ColumnComboBox = new NoKeyUpCombo (); ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList; ColumnComboBox.Leave = new EventHandler (LeaveComboBox); // BS The Cheater ColumnComboBox.SelectionChangeCommitted = new EventHandler (ComboStartEditing);} private void Combostartediting (Object Sender, Eventargs E) {_iediting = true; base.columnstartededIting (Control) Sender;

Private Void Handlescroll (Object Sender, Eventargs E) {if (ColumnComboBox.visible) ColumnComboBox.hide ();

} Private void LeaveComboBox (object sender, EventArgs e) {if (_isEditing) {SetColumnValueAtRow (_source, _rowNum, ColumnComboBox.Text); _isEditing = false; Invalidate ();} ColumnComboBox.Hide (); this.DataGridTableStyle.DataGrid.Scroll - = new EventHandler (HandleScroll);} protected override void Edit (System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) {

Base.edit (Source, Rownum, Bounds, Readonly, InstantText, Cellisvisible);

_rowNum = rowNum; _source = source; ColumnComboBox.Parent = this.TextBox.Parent; ColumnComboBox.Location = this.TextBox.Location; ColumnComboBox.Size = new Size (this.TextBox.Size.Width, ColumnComboBox.Size.Height); ColumnComboBox.SelectedIndex = ColumnComboBox.FindStringExact (this.TextBox.Text); ColumnComboBox.Text = this.TextBox.Text; this.TextBox.Visible = false; ColumnComboBox.Visible = true; this.DataGridTableStyle.DataGrid.Scroll = new EventHandler (Handlescroll); columncomboBox.bringtofront (); columncomboBox.focus ();

protected override bool Commit (System.Windows.Forms.CurrencyManager dataSource, int rowNum) {if (_isEditing) {_isEditing = false; SetColumnValueAtRow (dataSource, rowNum, ColumnComboBox.Text);} return true;}

Protected Override Void Concedefocus () {Console.writeline ("Concredefocus"); base.concedefocus ();

Protected Override Object GetColumnValueatrow (System.Windows.Forms.currencyManager Source, Int rownum) {

Object s = base.getColumnValueatrow (Source, Rownum); DataView DV = (DataView) this.columncomboBox.DataSource; int Rowcount = DV.Count; int = 0; while (i

DataView DV = (DataView) this.columncomboBox.datasource; int = 0; While (I

}

Public class nokeyupcombo: ComboBox {private const INT WM_KEYUP = 0x101;

Protected Override Void WndProc (Ref System.Windows.Forms.Message M) {IF (M.MSG == WM_KEYUP) {Return;} Base.WndProc (Ref M);}}

转载请注明原文地址:https://www.9cbs.com/read-63609.html

New Post(0)