DataGridComboBoxColumn控件
生活随笔
收集整理的這篇文章主要介紹了
DataGridComboBoxColumn控件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
控件
Datagrid是由多個表(table)組成的,table是由行(row)與列(column)組成的,行與列的交互,組成的一個個單元(cell)。我們的需要就是能控制每一個cell的變化,從而使row與column發生變化,最終形成table的變化。這每一種變化都可視為是Datagrid中table的一種風格格式(style)。
我們在往form上部署Datagrid控件后,會在其屬性窗口下方會出現“自動套用格式”,它們的變化多是背景色(Backcolor)與前景色(Forecolor)與字體(Font)的變化。經過本文的講述后,您將能夠實現更多的格式變化。
平常所看到的默認結構Datagrid,即把DatagridColumnStyle設定為DatagridTextBoxColumn列結構,把datagrid的列設為由textbox組成。從而我們就可以看到的那種效果就是每一個cell里都是一個textbox。同理,我們就知道如果把某一列的DatagridColumnStyle設定為DatagridBoolColumn列結構,就可以在該列中用checkbox控件顯示與更改boolean類型的值了。我們甚至可以自定義某一列的列類型,加入combox等等.。
要實現創建一個列類,應該很好地再次研究一下DataGridTextBoxColumn類,并據此相似的創建一個新類,本次示例是要創建一個具有下拉框的列,因此,它應該與DataGridTextBoxColumn類是同級的,也就是說它們應該是從同個父類派生出來的,這樣才可以在保證在使用上的相似性與一致性。
我們可以方便的了解到下拉框列的處理過程,在平常狀況下,colboBox列中的cell還是以textbox的形式進行顯示的,下拉框的出現是被textbox中落入焦點才激發的,而后,該cell就是一個實實在在的下拉框了,當焦點離開該cell后,cell又恢復成一個textbox的模樣了。
正式要開工了,先理一下思路,我們要做些什么事:
(1)?????從DataGridTextBoxColumn類的父類DataGridColumnStyle繼承一個自定義列類:DataGridComboBoxColumn;
(2)?????在列中加入一個ComboBox實例,用于聚焦后的出現,與DataGridTextBoxColumn類使用的textbox所屬的DataGridTextBox類相似的,我們應該設計一個專用的combobox類提供給DataGridComboBoxColumn類使用。
(3)?????跟蹤cell的狀態,當聚焦時在textbox的外面添加一個combobox,失去焦點后隱藏起combobox,恢復成textbox模樣;
(4)????重寫父類的Edit與Paint方法,以適應Combobox的使用,在Edit中將Combobox中產生的(用戶輸入或在下拉框中選擇)變化記錄入cell中,這也便于在更改后更新到相關的數據源中去。
現在來了解一下DataGridComboBox的運行機制。當第一次加載數據的時候,comboBox根據ValueMember綁定vaule,DisplayMember則綁定為text顯示給大家看。ComboBoxColumn的顯示則通過DataGrid的source和rowNum來決定,也就是將cell中的內容通過GetComboBoxText進行轉換然后paint顯示。為什么只需要source和rowNum來決定呢?那是因為DataGrid能夠自動記錄列號。
代碼如下:
DataGridComboBox.cs
using?System;
using?System.Windows.Forms;
namespace?DataGridComboBoxColumn
{
????///?<summary>
????///?DataGridComboBox?的摘要說明。
????///?</summary>
????public?class?DataGridComboBox:ComboBox
????{
????????private?const?int?WM_KEYUP?=?0x101;
????????protected?override?void?WndProc(ref?System.Windows.Forms.Message?message)?
????????{????????????
????????????if?(message.Msg?==?WM_KEYUP)?
????????????{
????????????????return;
????????????}
????????????base.WndProc(ref?message);
????????}?
????????//通過索引取得items的value。
????????public?string?GetValueText(int?index)
????????{
????????????if(index?<?0?||?index?>=?base.Items.Count)
????????????{
????????????????//拋出索引超出異常
????????????????throw?new?IndexOutOfRangeException("無效索引。");
????????????}
????????????else
????????????{
????????????????string?text?=?string.Empty;
????????????????int?memIndex?=??-1;
????????????????try
????????????????{
????????????????????base.BeginUpdate();
????????????????????memIndex?=?base.SelectedIndex;
????????????????????base.SelectedIndex?=?index;
????????????????????text?=?base.SelectedValue.ToString();
????????????????????base.SelectedIndex?=?memIndex;
????????????????}
????????????????catch
????????????????{}
????????????????finally
????????????????{
????????????????????base.EndUpdate();
????????????????}
????????????????return?text;
????????????}????????????
????????}
????????//通過索引取得items的text????????
????????public?string?GetDisplayText(int?index)
????????{
????????????if(index?<0?||?index?>=base.Items.Count)
????????????{
????????????????//拋出索引超出異常
????????????????throw?new?IndexOutOfRangeException("無效索引。");
????????????}
????????????else
????????????{
????????????????string?text?=?string.Empty;
????????????????int?memIndex?=?-1;
????????????????try
????????????????{
????????????????????base.BeginUpdate();
????????????????????memIndex?=?base.SelectedIndex;
????????????????????base.SelectedIndex?=?index;
????????????????????text?=?base.Text.ToString();
????????????????????base.SelectedIndex?=?memIndex;
????????????????}
????????????????catch
????????????????{}
????????????????finally
????????????????{
????????????????????base.EndUpdate();
????????????????}
????????????????return?text;
????????????}
????????}
????????//通過value取得items的text
????????public?string?GetDisplayText(object?value)
????????{????????????
????????????string?text?=?string.Empty;
????????????int?memIndex=?-1;
????????????try
????????????{
????????????????base.BeginUpdate();
????????????????memIndex?=?base.SelectedIndex;
????????????????base.SelectedValue?=?value.ToString();
????????????????text?=?base.Text.ToString();????????????????
????????????????base.SelectedIndex?=?memIndex;
????????????}
????????????catch
????????????{}
????????????finally
????????????{
????????????????base.EndUpdate();
????????????}
????????????return?text;
????????}
????????//循環獲取items的text
????????public?string[]?GetDisplayText()
????????{
????????????string[]?text?=?new?string[base.Items.Count];
????????????int?memIndex?=?-1;
????????????try
????????????{
????????????????base.BeginUpdate();
????????????????memIndex?=?base.SelectedIndex;
????????????????for(int?i=0;i<base.Items.Count;i++)
????????????????{
????????????????????base.SelectedIndex?=?i;
????????????????????text[i]?=?base.Text.ToString();????????????????????
????????????????}
????????????????base.SelectedIndex?=?memIndex;
????????????}
????????????catch
????????????{}
????????????finally
????????????{
????????????????base.EndUpdate();
????????????}
????????????return?text;
????????}
????}
}
DataGridComboBoxColumn.cs
using?System;
using?System.Drawing;
using?System.Collections;
using?System.ComponentModel;
using?System.Windows.Forms;
using?System.Data;
namespace?DataGridComboBoxColumn
{
????///?<summary>
????///?DataGridComboBoxColumn?的摘要說明。
????///?</summary>
????public?class?DataGridComboBoxColumn?:?DataGridColumnStyle
????{
????????private?DataGridComboBox?m_comboBox;
????????private?bool?m_edit;
????????public?DataGridComboBoxColumn()
????????{
????????????this.m_comboBox?=?new?DataGridComboBox();
????????????this.m_comboBox.Visible?=?false;
????????????this.m_comboBox.DropDownStyle?=?ComboBoxStyle.DropDownList;????????????
????????????this.m_comboBox.Leave?+=new?EventHandler(m_comboBox_Leave);
????????????this.m_comboBox.SelectionChangeCommitted?+=new?EventHandler(m_comboBox_SelectedIndexChanged);
????????????this.m_edit?=?false;
????????}
????????
????????public?ComboBox?comboBox
????????{
????????????get
????????????{
????????????????return?m_comboBox;
????????????}
????????}
????????//comboBox事件
????????//失去焦點后comboBox隱藏
????????private?void?m_comboBox_Leave(object?sender,EventArgs?e)
????????{
????????????this.m_comboBox.Hide();
????????}
????????//選定項發生更改并提交更改通知用戶開始編輯列
????????private?void?m_comboBox_SelectedIndexChanged(object?sender,EventArgs?e)
????????{
????????????this.m_edit?=?true;
????????????base.ColumnStartedEditing((Control)sender);
????????}
????????//重寫DataGridColunmStyle
????????protected?override?void?SetDataGridInColumn(DataGrid?value)
????????{
????????????//將comboBox加入到DataGrid控件集合中
????????????//確保正確的DataGrid?scrolling
????????????value.Controls.Add(this.m_comboBox);
????????????base.SetDataGridInColumn?(value);
????????}
????????//當?DataGridColumnStyle?方法的?Commit?方法返回?false?時,Abort?方法被?DataGrid?使用。
????????//在這種情況下,列值滾動回原先的值。
????????//在返回之前,DataGridColumnStyle?必須結束所有編輯操作。使用?Abort?方法來實現該操作。
????????//System.Windows.Forms.DataGrid?控件的?EndEdit?方法間接調用?Abort(如果其?ShouldAbort?參數設置為?true)。
????????protected?override?void?Abort(int?rowNum)
????????{
????????????this.m_edit=false;
????????????Invalidate();
????????????this.m_comboBox.Hide();
????????}
????????//準備單元格以便進行編輯。
????????protected?override?void?Edit(CurrencyManager?source,?int?rowNum,?System.Drawing.Rectangle?bounds,?bool?readOnly,?string?instantText,?bool?cellIsVisible)
????????{
????????????this.m_comboBox.Parent?=?this.DataGridTableStyle.DataGrid;
????????????this.m_comboBox.Bounds?=?bounds;
????????????this.m_comboBox.Size?=?new?System.Drawing.Size(this.Width,this.comboBox.Height);
????????????this.m_comboBox.SelectedValue?=?base.GetColumnValueAtRow(source,rowNum).ToString();
????????????this.m_comboBox.Visible?=?(!readOnly)?&&?cellIsVisible;
????????????this.m_comboBox.BringToFront();
????????????this.m_comboBox.Focus();
????????}
????????
????????//如果編輯過程成功提交,則為?true;否則為?false。
????????protected?override?bool?Commit(CurrencyManager?dataSource,?int?rowNum)
????????{
????????????if(this.m_edit==true)
????????????{
????????????????this.m_edit?=?false;
????????????????//保存值
????????????????this.SetColumnValueAtRow(dataSource,?rowNum,?this.m_comboBox.SelectedValue);
????????????}
????????????return?true;
????????}
????????//獲取指定?CurrencyManager?中指定行內的值。
????????protected?override?object?GetColumnValueAtRow(CurrencyManager?source,?int?rowNum)
????????{
????????????//return?base.GetColumnValueAtRow?(source,?rowNum);
????????????return?this.m_comboBox.GetDisplayText(base.GetColumnValueAtRow(source,rowNum));
????????}
????????//用來自指定?CurrencyManager?的值設置指定行中的值。
????????protected?override?void?SetColumnValueAtRow(CurrencyManager?source,?int?rowNum,?object?value)
????????{
????????????try
????????????{
????????????????base.SetColumnValueAtRow?(source,?rowNum,?value.ToString());
????????????????return;
????????????}
????????????catch
????????????{}
????????????//下面是另外一種方法,對于使用GUID全局唯一標識符有效
????????????try
????????????{
????????????????base.SetColumnValueAtRow?(source,?rowNum,?new?Guid(value.ToString()));
????????????????return;
????????????}
????????????catch
????????????{}
????????}
????????protected?override?int?GetMinimumHeight()
????????{
????????????return?this.m_comboBox.PreferredHeight+2;
????????}
????????????????????
????????//在派生類中被重寫時,將獲取自動調整列的大小所用的高度。
????????protected?override?int?GetPreferredHeight(System.Drawing.Graphics?g,?object?value)
????????{
????????????return?FontHeight?+?2?;
????????}
????????//在派生類中被重寫時,將獲取指定值的寬度和高度。
????????//在用戶定位到使用?DataGridColumnStyle?的?DataGridTableStyle?時將使用該寬度和高度。
????????protected?override?System.Drawing.Size?GetPreferredSize(System.Drawing.Graphics?g,?object?value)
????????{
????????????//return?new?System.Drawing.Size?();
????????????int?widths?=?0;
????????????SizeF?strF?=?new?SizeF(0,0);
????????????foreach?(string?str?in?this.m_comboBox.GetDisplayText())
????????????{
????????????????strF?=?g.MeasureString(str,?base.DataGridTableStyle.DataGrid.Font);
????????????????if(strF.Width?>?widths)
????????????????{
????????????????????widths?=?(int)Math.Ceiling(strF.Width);///
????????????????}
????????????}
????????????return?new?System.Drawing.Size?(widths?+25,this.m_comboBox.PreferredHeight+2);
????????}
????????//繪制具有指定?Graphics、Rectangle、CurrencyManager?和行號的?DataGridColumnStyle。
????????protected?override?void?Paint(System.Drawing.Graphics?g,?System.Drawing.Rectangle?bounds,?CurrencyManager?source,?int?rowNum)
????????{
????????????//繪制具有指定?Graphics、Rectangle、CurrencyManager、行號和對齊方式的?DataGridColumnStyle。
????????????Paint(g,?bounds,?source,?rowNum,?false);
????????}
????????//繪制具有指定?Graphics、Rectangle、CurrencyManager、行號、背景色、前景色和對齊方式的?DataGridColumnStyle。
????????protected?override?void?Paint(System.Drawing.Graphics?g,?System.Drawing.Rectangle?bounds,?CurrencyManager?source,?int?rowNum,?bool?alignToRight)
????????{
????????????//string?text?=?this.GetColumnValueAtRow(source,rowNum).ToString();
????????????string?text?=?GetColumnValueAtRow(source,rowNum).ToString();
????????????Brush?backBrush?=?new?SolidBrush(base.DataGridTableStyle.BackColor);
????????????Brush?foreBrush????=?new?SolidBrush(base.DataGridTableStyle.ForeColor);
????????????Rectangle?rect?=?bounds;
????????????StringFormat?format????=?new?StringFormat();
????????????//?單元格被選中
????????????if?(base.DataGridTableStyle.DataGrid.IsSelected(rowNum)?==?true)?
????????????{
????????????????backBrush?=?new?SolidBrush(base.DataGridTableStyle.SelectionBackColor);
????????????????foreBrush?=?new?SolidBrush(base.DataGridTableStyle.SelectionForeColor);
????????????}
????????????if?(alignToRight?==?true)?
????????????{
????????????????format.FormatFlags?=?StringFormatFlags.DirectionRightToLeft;
????????????}
????????????
????????????switch?(this.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;
????????????}
????????????//?Paint.
????????????format.FormatFlags?=?StringFormatFlags.NoWrap;
????????????g.FillRectangle(backBrush,?rect);
????????????rect.Offset(0,?0);
????????????rect.Height?=?0;
????????????g.DrawString(text,this.DataGridTableStyle.DataGrid.Font,?foreBrush,?rect,?format);
????????????format.Dispose();
????????}
????}
}
private?void?DispDgList()
private?void?DispDgList()
????????{
????????????//創建一個新的DataGrid表格樣式
????????????DataGridTableStyle?ts?=?new?DataGridTableStyle();
????????????ts.MappingName?=?this.DsBook.Tables[0].TableName;
????????????//創建一個ComboBox控件的列
????????????DataGridComboBoxColumn.DataGridComboBoxColumn?cboCol?=?null;
????????????//創建一個textBox控件的列
????????????DataGridTextBoxColumn?txtCol=?null;
????????????//計算列數
????????????int?numCols=this.DsBook.Tables[0].Columns.Count;
????????????for?(int?i=0;i<numCols;i++)
????????????{
????????????????if(this.DsBook.Tables[0].Columns[i].ColumnName.Equals("studentid"))
????????????????{
????????????????????cboCol?=?new?DataGridComboBoxColumn.DataGridComboBoxColumn();????????????????????
????????????????????cboCol.comboBox.DataSource?=?this.DsStudent.Tables[0];
????????????????????cboCol.comboBox.DisplayMember?=?"name";
????????????????????cboCol.comboBox.ValueMember?=?"studentid";
????????????????????cboCol.HeaderText?=?"student";
????????????????????cboCol.MappingName?=??"studentid";????????????????????
????????????????????ts.GridColumnStyles.Add(cboCol);
????????????????}
????????????????else
????????????????{
????????????????????txtCol?=?new?DataGridTextBoxColumn();
????????????????????txtCol.HeaderText?=?this.DsBook.Tables[0].Columns[i].ColumnName;
????????????????????txtCol.MappingName?=?this.DsBook.Tables[0].Columns[i].ColumnName;????
????????????????????ts.GridColumnStyles.Add(txtCol);
????????????????}
????????????}
????????????this.dataGrid1.TableStyles.Clear();
????????????this.dataGrid1.TableStyles.Add(ts);
????????????this.dataGrid1.DataSource?=?this.DsBook.Tables[0];
????????????this.dataGrid1.CaptionText?=?"book";
????????}
Datagrid是由多個表(table)組成的,table是由行(row)與列(column)組成的,行與列的交互,組成的一個個單元(cell)。我們的需要就是能控制每一個cell的變化,從而使row與column發生變化,最終形成table的變化。這每一種變化都可視為是Datagrid中table的一種風格格式(style)。
我們在往form上部署Datagrid控件后,會在其屬性窗口下方會出現“自動套用格式”,它們的變化多是背景色(Backcolor)與前景色(Forecolor)與字體(Font)的變化。經過本文的講述后,您將能夠實現更多的格式變化。
平常所看到的默認結構Datagrid,即把DatagridColumnStyle設定為DatagridTextBoxColumn列結構,把datagrid的列設為由textbox組成。從而我們就可以看到的那種效果就是每一個cell里都是一個textbox。同理,我們就知道如果把某一列的DatagridColumnStyle設定為DatagridBoolColumn列結構,就可以在該列中用checkbox控件顯示與更改boolean類型的值了。我們甚至可以自定義某一列的列類型,加入combox等等.。
要實現創建一個列類,應該很好地再次研究一下DataGridTextBoxColumn類,并據此相似的創建一個新類,本次示例是要創建一個具有下拉框的列,因此,它應該與DataGridTextBoxColumn類是同級的,也就是說它們應該是從同個父類派生出來的,這樣才可以在保證在使用上的相似性與一致性。
我們可以方便的了解到下拉框列的處理過程,在平常狀況下,colboBox列中的cell還是以textbox的形式進行顯示的,下拉框的出現是被textbox中落入焦點才激發的,而后,該cell就是一個實實在在的下拉框了,當焦點離開該cell后,cell又恢復成一個textbox的模樣了。
正式要開工了,先理一下思路,我們要做些什么事:
(1)?????從DataGridTextBoxColumn類的父類DataGridColumnStyle繼承一個自定義列類:DataGridComboBoxColumn;
(2)?????在列中加入一個ComboBox實例,用于聚焦后的出現,與DataGridTextBoxColumn類使用的textbox所屬的DataGridTextBox類相似的,我們應該設計一個專用的combobox類提供給DataGridComboBoxColumn類使用。
(3)?????跟蹤cell的狀態,當聚焦時在textbox的外面添加一個combobox,失去焦點后隱藏起combobox,恢復成textbox模樣;
(4)????重寫父類的Edit與Paint方法,以適應Combobox的使用,在Edit中將Combobox中產生的(用戶輸入或在下拉框中選擇)變化記錄入cell中,這也便于在更改后更新到相關的數據源中去。
現在來了解一下DataGridComboBox的運行機制。當第一次加載數據的時候,comboBox根據ValueMember綁定vaule,DisplayMember則綁定為text顯示給大家看。ComboBoxColumn的顯示則通過DataGrid的source和rowNum來決定,也就是將cell中的內容通過GetComboBoxText進行轉換然后paint顯示。為什么只需要source和rowNum來決定呢?那是因為DataGrid能夠自動記錄列號。
代碼如下:
DataGridComboBox.cs
| 程序代碼: | [ 復制代碼 ] [ 運行代碼 ] |
using?System.Windows.Forms;
namespace?DataGridComboBoxColumn
{
????///?<summary>
????///?DataGridComboBox?的摘要說明。
????///?</summary>
????public?class?DataGridComboBox:ComboBox
????{
????????private?const?int?WM_KEYUP?=?0x101;
????????protected?override?void?WndProc(ref?System.Windows.Forms.Message?message)?
????????{????????????
????????????if?(message.Msg?==?WM_KEYUP)?
????????????{
????????????????return;
????????????}
????????????base.WndProc(ref?message);
????????}?
????????//通過索引取得items的value。
????????public?string?GetValueText(int?index)
????????{
????????????if(index?<?0?||?index?>=?base.Items.Count)
????????????{
????????????????//拋出索引超出異常
????????????????throw?new?IndexOutOfRangeException("無效索引。");
????????????}
????????????else
????????????{
????????????????string?text?=?string.Empty;
????????????????int?memIndex?=??-1;
????????????????try
????????????????{
????????????????????base.BeginUpdate();
????????????????????memIndex?=?base.SelectedIndex;
????????????????????base.SelectedIndex?=?index;
????????????????????text?=?base.SelectedValue.ToString();
????????????????????base.SelectedIndex?=?memIndex;
????????????????}
????????????????catch
????????????????{}
????????????????finally
????????????????{
????????????????????base.EndUpdate();
????????????????}
????????????????return?text;
????????????}????????????
????????}
????????//通過索引取得items的text????????
????????public?string?GetDisplayText(int?index)
????????{
????????????if(index?<0?||?index?>=base.Items.Count)
????????????{
????????????????//拋出索引超出異常
????????????????throw?new?IndexOutOfRangeException("無效索引。");
????????????}
????????????else
????????????{
????????????????string?text?=?string.Empty;
????????????????int?memIndex?=?-1;
????????????????try
????????????????{
????????????????????base.BeginUpdate();
????????????????????memIndex?=?base.SelectedIndex;
????????????????????base.SelectedIndex?=?index;
????????????????????text?=?base.Text.ToString();
????????????????????base.SelectedIndex?=?memIndex;
????????????????}
????????????????catch
????????????????{}
????????????????finally
????????????????{
????????????????????base.EndUpdate();
????????????????}
????????????????return?text;
????????????}
????????}
????????//通過value取得items的text
????????public?string?GetDisplayText(object?value)
????????{????????????
????????????string?text?=?string.Empty;
????????????int?memIndex=?-1;
????????????try
????????????{
????????????????base.BeginUpdate();
????????????????memIndex?=?base.SelectedIndex;
????????????????base.SelectedValue?=?value.ToString();
????????????????text?=?base.Text.ToString();????????????????
????????????????base.SelectedIndex?=?memIndex;
????????????}
????????????catch
????????????{}
????????????finally
????????????{
????????????????base.EndUpdate();
????????????}
????????????return?text;
????????}
????????//循環獲取items的text
????????public?string[]?GetDisplayText()
????????{
????????????string[]?text?=?new?string[base.Items.Count];
????????????int?memIndex?=?-1;
????????????try
????????????{
????????????????base.BeginUpdate();
????????????????memIndex?=?base.SelectedIndex;
????????????????for(int?i=0;i<base.Items.Count;i++)
????????????????{
????????????????????base.SelectedIndex?=?i;
????????????????????text[i]?=?base.Text.ToString();????????????????????
????????????????}
????????????????base.SelectedIndex?=?memIndex;
????????????}
????????????catch
????????????{}
????????????finally
????????????{
????????????????base.EndUpdate();
????????????}
????????????return?text;
????????}
????}
}
DataGridComboBoxColumn.cs
| 程序代碼: | [ 復制代碼 ] [ 運行代碼 ] |
using?System.Drawing;
using?System.Collections;
using?System.ComponentModel;
using?System.Windows.Forms;
using?System.Data;
namespace?DataGridComboBoxColumn
{
????///?<summary>
????///?DataGridComboBoxColumn?的摘要說明。
????///?</summary>
????public?class?DataGridComboBoxColumn?:?DataGridColumnStyle
????{
????????private?DataGridComboBox?m_comboBox;
????????private?bool?m_edit;
????????public?DataGridComboBoxColumn()
????????{
????????????this.m_comboBox?=?new?DataGridComboBox();
????????????this.m_comboBox.Visible?=?false;
????????????this.m_comboBox.DropDownStyle?=?ComboBoxStyle.DropDownList;????????????
????????????this.m_comboBox.Leave?+=new?EventHandler(m_comboBox_Leave);
????????????this.m_comboBox.SelectionChangeCommitted?+=new?EventHandler(m_comboBox_SelectedIndexChanged);
????????????this.m_edit?=?false;
????????}
????????
????????public?ComboBox?comboBox
????????{
????????????get
????????????{
????????????????return?m_comboBox;
????????????}
????????}
????????//comboBox事件
????????//失去焦點后comboBox隱藏
????????private?void?m_comboBox_Leave(object?sender,EventArgs?e)
????????{
????????????this.m_comboBox.Hide();
????????}
????????//選定項發生更改并提交更改通知用戶開始編輯列
????????private?void?m_comboBox_SelectedIndexChanged(object?sender,EventArgs?e)
????????{
????????????this.m_edit?=?true;
????????????base.ColumnStartedEditing((Control)sender);
????????}
????????//重寫DataGridColunmStyle
????????protected?override?void?SetDataGridInColumn(DataGrid?value)
????????{
????????????//將comboBox加入到DataGrid控件集合中
????????????//確保正確的DataGrid?scrolling
????????????value.Controls.Add(this.m_comboBox);
????????????base.SetDataGridInColumn?(value);
????????}
????????//當?DataGridColumnStyle?方法的?Commit?方法返回?false?時,Abort?方法被?DataGrid?使用。
????????//在這種情況下,列值滾動回原先的值。
????????//在返回之前,DataGridColumnStyle?必須結束所有編輯操作。使用?Abort?方法來實現該操作。
????????//System.Windows.Forms.DataGrid?控件的?EndEdit?方法間接調用?Abort(如果其?ShouldAbort?參數設置為?true)。
????????protected?override?void?Abort(int?rowNum)
????????{
????????????this.m_edit=false;
????????????Invalidate();
????????????this.m_comboBox.Hide();
????????}
????????//準備單元格以便進行編輯。
????????protected?override?void?Edit(CurrencyManager?source,?int?rowNum,?System.Drawing.Rectangle?bounds,?bool?readOnly,?string?instantText,?bool?cellIsVisible)
????????{
????????????this.m_comboBox.Parent?=?this.DataGridTableStyle.DataGrid;
????????????this.m_comboBox.Bounds?=?bounds;
????????????this.m_comboBox.Size?=?new?System.Drawing.Size(this.Width,this.comboBox.Height);
????????????this.m_comboBox.SelectedValue?=?base.GetColumnValueAtRow(source,rowNum).ToString();
????????????this.m_comboBox.Visible?=?(!readOnly)?&&?cellIsVisible;
????????????this.m_comboBox.BringToFront();
????????????this.m_comboBox.Focus();
????????}
????????
????????//如果編輯過程成功提交,則為?true;否則為?false。
????????protected?override?bool?Commit(CurrencyManager?dataSource,?int?rowNum)
????????{
????????????if(this.m_edit==true)
????????????{
????????????????this.m_edit?=?false;
????????????????//保存值
????????????????this.SetColumnValueAtRow(dataSource,?rowNum,?this.m_comboBox.SelectedValue);
????????????}
????????????return?true;
????????}
????????//獲取指定?CurrencyManager?中指定行內的值。
????????protected?override?object?GetColumnValueAtRow(CurrencyManager?source,?int?rowNum)
????????{
????????????//return?base.GetColumnValueAtRow?(source,?rowNum);
????????????return?this.m_comboBox.GetDisplayText(base.GetColumnValueAtRow(source,rowNum));
????????}
????????//用來自指定?CurrencyManager?的值設置指定行中的值。
????????protected?override?void?SetColumnValueAtRow(CurrencyManager?source,?int?rowNum,?object?value)
????????{
????????????try
????????????{
????????????????base.SetColumnValueAtRow?(source,?rowNum,?value.ToString());
????????????????return;
????????????}
????????????catch
????????????{}
????????????//下面是另外一種方法,對于使用GUID全局唯一標識符有效
????????????try
????????????{
????????????????base.SetColumnValueAtRow?(source,?rowNum,?new?Guid(value.ToString()));
????????????????return;
????????????}
????????????catch
????????????{}
????????}
????????protected?override?int?GetMinimumHeight()
????????{
????????????return?this.m_comboBox.PreferredHeight+2;
????????}
????????????????????
????????//在派生類中被重寫時,將獲取自動調整列的大小所用的高度。
????????protected?override?int?GetPreferredHeight(System.Drawing.Graphics?g,?object?value)
????????{
????????????return?FontHeight?+?2?;
????????}
????????//在派生類中被重寫時,將獲取指定值的寬度和高度。
????????//在用戶定位到使用?DataGridColumnStyle?的?DataGridTableStyle?時將使用該寬度和高度。
????????protected?override?System.Drawing.Size?GetPreferredSize(System.Drawing.Graphics?g,?object?value)
????????{
????????????//return?new?System.Drawing.Size?();
????????????int?widths?=?0;
????????????SizeF?strF?=?new?SizeF(0,0);
????????????foreach?(string?str?in?this.m_comboBox.GetDisplayText())
????????????{
????????????????strF?=?g.MeasureString(str,?base.DataGridTableStyle.DataGrid.Font);
????????????????if(strF.Width?>?widths)
????????????????{
????????????????????widths?=?(int)Math.Ceiling(strF.Width);///
????????????????}
????????????}
????????????return?new?System.Drawing.Size?(widths?+25,this.m_comboBox.PreferredHeight+2);
????????}
????????//繪制具有指定?Graphics、Rectangle、CurrencyManager?和行號的?DataGridColumnStyle。
????????protected?override?void?Paint(System.Drawing.Graphics?g,?System.Drawing.Rectangle?bounds,?CurrencyManager?source,?int?rowNum)
????????{
????????????//繪制具有指定?Graphics、Rectangle、CurrencyManager、行號和對齊方式的?DataGridColumnStyle。
????????????Paint(g,?bounds,?source,?rowNum,?false);
????????}
????????//繪制具有指定?Graphics、Rectangle、CurrencyManager、行號、背景色、前景色和對齊方式的?DataGridColumnStyle。
????????protected?override?void?Paint(System.Drawing.Graphics?g,?System.Drawing.Rectangle?bounds,?CurrencyManager?source,?int?rowNum,?bool?alignToRight)
????????{
????????????//string?text?=?this.GetColumnValueAtRow(source,rowNum).ToString();
????????????string?text?=?GetColumnValueAtRow(source,rowNum).ToString();
????????????Brush?backBrush?=?new?SolidBrush(base.DataGridTableStyle.BackColor);
????????????Brush?foreBrush????=?new?SolidBrush(base.DataGridTableStyle.ForeColor);
????????????Rectangle?rect?=?bounds;
????????????StringFormat?format????=?new?StringFormat();
????????????//?單元格被選中
????????????if?(base.DataGridTableStyle.DataGrid.IsSelected(rowNum)?==?true)?
????????????{
????????????????backBrush?=?new?SolidBrush(base.DataGridTableStyle.SelectionBackColor);
????????????????foreBrush?=?new?SolidBrush(base.DataGridTableStyle.SelectionForeColor);
????????????}
????????????if?(alignToRight?==?true)?
????????????{
????????????????format.FormatFlags?=?StringFormatFlags.DirectionRightToLeft;
????????????}
????????????
????????????switch?(this.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;
????????????}
????????????//?Paint.
????????????format.FormatFlags?=?StringFormatFlags.NoWrap;
????????????g.FillRectangle(backBrush,?rect);
????????????rect.Offset(0,?0);
????????????rect.Height?=?0;
????????????g.DrawString(text,this.DataGridTableStyle.DataGrid.Font,?foreBrush,?rect,?format);
????????????format.Dispose();
????????}
????}
}
private?void?DispDgList()
| 程序代碼: | [ 復制代碼 ] [ 運行代碼 ] |
????????{
????????????//創建一個新的DataGrid表格樣式
????????????DataGridTableStyle?ts?=?new?DataGridTableStyle();
????????????ts.MappingName?=?this.DsBook.Tables[0].TableName;
????????????//創建一個ComboBox控件的列
????????????DataGridComboBoxColumn.DataGridComboBoxColumn?cboCol?=?null;
????????????//創建一個textBox控件的列
????????????DataGridTextBoxColumn?txtCol=?null;
????????????//計算列數
????????????int?numCols=this.DsBook.Tables[0].Columns.Count;
????????????for?(int?i=0;i<numCols;i++)
????????????{
????????????????if(this.DsBook.Tables[0].Columns[i].ColumnName.Equals("studentid"))
????????????????{
????????????????????cboCol?=?new?DataGridComboBoxColumn.DataGridComboBoxColumn();????????????????????
????????????????????cboCol.comboBox.DataSource?=?this.DsStudent.Tables[0];
????????????????????cboCol.comboBox.DisplayMember?=?"name";
????????????????????cboCol.comboBox.ValueMember?=?"studentid";
????????????????????cboCol.HeaderText?=?"student";
????????????????????cboCol.MappingName?=??"studentid";????????????????????
????????????????????ts.GridColumnStyles.Add(cboCol);
????????????????}
????????????????else
????????????????{
????????????????????txtCol?=?new?DataGridTextBoxColumn();
????????????????????txtCol.HeaderText?=?this.DsBook.Tables[0].Columns[i].ColumnName;
????????????????????txtCol.MappingName?=?this.DsBook.Tables[0].Columns[i].ColumnName;????
????????????????????ts.GridColumnStyles.Add(txtCol);
????????????????}
????????????}
????????????this.dataGrid1.TableStyles.Clear();
????????????this.dataGrid1.TableStyles.Add(ts);
????????????this.dataGrid1.DataSource?=?this.DsBook.Tables[0];
????????????this.dataGrid1.CaptionText?=?"book";
????????}
?
?
轉載于:https://www.cnblogs.com/ami/archive/2006/07/20/455360.html
總結
以上是生活随笔為你收集整理的DataGridComboBoxColumn控件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP实现简单文件上传系统
- 下一篇: struts2+hibernate+sp