程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> DataGridView新特色(vs2005)

DataGridView新特色(vs2005)

編輯:.NET實例教程
1、自定義列
   Customize Cells and Columns in the Windows Forms DataGridVIEw Control by Extending Their

    Behavior and Appearance
    Host Controls in Windows Forms DataGridVIEw Cells
    繼承 DataGridViewTextBoxCell 類生成新的Cell類,然後再繼承 DataGridVIEwColumn 生成新的Column類,並指定
    CellTemplate為新的Cell類。新生成的Column便可以增加到DataGridVIEw中去。
2、自動適應列寬
    Programmatically Resize Cells to Fit Content in the Windows Forms DataGridVIEw Control
    Samples:
    DataGridVIEw.AutoSizeColumns(
           DataGridVIEwAutoSizeColumnCriteria.HeaderAndDisplayedRows);
    DataGridVIEw.AutoSizeColumn(
                DataGridVIEwAutoSizeColumnCriteria.HeaderOnly,
                2, false);
    DataGridVIEw.AutoSizeRow(
                DataGridVIEwAutoSizeRowCriteria.Columns,
                2, false);
    

: #ffff66">DataGridVIEw.AutoSizeRows(
              DataGridVIEwAutoSizeRowCriteria.HeaderAndColumns,
                0, dataGridVIEw1.Rows.Count, false);
3、可以綁定並顯示對象
    Bind Objects to Windows Forms DataGridVIEw Controls
4、可以改變表格線條風格
    Change the Border and Gridline Styles in the Windows Forms DataGridVIEw Control
    Samples:
    this.dataGridVIEw1.GridColor = Color.BlueViolet;
    this.dataGridVIEw1.BorderStyle = BorderStyle.Fixed3D;
    this.dataGridVIEw1.CellBorderStyle = DataGridVIEwCellBorderStyle.None;
    this.dataGridVIEw1.RowHeadersBorderStyle = DataGridVIEwHeaderBorderStyle.Single;
    this.dataGridVIEw1.ColumnHeadersBorderStyle = DataGridVIEwHeaderBorderStyle.Single;
5、動態改變列是否顯示,和動態改變列的顯示順序
    Change the Order of the Columns in the Windows Forms DataGridVIEw Control
    Samples:
    customersDataGridVIEw.Columns["CustomerID"].Visible = false;

t size="2">    customersDataGridVIEw.Columns["ContactName"].DisplayIndex = 0;
    customersDataGridVIEw.Columns["ContactTitle"].DisplayIndex = 1;
    customersDataGridVIEw.Columns["City"].DisplayIndex = 2;
    customersDataGridVIEw.Columns["Country"].DisplayIndex = 3;
    customersDataGridVIEw.Columns["CompanyName"].DisplayIndex = 4;
6、可以在列中顯示圖像
    Display Images in Cells of the Windows Forms DataGridVIEw Control
    Samples:
            Icon treeIcon = new Icon(this.GetType(), "tree.ico");
    DataGridViewImageColumn iconColumn = new DataGridVIEwImageColumn ();
           iconColumn.Image = treeIcon.ToBitmap();
           iconColumn.Name = "Tree";
           iconColumn.HeaderText = "Nice tree";
           dataGridVIEw1.Columns.Insert(2, iconColumn);
7、格式化顯示內容:
    Format Data in the Windows Forms DataGridVIEw Control
    Samples:
    this.dataGridVIEw1.Columns["UnitPrice"].DefaultCellStyle.Format = "c";
    this.fff66">dataGridVIEw1.Columns["ShipDate"].DefaultCellStyle.Format = "d";
    this.dataGridVIEw1.DefaultCellStyle.NullValue = "no entry";
    this.dataGridVIEw1.DefaultCellStyle.WrapMode = DataGridVIEwWrapMode.Wrap;
    this.dataGridVIEw1.Columns["CustomerName"].DefaultCellStyle.Alignment =
                         DataGridVIEwContentAlignment.MiddleRight;
   
8、在拖動列的滾動條時可以將指定的列凍結
    Freeze Columns in the Windows Forms DataGridVIEw Control
    Samples:將指定列及以前的列固定不動
    this.dataGridVIEw1.Columns["AddToCartButton"].Frozen = true;
9、獲取選擇的單元格,行,列
    Get the Selected Cells, Rows, and Columns in the Windows Forms DataGridVIEw Control
    Samples:
       見msdn。
10、顯示錄入時出現的錯誤信息
    Handle Errors that Occur During Data Entry in the Windows Forms DataGridVIEw Control
    Samples:
    private void dataGridVIEw1_DataError(object sender,


        DataGridVIEwDataErrorEventArgs e)
        {
        // If the data source raises an exception when a cell value is
        // commited, display an error message.
        if (e.Exception != null &&
            e.Context == DataGridVIEwDataErrorContext.Commit)
        {
            MessageBox.Show("CustomerID value must be unique.");
        }
        }
11、大數據量顯示采用Virtual Mode
    Implement Virtual Mode in the Windows Forms DataGridVIEw Control
12、設置指定的列只讀
    Make Columns in the Windows Forms DataGridVIEw Control Read-Only
    Samples:
    dataGridVIEw1.Columns["CompanyName"].ReadOnly = true;
 
13、移去自動生成的列
    Remove Autogenerated Columns from a Windows Forms DataGridVIEw Control
    Sample:
-COLOR: #ffff66">dataGridVIEw1.AutoGenerateColumns = true;
   

lack; BACKGROUND-COLOR: #ffff66">dataGridVIEw1.DataSource = customerDataSet;
    dataGridVIEw1.Columns.Remove ("Fax");
    或:
    dataGridVIEw1.Columns["CustomerID"].Visible = false;
14、自定義選擇模式
    Set the Selection Mode of the Windows Forms DataGridVIEw Control
    Sample:
    this.dataGridVIEw1.SelectionMode = DataGridVIEwSelectionMode.FullRowSelect;
    this.dataGridVIEw1.MultiSelect = false;
15、自定義設定光標進入單元格是否編輯模式(編輯模式)
    Specify the Edit Mode for the Windows Forms DataGridVIEw Control
    this.dataGridVIEw1.EditMode = DataGridVIEwEditMode.EditOnEnter;
16、新行指定默認值
    Specify Default Values for New Rows in the Windows Forms DataGridVIEw Control
    Sample:
    private void dataGridVIEw1_DefaultValuesNeeded(object sender,                       System.Windows.Forms.DataGridVIEwRowEventArgs e)
    {
      e.Row.Cells["Region"].Value = "WA";
       e.Row.Cells["City"].Value = "Redmond";
       e.Row.Cells["PostalCode"].Value = "98052-6399";
      e.Row.Cells["Region"].Value = "NA";
      e.Row.Cells["Country"].Value = "USA";
    e.Row.Cells["CustomerID"].Value = NewCustomerId();
    }
17、數據驗證
    Validate Data in the Windows Forms DataGridVIEw Control
    Samples:
     private void dataGridVIEw1_CellValidating(object sender,
            DataGridVIEwCellValidatingEventArgs e)
        {
        // Validate the CompanyName entry by disallowing empty strings.
        if (dataGridVIEw1.Columns[e.ColumnIndex].Name == "CompanyName")
        {
            if (e.FormattedValue.ToString() == String.Empty)
            {
                dataGridVIEw1.Rows[e.RowIndex].ErrorText =
                    "Company Name must not be empty";
                e.Cancel = true;
            }
        }
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved