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
相關文章
- Winform下的畫板ORM
- WPF的DataGrid使用AutoGenerateColumns自動生成列的時候,控制列的寬度
- 如何寫一個 GNU 風格的命令列程式命令列
- 求求你規範下你的程式碼風格
- 討論下 RESTful 風格 API 的路由設計RESTAPI路由
- WPF --- 如何以Binding方式隱藏DataGrid列
- Linux 命令列引數的三種風格 Unix/Posix、BSD、GNULinux命令列
- 淺風的部落格
- WPF隨筆收錄-DataGrid固定右側列
- 子VN多年的風格
- WPF—DataGrid設定列的百分比寬度 & 列值顯示格式化
- 《風來之國》PC定檔9月16日,畫素風格下的多彩世界
- WPF datagrid mvvm multi select via customize datagridMVVM
- WPF DataGrid實現單擊單元格直接編輯
- 建立 SysV 風格的 linux daemon 程式Linux
- 中介軟體的型別:不同的風格型別
- 軟體架構風格——閉環架構風格(過程風格)架構
- 《FANTASIAN》Fami 通評測:在熟悉的「阪口風格」下體驗新的冒險
- 軟體架構風格——倉庫風格架構
- WinForm下實現子窗體ORM
- Android Dialog風格彈出框的ActivityAndroid
- 各種流行的程式設計風格程式設計
- 定製不同風格的App主題APP
- [C++]C風格、C++風格和C++11特性的執行緒池C++執行緒
- C# winform在高解析度下介面模糊的問題C#ORM
- jQueryEasyUI重寫datagrid的datetimebox編輯型別jQueryUI型別
- VuePress 手摸手教你搭建Vue風格的技術文件/部落格Vue
- 開源 react + koa + mysql 設計的簡約風格的個人部落格ReactMySql
- wpf datagrid樣式
- WPF DataGrid ItemsSource StaticResource
- 軟體架構風格——虛擬機器風格架構虛擬機
- 如何培養良好的程式設計風格程式設計
- Yii2.0 RESTful風格的Controller與ActiveControllerRESTController
- Go並不需要Java風格的GCGoJavaGC
- Bootstrap風格buttonboot
- Json風格指南JSON
- CSharp命名風格CSharp
- RESTful風格APIRESTAPI