WinForm中DataGridView的使用(一) - 基本使用


  • 數據綁定
    • 直接指定源數據(List<T>):this.DataSource = data;
    • 通常也可以直接指定DataTable類型的數據
      •  1 DataTable dt = new DataTable();
         2             DataColumn dc1 = new DataColumn(_column1Name, Type.GetType("System.String"));
         3             dt.Columns.Add(dc1);
         4             foreach (string searchText in VisualXmlApp.Instance.searchHistoryGridView.Take(5))
         5             {
         6                 DataRow dr = dt.NewRow();
         7                 dr[_column1Name] = searchText;
         8                 dt.Rows.Add(dr);
         9             }
        10             this.dgvDataSourceSearchHistory.DataSource = dt;
        View Code
    • 自定義列
      • 取消自動生成列:this.AutoGenerateColumns = false;
      • 自定義列數、列名、列寬權重、列填充
        •  1             this.Columns.Clear();
           2             this.ColumnCount = 3;
           3             this.Columns[0].Name = "Column 1";
           4             this.Columns[0].DataPropertyName = "VarName";
           5             this.Columns[0].FillWeight = 200;
           6             this.Columns[1].Name = "Column 2";
           7             this.Columns[1].DataPropertyName = "VarPath";
           8             this.Columns[1].FillWeight = 300;
           9             this.Columns[2].Name = "Column 2";
          10             this.Columns[2].FillWeight = 100;
          View Code
  • 樣式
    • 整體背景色(當窗口變大時,空白處的顏色)
      • this.BackgroundColor = DataSourceUIParams.WindowBackColor;
    • 邊框
      • 風格
        • DataGridView.BorderStyle
          • BorderStyle 枚舉: FixedSingle(單線,默認)、Fixed3D、None
      • 顏色(所有邊框,含普通數據行和頭部)
        • this.GridColor = Color.FromArgb(173, 190, 203);
        • 如果要頭部生效,別忘了取消系統風格影響:this.EnableHeadersVisualStyles=false;
        • 默認是 ControlDarkDark 。但是只有在 CellBorderStyle 被設定為 Single、SingleHorizontal、SingleVertical  的條件下才能改變其邊框線的顏色。
      • 如果是自定義了一個控件繼承自DataGridView,那么即使在這個控件中設置了不顯示邊框,在父控件中仍然需要再設置一次其BorderStyle = BorderStyle.None;
      • 其他可參考(如顏色、四個方位邊框的單獨設置):https://blog.csdn.net/yunhaic/article/details/7176015
    • 頭部
      • 取消使用系統風格:this.EnableHeadersVisualStyles = false;
      • 行頭
        • 取消顯示行頭
          • this.RowHeadersVisible = false;
        • 邊框
          • 風格
            • 直接設置RowHeadersBorderStyle,默認是DataGridViewHeaderBorderStyle.Raised,可改為Single、None等。屬性設定值是DataGridViewHeaderBorderStyle枚舉的值
      • 列頭
        • 取消顯示列頭
          • this.ColumnHeadersVisible = false;
        • 高度
          • 先把ColumnHeadersHeightSizeMode從默認的AutoSize設置為EnableResizing
          • 再用ColumnHeadersHeight直接設置高度
          • 注:代碼構造函數中直接設置這兩個屬性也可以
          • 注:將DataGridView設置固定高度,並自動顯示滾動條時,如果Header高度是AutoSize的,可能會使最后一行顯示不全
        • 字體
        • 邊框
          • 風格
            • 直接設置ColumnHeadersBorderStyle,默認是DataGridViewHeaderBorderStyle.Raised,可改為Single、None等。屬性設定值是DataGridViewHeaderBorderStyle枚舉的值
            • 同樣,ColumnHeadersBorderStyle只能設定單元格全部邊框線的式樣。要單獨改變單元格某一邊邊框式樣的話,需要用到設定行頭單元格的屬性是: RowHeadersBorderStyle, 設定列頭單元格屬性是:ColumnHeadersBorderStyle
    • 數據行
      • 高度
        • this.RowTemplate.Height = 30;
      • 奇偶行
        • 背景色
          • this.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(223, 230, 237);
          • this.RowsDefaultCellStyle.BackColor = Color.White;
      • 邊框
        • 風格
          • 直接設置CellBorderStyle,默認為BorderStyle.FixedSingle,但這個時候底邊框有可能消失,可以考慮改為BorderStyle.Fixed3D
          • CellBorderStyle只能設定單元格全部邊框線的式樣。要單獨改變單元格某一邊邊框式樣的話,需要用到DataGridView.AdvancedCellBorderStyle屬性。
  • 是否可編輯、新增、刪除
    • this.ReadOnly = true;
    • this.AllowUserToAddRows = false;
    • this.AllowUserToDeleteRows = false;
  • 是否可調整列寬、行寬
    • this.AllowUserToResizeColumns = false;
      this.AllowUserToResizeRows = false;
  • 是否可選中及選中模式
    • 是否可多選:this.MultiSelect = false;
    • 設置選中模式
      • 直接設置SelectionMode屬性,包括DataGridViewSelectionMode.FullRowSelect等選項
    • 取消首行或首個單元格的默認選中
      • 在DataGridView的RowsPrePaint事件處理函數中
        •  1         private void RowsPrePaintHandler(object sender, DataGridViewRowPrePaintEventArgs e)
           2         {
           3             int index = e.RowIndex;
           4 
           5             // cancel default selected first row
           6             if (index == 0)
           7             {
           8                 this.Rows[index].Selected = false;
           9             }
          10         }
          View Code


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM