DataGrid中ComboBox列样式控制
来源:互联网 发布:mac如何自动登录 编辑:程序博客网 时间:2024/05/18 02:54
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();
}
}
}
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();
}
}
}
- DataGrid中ComboBox列样式控制
- DataGrid中列绑定ComboBox
- WPF中DataGrid设置列头样式
- FLASH组件中List/ComboBox/DataGrid文本样式设置
- Datagrid 中添加ComboBox
- 控制Datagrid显示样式
- Silverlight中Datagrid控件自定义列头样式Demo
- Silverlight中Datagrid添加Button列用于控制单行对象
- WPF DataGrid中嵌入Combobox
- WPF中DataGrid放置combobox
- WPF中设置DataGrid中DataGridColumn列的普通样式和编辑样式
- Datagrid中设置样式
- 向DataGrid控件中添加ComboBox控件
- 在WinForm的DataGrid中加入ComboBox
- 在dataGrid中使用checkBox和ComboBox
- flex datagrid 中实现combobox联动
- Flex中DataGrid绑定(嵌入)ComboBox
- flex datagrid 中实现combobox联动
- MySQL入门学习
- C语言嵌入式系统编程修炼(背景篇) (转)
- Sybase参考手册 下载
- javascript打印web页面并设置打印机的实现
- C语言嵌入式系统编程修炼(软件架构篇)
- DataGrid中ComboBox列样式控制
- 微软收购以色列广告公司YaData
- 请教hpp与h的差别
- PCB板管理軟件,如何過帳比較好
- 常用的一些javascript小技巧(屏蔽)
- [转]C/C++ 误区一:void main()
- 调用委托后,资源占用哪个线程的? (一个例子)
- 网页常用小技巧(javascript)
- VC 多线程