using System;using System.Collections;using System.ComponentModel;using System.Drawing;using System.Windows.Forms;using System.Data;namespace Common...{ public class DataGridComboBoxColumnStyle: DataGridColumnStyle ...{ private int xMargin = 2; private int yMargin =1; private ComboBox Combo; private string _DisplayMember; private string _ValueMember; private string OldVal = String.Empty; private bool InEdit = false; public DataGridComboBoxColumnStyle(DataTable DataSource, int DisplayMember,int ValueMember) ...{ Combo = new ComboBox(); _DisplayMember = DataSource.Columns[DisplayMember].ToString(); _ValueMember = DataSource.Columns[ValueMember].ToString(); Combo.SelectedIndexChanged += new System.EventHandler(this.Combo_SelectedIndexChanged); Combo.Visible = false; Combo.DataSource = DataSource; Combo.DisplayMember = _DisplayMember; Combo.ValueMember = _ValueMember; } public DataGridComboBoxColumnStyle(DataTable DataSource, string DisplayMember,string ValueMember) ...{ Combo = new ComboBox(); Combo.SelectedIndexChanged += new System.EventHandler(this.Combo_SelectedIndexChanged); Combo.Visible = false; Combo.DataSource = DataSource; Combo.DisplayMember = DisplayMember; Combo.ValueMember = ValueMember; } protected override void Abort(int RowNum) ...{ RollBack(); HideComboBox(); EndEdit(); } protected override bool Commit(CurrencyManager DataSource, int RowNum) ...{ HideComboBox(); if(!InEdit) return true; try ...{ Object Value = Combo.SelectedValue; Object Text = Combo.SelectedText; if(Text.ToString()!=String.Empty) ...{ if(Value!=null) ...{ if(NullText.Equals(Value)) ...{ Value = Convert.DBNull; } SetColumnValueAtRow(DataSource, RowNum, Value); } else return true; } } catch(Exception e) ...{ RollBack(); return false; } EndEdit(); return true; } CurrencyManager cm; int rowNum; private void Combo_SelectedIndexChanged(object sender, System.EventArgs e) ...{ try ...{ if(this.InEdit) ...{ if(Combo!=null) ...{ Object Value = Combo.SelectedValue; if(Value!=null) ...{ if(NullText.Equals(Value)) ...{ Value = Convert.DBNull; } SetColumnValueAtRow(cm, rowNum, Value); } } } } catch( System.Exception ex) ...{ throw ex; } } protected override void ConcedeFocus() ...{ Combo.Visible = false; } protected override void Edit(CurrencyManager Source,int Rownum,Rectangle Bounds,bool ReadOnly,string InstantText, bool CellIsVisible) ...{ cm=Source; rowNum=Rownum; try ...{ Combo.Text = String.Empty; Rectangle OriginalBounds = Bounds; OldVal = Combo.Text; if(CellIsVisible) ...{ Bounds.Offset(xMargin, yMargin); Bounds.Width -= xMargin * 2; Bounds.Height -= yMargin; Combo.Bounds = Bounds; Combo.Visible = true; } else ...{ Combo.Bounds = OriginalBounds; Combo.Visible = false; } Combo.Text =GetText(GetColumnValueAtRow(Source, Rownum)); Combo.RightToLeft = this.DataGridTableStyle.DataGrid.RightToLeft; Combo.Focus(); if( Combo.Visible) DataGridTableStyle.DataGrid.Invalidate(OriginalBounds); InEdit = true; } catch(Exception ex) ...{ string m=ex.Message; } } protected override int GetMinimumHeight() ...{ return Combo.PreferredHeight + yMargin; } protected override int GetPreferredHeight(Graphics g, Object Value) ...{ int NewLineIndex = 0; int NewLines = 0; string ValueString = this.GetText(Value); while(NewLineIndex != -1) ...{ NewLineIndex = ValueString.IndexOf("r ", NewLineIndex + 1); NewLines += 1; } return FontHeight * NewLines + yMargin; } protected override Size GetPreferredSize(Graphics g, Object Value) ...{ Size Extents = Size.Ceiling(g.MeasureString(GetText(Value), this.DataGridTableStyle.DataGrid.Font)); Extents.Width += xMargin * 2 + DataGridTableGridLineWidth; Extents.Height += yMargin; return Extents; } protected override object GetColumnValueAtRow(System.Windows.Forms.CurrencyManager source, int rowNum) ...{ object s = base.GetColumnValueAtRow(source, rowNum); DataTable dv = (DataTable)Combo.DataSource; int rowCount = dv.Rows.Count; int i = 0; while (i < rowCount) ...{ if( s.ToString().Trim().Equals( dv.Rows[i][Combo.ValueMember].ToString().Trim())) break; ++i; } if(i < rowCount) return dv.Rows[i][Combo.DisplayMember]; return DBNull.Value; } protected override void Paint(Graphics g ,Rectangle Bounds,CurrencyManager Source,int RowNum) ...{ Paint(g, Bounds, Source, RowNum, false); } protected override void Paint(Graphics g ,Rectangle Bounds ,CurrencyManager Source ,int RowNum,bool AlignToRight) ...{ string Text = GetText(GetColumnValueAtRow(Source, RowNum)); PaintText(g, Bounds, Text, AlignToRight); } protected override void Paint(Graphics g ,Rectangle Bounds ,CurrencyManager Source,int RowNum,Brush BackBrush,Brush ForeBrush,bool AlignToRight) ...{ string Text = GetText(GetColumnValueAtRow(Source, RowNum)); PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight); } protected override void SetDataGridInColumn(DataGrid Value) ...{ base.SetDataGridInColumn(Value); if(Combo.Parent!=Value) ...{ if(Combo.Parent!=null) Combo.Parent.Controls.Remove(Combo); } if(Value !=null) Value.Controls.Add(Combo); } protected override void UpdateUI(CurrencyManager Source , int RowNum,string InstantText) ...{ Combo.Text = GetText(GetColumnValueAtRow(Source, RowNum)); if(InstantText!=null) Combo.Text = InstantText; } private int DataGridTableGridLineWidth ...{ get ...{ if(DataGridTableStyle.GridLineStyle == DataGridLineStyle.Solid) return 1; else return 0; } } private void EndEdit() ...{ InEdit = false; Invalidate(); } private string GetText(Object Value) ...{ if(Value==System.DBNull.Value) return NullText; if(Value!=null) return Value.ToString(); else return String.Empty; } private void HideComboBox() ...{ if( Combo.Focused) this.DataGridTableStyle.DataGrid.Focus(); Combo.Visible = false; } private void RollBack() ...{ Combo.Text = OldVal; } private void PaintText(Graphics g, Rectangle Bounds,string Text,bool AlignToRight) ...{ Brush BackBrush = new SolidBrush(DataGridTableStyle.BackColor); Brush ForeBrush = new SolidBrush(DataGridTableStyle.ForeColor); PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight); } private void PaintText(Graphics g , Rectangle TextBounds,string Text,Brush BackBrush,Brush ForeBrush,bool AlignToRight) ...{ Rectangle Rect = TextBounds; RectangleF RectF = (RectangleF)(Rect); StringFormat Format = new StringFormat(); if(AlignToRight) Format.FormatFlags = StringFormatFlags.DirectionRightToLeft; switch(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 = Format.FormatFlags | StringFormatFlags.NoWrap; g.FillRectangle(BackBrush, Rect); Rect.Offset(0, yMargin); Rect.Height -= yMargin; g.DrawString(Text, DataGridTableStyle.DataGrid.Font, ForeBrush, RectF, Format); Format.Dispose(); } }}