DataGridView樣式--初級

DataGridView樣式--初級

 

vs2005為我們提供的樣式有以下6個,其中前3個只要綁定到相應字段類型就會自動產生,後3個則必須要手動設置綁定的數据 .除此之外,我們還可以自定義單元格樣式,及列樣式.


1.vs2005自帶提供的列樣式說明

--1.DataGridViewTextBoxColumn
與文字基礎的值搭配使用。當繫結至數字或字串時會自動產生。

在編輯模式中,會顯示一個TextBox在當前單元格中,用于修改單元格中的值。

單元格值會自動轉換為顯示的字串。會自動剖析使用者輸入或修改的值,以建立適當資料型別的單元格值。您可以通過處理 DataGridView 控制項的 CellFormatting 和 CellParsing 事件自定義這些轉換。

每列的單元格值資料型別可以在每列的 ValueType 屬性中加以指定,不過屬性視窗中好像沒有,要在代碼裡去設置.


--2.DataGridViewCheckBoxColumn
與 Boolean 和 CheckState 值搭配使用。當繫結至這些型別的值時會自動產生。

而 Boolean 值會依 ThreeState 屬性值而定,顯示為兩種狀態或三種狀態的核取方塊。當資料行繫結至 CheckState 值時,根據預設 ThreeState 屬性值會為 true。

通常核取方塊值的目的是為了儲存 (例如任何其他資料) 或執行大量作業。如果您想要在使用者按一下核取方塊單元格時立即回應,就可以處理 CellClick 事件,但這個事件是發生在更新單元格值之前。如果您在按一下的時候需要新的值,有一個選擇是根據目前的值計算需要的值。另一種方法是立即認可變更,並處理 CellValueChanged 事件以做出回應。若要在按一下單元格時認可變更,您必須處理 CurrentCellDirtyStateChanged 事件。在處理常式中,如果目前的單元格是核取方塊單元格,則呼叫 CommitEdit 方法,並傳入 Commit 值。

--3.DataGridViewImageColumn
用來顯示圖片。當繫結至位元組陣列、Image 物件或 Icon 物件時,便會自動產生。
資料來源的圖片資料行的自動填入能使用於各種圖片格式的位元組陣列,包括 Image 類別支援的所有格式,以及  Access 和 Northwind 範例資料庫的 OLE 圖片格式。

圖片數據除了可從資料來源自動填入外,也可以用手動方式為未繫結資料行填入,或通過 CellFormatting 事件的自定義填入。

您可以處理System.Windows.Forms.DataGridView.CellClick 事件以回應在圖片單元格內按選的動作。

當您想為計算值或非圖片格式的值提供圖片時,在 CellFormatting 事件的處理常式中填入圖片資料行的單元格,會是很有用的方式。例如,您可能有一個「風險」資料行,其中包含 "high"、"middle" 和 "low" 等您想要顯示為圖示的字串值。此外,也可能有「圖片」資料行,其中包含的是必須載入的圖片的位置,而非圖片的二進位內容。

--4.DataGridViewButtonColumn
用來顯示單元格中的按鈕。不會在繫結時自動產生。通常用來做為未繫結資料行。
它可以看成是DataGridViewImageColumn的一個樣例.

您可以透過處理 System.Windows.Forms.DataGridView.CellClick 事件,回應使用者在按鈕單元格中的按選動作。

--5.DataGridViewComboBoxColumn
用來顯示單元格中的下拉式清單。不會在繫結時自動產生。通常以手動方式繫結至資料。

您可以使用填入 ComboBox 下拉式清單的相同方式,填入用於所有單元格的下拉式清單:(1)一種方法是透過 Items 屬性傳回的集合,以手動方式進行,(2)另一種方法則是透過 DataSource、DisplayMember 和 ValueMember 屬性,將清單繫結至資料來源。

您可以繫結實際的單元格值至 DataGridView 控制項所使用的資料來源,方法是設定 System.Windows.Forms.DataGridViewComboBoxColumn 的 DataPropertyName 屬性。


--6.DataGridViewLinkColumn
用來顯示單元格中的連結。不會在繫結時自動產生。通常以手動方式繫結至資料。

您可以透過處理 CellContentClick 事件,回應使用者按選連結的動作。這個事件有別於 CellClick 和 CellMouseClick 事件,後兩者是發生在使用者按選單元格內的任何位置時。

DataGridViewLinkColumn 類別會提供數種屬性,可在按選連結前、按選連結的期間和按選連結後修改連結的外觀。

2.自訂列樣式
方法是先建立一個自定的DataGridViewCell,它衍生自 DataGridViewCell 基底類別 (Base Class) 或其中一個衍生類別.然後再繼承 DataGridViewColumn 類或其中一個衍生類,以提供列的自訂外觀、行為或裝載控制項.

以下是一個完整的示例,它將建立稱為 DataGridViewRolloverCell 的自訂單元格類別,此類別會偵測滑鼠何時進入或離開單元格界限。滑鼠位於單元格的界線內時,會描繪內凹方框。另做了個配套的列樣式稱為 DataGridViewRolloverColumn。

 

using  System;
using  System.Drawing;
using  System.Windows.Forms;

class  Form1 : Form
{
    [STAThreadAttribute()]
    
public   static   void  Main()
    {
        Application.Run(
new  Form1());
    }

    
public  Form1()
    {
        DataGridView dataGridView1 
=   new  DataGridView();
        DataGridViewRolloverCellColumn col 
=
            
new  DataGridViewRolloverCellColumn();
        dataGridView1.Columns.Add(col);
        dataGridView1.Rows.Add(
new   string [] {  ""  });
        dataGridView1.Rows.Add(
new   string [] {  ""  });
        dataGridView1.Rows.Add(
new   string [] {  ""  });
        dataGridView1.Rows.Add(
new   string [] {  ""  });
        
this .Controls.Add(dataGridView1);
        
this .Text  =   " DataGridView rollover-cell demo " ;
    }
}

public   class  DataGridViewRolloverCell : DataGridViewTextBoxCell
{
    
protected   override   void  Paint(
        Graphics graphics,
        Rectangle clipBounds,
        Rectangle cellBounds,
        
int  rowIndex,
        DataGridViewElementStates cellState,
        
object  value,
        
object  formattedValue,
        
string  errorText,
        DataGridViewCellStyle cellStyle,
        DataGridViewAdvancedBorderStyle advancedBorderStyle,
        DataGridViewPaintParts paintParts)
    {
        
//  Call the base class method to paint the default cell appearance.
         base .Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
            value, formattedValue, errorText, cellStyle,
            advancedBorderStyle, paintParts);

        
//  Retrieve the client location of the mouse pointer.
        Point cursorPosition  =
            
this .DataGridView.PointToClient(Cursor.Position);

        
//  If the mouse pointer is over the current cell, draw a custom border.
         if  (cellBounds.Contains(cursorPosition))
        {
            Rectangle newRect 
=   new  Rectangle(cellBounds.X  +   1 ,
                cellBounds.Y 
+   1 , cellBounds.Width  -   4 ,
                cellBounds.Height 
-   4 );
            graphics.DrawRectangle(Pens.Red, newRect);
        }
    }

    
//  Force the cell to repaint itself when the mouse pointer enters it.
     protected   override   void  OnMouseEnter( int  rowIndex)
    {
        
this .DataGridView.InvalidateCell( this );
    }

    
//  Force the cell to repaint itself when the mouse pointer leaves it.
     protected   override   void  OnMouseLeave( int  rowIndex)
    {
        
this .DataGridView.InvalidateCell( this );
    }

}

public   class  DataGridViewRolloverCellColumn : DataGridViewColumn
{
    
public  DataGridViewRolloverCellColumn()
    {
        
this .CellTemplate  =   new  DataGridViewRolloverCell();
    }
}

3.DataGridView的優化處理
如果需要顯示大量的數據,應按照以下的方法去做.
http://technet.microsoft.com/zh-tw/library/ha5xt0d9(VS.80).aspx


參考文獻:http://technet.microsoft.com/zh-tw/library/bxt3k60s(VS.80).aspx 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值