Winform下的Datagrid的列风格(2)—DataGridTextBindColumn
class DataGridTextBindColumn:DataGridColumnStyle
{
private DataView ChoiceDataViewSource;
private string ChoiceDisplayField;
private string ChoiceValueField;
public DataGridTextBindColumn(DataView ChoiceDataViewSource,string ChoiceDisplayField,string ChoiceValueField) : base()
{
this.ChoiceDataViewSource =ChoiceDataViewSource;
this.ChoiceDisplayField =ChoiceDisplayField;
this.ChoiceValueField =ChoiceValueField;
}
protected override void Abort(int rowNum)
{
Invalidate();
}
protected override bool Commit
(CurrencyManager dataSource, int rowNum)
{
Invalidate();
return true;
}
protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
}
protected int Find(System.Object value)
{
int int1;
int int2;
int2=ChoiceDataViewSource.Count ;
if (int2<=0)
{
return -1;
}
for (int1=0;int1<int2;int1++)
{
if (ChoiceDataViewSource[int1][ChoiceValueField ].ToString ().Trim ()==value.ToString ().Trim ())
{
return int1;
}
}
return -1;
}
protected override Size GetPreferredSize(
Graphics g,
object value)
{
return new Size(100, 4);
}
protected override int GetMinimumHeight()
{
return 4;
}
protected override int GetPreferredHeight(Graphics g,
object value)
{
return 4;
}
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)
{
Paint(
g,bounds,
source,
rowNum,
Brushes.Red,
Brushes.Blue,
alignToRight);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight)
{
object value=GetColumnValueAtRow(source, rowNum);
int int1=Find (value);
string display="";
if (int1>=0)
{
display=this.ChoiceDataViewSource [int1][ChoiceDisplayField].ToString ();
}
Rectangle rect = bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(0, 2);
rect.Height -= 2;
g.DrawString(display,
this.DataGridTableStyle.DataGrid.Font,
foreBrush, rect);
}
protected override void SetDataGridInColumn(DataGrid value)
{
}
}
{
private DataView ChoiceDataViewSource;
private string ChoiceDisplayField;
private string ChoiceValueField;
public DataGridTextBindColumn(DataView ChoiceDataViewSource,string ChoiceDisplayField,string ChoiceValueField) : base()
{
this.ChoiceDataViewSource =ChoiceDataViewSource;
this.ChoiceDisplayField =ChoiceDisplayField;
this.ChoiceValueField =ChoiceValueField;
}
protected override void Abort(int rowNum)
{
Invalidate();
}
protected override bool Commit
(CurrencyManager dataSource, int rowNum)
{
Invalidate();
return true;
}
protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
}
protected int Find(System.Object value)
{
int int1;
int int2;
int2=ChoiceDataViewSource.Count ;
if (int2<=0)
{
return -1;
}
for (int1=0;int1<int2;int1++)
{
if (ChoiceDataViewSource[int1][ChoiceValueField ].ToString ().Trim ()==value.ToString ().Trim ())
{
return int1;
}
}
return -1;
}
protected override Size GetPreferredSize(
Graphics g,
object value)
{
return new Size(100, 4);
}
protected override int GetMinimumHeight()
{
return 4;
}
protected override int GetPreferredHeight(Graphics g,
object value)
{
return 4;
}
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)
{
Paint(
g,bounds,
source,
rowNum,
Brushes.Red,
Brushes.Blue,
alignToRight);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight)
{
object value=GetColumnValueAtRow(source, rowNum);
int int1=Find (value);
string display="";
if (int1>=0)
{
display=this.ChoiceDataViewSource [int1][ChoiceDisplayField].ToString ();
}
Rectangle rect = bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(0, 2);
rect.Height -= 2;
g.DrawString(display,
this.DataGridTableStyle.DataGrid.Font,
foreBrush, rect);
}
protected override void SetDataGridInColumn(DataGrid value)
{
}
}