[WinForm] ComboBox drop-down box in DataGrid

zhaozj2021-02-16  77

I found a class found online, I didn't see the source, I hope the author sees forgiveness, it is inherited from DataGridColumnStyle

The original class is incorporated in an error while increasing the new row, the cause is not written to the Bind Manager (CurrencyManager)

The modified part I will listen out.

Reference:

1. A good FAQ about Winform.DataGrid

http://www.syncfusion.com/faq/winforms/faq_c44c.asp#q480q

2.codeProject and C #-Corner

Http://www.codeproject.com/cs/misctrl/datagridzen.asp?df=100&Forumid=31224&exp=0&select=715228

http://www.codeproject.com/cs/misctrl/rendatagridcomboBoxColumn.asp

http://www.c-sharpcorner.com/winforms/comboBoxindataGridskj.asp

3.msdn and 9cbs

Using system.data; using system.windows.form; using system.drawing;

namespace ComboBoxInDataGrid {///

/// Implementation of a ComboBox as a column in a DataGrid /// public class DataGridComboBoxColumn: DataGridColumnStyle {private int _xMargin = 1; private int _yMargin = 0; private ComboBox _comboBox Private string _oldValue = ""; private bool _inedit = false; private data _datable; private string _displaymember, _valuemember; // Add two member variables, save the data when the EDIT event occurs

Private currencymanager _cm; private int _irown

///

///// /// The name of the column /// The datasource what contains the look table /// The Member of the lookuptable to display /// The member of the lookuptable with the value /// The datagrid parent of this column public DataGridComboBoxColumn (string colName, DataTable dataSource, string displayMember, string valueMember, DataGrid dataGrid) {_comboBox = new ComboBox (); _comboBox.Visible = false; _comboBox.DataSource = dataSource; _dataTable = dataSource; _comboBox.DisplayMember = displayMember; _displayMember = displayMember; _valueMember = valueMember; _comboBox.ValueMember = valueMember; _comboBox.DropDownStyle = ComboBoxStyle.DropDownList; Graphics _GraphicsContext = DataGrid.creategraphics (); float _widest = 0; sizef _stringsize = new sizef (0, 0); foreach (DataRow dr in dataSource.Rows) {_stringSize = _graphicsContext.MeasureString (dr [displayMember] .ToString (), dataGrid.Font); if (_stringSize.Width> _widest) {_widest = _stringSize.Width;}} _comboBox.DropDownWidth = (int) Math.Ceiling (_widest); this.Width = _comboBox.DropDownWidth 25; // Add the space for the dropdown arrow this.MappingName = colName; this.HeaderText = colName; // adds a ComboBox.Leave Event, write the value to the binding management where this._comboBox.Leave = new eventhandler (_COMBOBOX_LEAVE);

DataGrid.controls.clear (); DataGrid.controls.add (_comboBox);} ///

/// standard override /// /// Protected override void Abort (int rownum) {_ companies = false; _comboBox.hide ();} /// /// standard override /// /// /// /// protected override bool commit (currencess) {if (! _ inedit) {return true DETURN TRUE } try {object _value = _comboBox.SelectedValue; if (NullText.Equals (_value)) {_value = System.Convert.DBNull;} this.SetColumnValueAtRow (dataSource, rowNum, _value);} catch {return false;} finally {_inEdit = false; _comboBox.hide ();} return true;} /// /// standard override /// /// /// /// /// /// /// protected override void Edit (CurrencyManager Source, Int Rownum, Rectangle Bounds, Bool Readonly, String InstantText, Bool Cellisvisible) {_comboBox.text = ""; Rectangle_ORIGINALBOUNDS = Bounds; _OLDVALUE = _COMBOX.TEXT;

// Record the binding manager and the line number

THIS._CM = Source; this._irownum = rownum;

IF (! cellisvisible) return;

bounds.Offset (_xMargin, _yMargin); bounds.Width - = _xMargin * 2; bounds.Height - = _yMargin; _comboBox.Bounds = bounds; _comboBox.Visible = true; // _ comboBox.BringToFront (); //_comboBox.Focus (); if (System.DBNull.Value == GetColumnValueAtRow (source, rowNum)) {MessageBox.Show ( "your cell is null point");} else {_comboBox.SelectedValue = GetText (GetColumnValueAtRow (source, rowNum )); If (instanttext! = Null) {_comboBox.selected; = instanttext; int end = _comboBox.text.length; _comboBox.select (end, 0);} else {_comboBox.selectall ();}} // this. DataGridtablesTyle.DataGrid.invalidate (OriginalBounds); // _ ComboBox.Bringtofront (); _ companies = true;

} ///

/// standard override /// /// protected override int getminimumheight () {return_comboBox.preferredheight _ymargin;} /// /// summary override /// /// /// /// Protected Override Int getPreferredHeight (Graphics g, object val) {return fontheight _ymargin;} /// /// standard override /// /// < / param> /// /// protected override size getpreferredsize (graphics g, object val) {size _extents = size.ceiling (g.measureString (GetText (Val), this.dataGridtablestyle.DataGrid.font); _extents.width = _xmargin * 2; _extents.height = _ymargin; return_extents;} /// /// standard override //// /// /// /// / // Protected Override Void Paint (Graphics G, Rectangle Bounds, CurrencyManager Source, Int Rownum) {Paint (g, bounds, source, rower, false);}

///

/// standard override /// /// /// // / /// /// protected override void Paint (Graphics G , Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight) {string _text = GetText (GetColumnValueAtRow (source, rowNum)); foreach (DataRow dr in _dataTable.Rows) {if (dr [_valueMember] .ToString () == _ text ) {_Text = DR [_displaymember] .tostring (); break;}} PaintText (g, bounds, _text, aligntery);}

// Helper functions private void PaintText (Graphics g, Rectangle bounds, string text, bool alignToRight) {Brush _backBrush = new SolidBrush (this.DataGridTableStyle.BackColor); Brush _foreBrush = new SolidBrush (this.DataGridTableStyle.ForeColor); Rectangle _rect = bounds; RectangleF _rectF = _rect; StringFormat _format = new StringFormat (); if (alignToRight) {_format.FormatFlags = StringFormatFlags.DirectionRightToLeft;} switch (this.Alignment) {case HorizontalAlignment.Left: _format.Alignment = StringAlignment.Near; break ; case HorizontalAlignment.Right: _format.Alignment = StringAlignment.Far; break; case HorizontalAlignment.Center: _format.Alignment = StringAlignment.Center; break;} _format.FormatFlags = StringFormatFlags.NoWrap; g.FillRectangle (_backBrush, _rect); _rect .Offset (0, _ymargin); _Rect.height - = _ymargin; g.drawstring (text, this.datagridtablelectyle.DataGrid.font, _Forebrush, _Rectf, _Format); _for Mat.dispose ();} private string gettext (object val) {if (val == system.dbnull.value) {return this.nullText;} if (val! = null) {Return Val.Tostring ();} else {Return String.empty;}}

// When the focus is left from ComboBox, write back the value in ComboBox back the binding manager and invisible ComboBox.

private void _comboBox_Leave (object sender, EventArgs e) {object objValue = this._comboBox.SelectedValue; if (objValue == null) {objValue = DBNull.Value;} this.SetColumnValueAtRow (this._cm, this._iRowNum, objValue); This._comboBox.visible = false;}}}

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

New Post(0)