轉帖|其它|編輯:郝浩|2010-12-02 11:37:17.000|閱讀 2530 次
概述:本文主要是對DataGridView的經典用法進行了總結,希望對大家有幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
一、DataGridView 單元格驗證
比如只允許輸入數字
要求:驗證錯誤后焦點不離開。
有兩種方法:
DataGridView.EditingControlShowing 事件和DataGridView.CellValidating 事件。
(1) DataGridView.EditingControlShowing 事件。
顯示用于編輯單元格的控件時發生,命名空間: System.Windows.Forms
程序集: System.Windows.Forms(在 system.windows.forms.dll 中)。
如:
void dgvCs_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.CellStyle.BackColor = Color.Aquamarine;//設置編譯時的顏色
control = new TextBox();
control = (TextBox)e.Control;
control.KeyPress += new KeyPressEventHandler(txt_KeyPress);//
}
然后在txt_KeyPress這里進行驗證。
(2) DataGridView.CellValidating 事件。
在單元格失去輸入焦點時發生,并啟用內容驗證功能。命名空間: System.Windows.Form,程序集: System.Windows.Forms(在 System.Windows
.Forms.dll 中)
備注:
驗證不通過時調用e.Cancel = true,終止事件鏈,單元格將保持編輯狀態。
調用dgv_details.CancelEdit();可以使單元格的內容會滾到修改前的值。
使用System.Windows.Forms.SendKeys.Send("^a");將全選單元格的內容。
如:
void dgv_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
decimal tmp = 0.0m;
if (!decimal.TryParse(e.FormattedValue.ToString(), out tmp))//是否是數字
{
if (e.FormattedValue != null && e.FormattedValue.ToString().Length != 0)
{
DevComponents.DotNetBar.MessageBoxEx.Show("請輸入有效數字!", "提示");
e.Cancel = true;
}
}
}
這兩種方法都能驗證。第一種方法當按鍵按下時(即當編譯時)就去驗證,而第二種方法是當焦點離開單元格編譯區域時觸發。所以個人感覺第一種方法更優一點。
二、指定選中單元格并開始編輯狀態
實現:
//獲得焦點
DataGridView.Focus();
//指定當前單元格
DataGridView.CurrentCell = dgv_details[0, 0]; []中對應參數為列索引(或列標題)、行索引。(注意:不是默認的先行索引)
//開始編輯狀態
d DataGridView.BeginEdit(false);false是指對指定行進行編輯。
DataGridView.BeginEdit 方法 嘗試將網格置于允許編輯的狀態。
命名空間: System.Windows.Forms
程序集: System.Windows.Forms(在 System.Windows.Forms.dll 中)
三、在拖動列的滾動條時可以將指定的列凍結
this.dataGridView1.Columns["AddToCartButton"].Frozen = true;
說明:中括號([])中指相應列的索引或者相應列的標題
這個知道了后一看就應該明白,無需多加解釋。
四、DataGridView選擇的部分拷貝至剪貼板
拷貝模式設定
DataGridView1.ClipboardCopyMode= DataGridViewClipboardCopyMode.EnableWithoutHeaderText //設置可復制的模式
其中DataGridView.ClipboardCopyMode 屬性獲取或設置一個值,該值指示用戶是否可以將單元格的文本值復制到 Clipboard,以及是否包括行標題和列標題文本。
命名空間: System.Windows.Forms
程序集: System.Windows.Forms(在 System.Windows.Forms.dll 中)
選中部分拷貝
Clipboard.SetDataObject(DataGridView1.GetClipboardContent()) //將控件選中的數據置于系統剪貼板中
DataGridView粘貼
代碼
if (DataGridView1.CurrentCell.Value == null)
{
return;
}
int insertRowIndex = DataGridView1.CurrentCell.RowIndex;
string pasteText=Clipboard.GetText();//從系統剪貼板中獲取數據
if(string.IsNullOrEmpty(pasteText))
{
return;
}
string[] lines=pasteText.Split('\r');//按行分組
bool isHeader=true;
foreach(string line in lines)
{
if(isHeader)
{
isHeader=false;//當可復制模式中含有標題時的過濾操作
}
else
{
string[] vals=line.Split('\t');//按tab空格分組
if (vals.Length - 1 != DataGridView1.ColumnCount)
{
throw new ApplicationException("列數錯誤");
}
DataGridViewRow row = DataGridView1.Rows[insertRowIndex];
row.HeaderCell.Value=vals[0];
for(int i=0;i<row.Cells.Count-1;i++)
{
row.Cells[i].Value=vals[(i+1)];
}
insertRowIndex+=1;
}
}
五、DatagridView自動編號
代碼
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
//自動編號與數據庫無關
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y
,dataGridView1.RowHeadersWidth - 4,e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),dataGridView1.
RowHeadersDefaultCellStyle.Font, rectangle,
dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
顯示行號
六、 指定單元格屬性
DataGridViewCell dgcell = new DataGridViewTextBoxCell();//申明單元格類型
this.dgvCss.Rows[i].Cells[k] = dgcell;
其實很簡單,只是很多人不知道有這個屬性。但這種方式和帶有復選框類型的單元格使用時,一般情況下出錯,也給一些人一種錯覺以為單元格類型不能這么操作。其實是因為你在申明列的時候先申明的checkbox類型的,而這時他們便有一個默認值(FormattedValue)false,當你重新給他賦值單元格類型時便會出現FormattedValue錯誤,這時你只需給它一個默認值就可以,如
this.dgvCss.Rows[i].Cells[k].Value = ""; 這樣便可解決。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:博客轉載