class DataGridComboBoxTableViewColumn: DataGridColumnStyle
{//與DataGridComboBoxColumn不同,顯示時使用DataTable,下拉時使用DataView
private ComboBox myComboBox = new ComboBox ();
private DataView ChoiceDataViewSource;
private DataView TableDataViewSource;
private string ChoiceDisplayField;
private string ChoiceValueField;
private bool isEditing;
public DataGridComboBoxTableViewColumn(DataView ChoiceDataViewSource,string ChoiceDisplayField,string ChoiceValueField) : base()
{
this.ChoiceDataViewSource =ChoiceDataViewSource;
TableDataViewSource=new System.Data .DataView (ChoiceDataViewSource.Table);
this.ChoiceDisplayField =ChoiceDisplayField;
this.ChoiceValueField =ChoiceValueField;
myComboBox.DropDownStyle =System.Windows .Forms .ComboBoxStyle .DropDownList ;
myComboBox.Visible = false;
myComboBox.DataSource =this.ChoiceDataViewSource;
myComboBox.DisplayMember= this.ChoiceDisplayField;
myComboBox.ValueMember =this.ChoiceValueField;
}
protected override void Abort(int rowNum)
{
isEditing = false;
myComboBox.SelectedIndexChanged -=
new EventHandler(ComboBoxSelectedIndexChanged );
Invalidate();
}
protected override bool Commit
(CurrencyManager dataSource, int rowNum)
{
myComboBox.Bounds = Rectangle.Empty;
myComboBox.SelectedIndexChanged -=
new EventHandler(ComboBoxSelectedIndexChanged );
if (!isEditing)
return true;
isEditing = false;
try
{
System.Object value = myComboBox.SelectedValue;
SetColumnValueAtRow(dataSource, rowNum, value);
}
catch (Exception)
{
Abort(rowNum);
return false;
}
Invalidate();
return true;
}
protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
object value =GetColumnValueAtRow(source, rowNum);
if (cellIsVisible)
{
myComboBox.Bounds = new Rectangle
(bounds.X + 2, bounds.Y + 2,
bounds.Width - 4, bounds.Height - 4);
int i= Find (value);
if (myComboBox.Items .Count >i)
{
myComboBox.SelectedIndex =i;
}
myComboBox.Visible = true;
myComboBox.SelectedIndexChanged +=
new EventHandler(ComboBoxSelectedIndexChanged );
}
else
{
myComboBox.SelectedIndex =this.Find (value);
myComboBox.Visible = false;
}
if (myComboBox.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);
}
protected int Find(System.Object value)
{
int int1;
int int2;
int2=TableDataViewSource.Count ;
if (int2<=0)
{
return -1;
}
for (int1=0;int1<int2;int1++)
{
if (TableDataViewSource[int1][this.myComboBox .ValueMember ].ToString ().Trim ()==value.ToString ().Trim ())
{
return int1;
}
}
return -1;
}
protected override Size GetPreferredSize(
Graphics g,
object value)
{
return new Size(100, myComboBox.PreferredHeight+ 4);
}
protected override int GetMinimumHeight()
{
return myComboBox.PreferredHeight + 4;
}
protected override int GetPreferredHeight(Graphics g,
object value)
{
return myComboBox.PreferredHeight + 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.TableDataViewSource [int1][this.myComboBox .DisplayMember ].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)
{
base.SetDataGridInColumn(value);
if (myComboBox.Parent != null)
{
myComboBox.Parent.Controls.Remove
(myComboBox);
}
if (value != null)
{
value.Controls.Add(myComboBox);
}
}
private void ComboBoxSelectedIndexChanged(object sender, EventArgs e)
{
this.isEditing = true;
base.ColumnStartedEditing(myComboBox);
}
}
Winform下的Datagrid的列風格(4)—DataGridComboBoxTableViewColumn
轉載於:https://www.cnblogs.com/yitian/archive/2008/09/12/1290000.html
相關文章
- 將OleDbDataAdapter繫結到Winform下的DataGrid (轉)APTORM
- C# winForm 建立水晶風格的按鈕C#ORM
- RESTful 架構風格下的 4 大常見安全問題REST架構
- 金庸筆下的良好程式碼風格
- 如何寫一個 GNU 風格的命令列程式命令列
- Winform下的畫板ORM
- 討論下 RESTful 風格 API 的路由設計RESTAPI路由
- 求求你規範下你的程式碼風格
- Henry手記:WinForm Datagrid結構剖析(二)程式 (轉)ORM
- 關於Easyui的datagrid的可編輯單元格和合並單元格UI
- Linux C 的風格Linux
- 程式碼的風格 (轉)
- easyui datagrid動態設定行、列、單元格不允許編輯UI
- WPF的DataGrid使用AutoGenerateColumns自動生成列的時候,控制列的寬度
- Linux 命令列引數的三種風格 Unix/Posix、BSD、GNULinux命令列
- 良好的HTML編碼風格HTML
- easyui datagrid 動態生成列UI
- 軟體架構風格——閉環架構風格(過程風格)架構
- 計算DataGrid中的模版列的結果,使用javascript.JavaScript
- 軟體架構風格——倉庫風格架構
- REST架構風格的由來REST架構
- 好的程式碼風格積累
- 中介軟體的型別:不同的風格型別
- eclipse下開發winform的外掛WindowBuilderEclipseORMUI
- [C++]C風格、C++風格和C++11特性的執行緒池C++執行緒
- 軟體架構風格——虛擬機器風格架構虛擬機
- 各種流行的程式設計風格程式設計
- 建立 SysV 風格的 linux daemon 程式Linux
- 定製不同風格的App主題APP
- REST架構風格的架構元素REST架構
- Restlet - REST架構風格的介紹REST架構
- 現代C++風格的新元素C++
- jquery easyui datagrid 動態隱藏列jQueryUI
- Datagrid擴充套件方法onClickCell{easyui-datagrid-擴充-支援單元格編輯}套件UI
- 關於構建REST風格的SOE的策略REST
- RESTful風格APIRESTAPI
- Bootstrap風格buttonboot
- C風格字串字串