天天看點

1. DataGridView設定字型、行高、列寬、單列居中

DataGridView表格内容的列寬、行高、字型的設定,設定某一列居中。一般地,會将行高設為統一的,列寬根據不同情況設定。

[csharp] view plaincopyprint?

  1. // 調整字型  
  2. dataGridView1.Font = new Font("宋體", 11);  
  3. // 調整行高  
  4. //dataGridView1.Rows[0].Height = 100;  
  5. dataGridView1.RowTemplate.Height = 30;  
  6. dataGridView1.Update();  
  7. // 調整列寬,注意autosizecolumnsmode屬性不能設定為fill  
  8. dataGridView1.Columns[0].Width = 70;  
  9. dataGridView1.Columns[1].Width = 360;  
  10. dataGridView1.Columns[2].Width = 100;  
  11. dataGridView1.Columns[3].Width = 239;  
  12. // 設定某一列居中  
  13. dataGridView1.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;  
// 調整字型
dataGridView1.Font = new Font("宋體", 11);
// 調整行高
//dataGridView1.Rows[0].Height = 100;
dataGridView1.RowTemplate.Height = 30;
dataGridView1.Update();
// 調整列寬,注意autosizecolumnsmode屬性不能設定為fill
dataGridView1.Columns[0].Width = 70;
dataGridView1.Columns[1].Width = 360;
dataGridView1.Columns[2].Width = 100;
dataGridView1.Columns[3].Width = 239;
// 設定某一列居中
dataGridView1.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
      

至于DataGridView行頭和列頭(即表頭部分)的設定可直接在控件的屬性視窗中設定。相關的屬性是ColumnHeader...和RowHeader...。

可能存在的問題:設定行高後若需要重新整理兩次後才顯示為新設定的行高,則可以通過把設定行高部分的代碼拷貝到構造函數中解決。

2. DataGridView單擊選中整行

方法一:

[csharp] view plaincopyprint?

  1. //設定為整行被選中  
  2. this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;  
//設定為整行被選中
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
      

上述代碼加到構造函數中即可,其他能被調用的地方也可。

方法二:添加事件(但是不完美)

[csharp] view plaincopyprint?

  1. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)  
  2. {  
  3.     dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;  
  4.     Console.WriteLine("點選内容...");  
  5. }  
  6. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)  
  7. {  
  8.     dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;  
  9.     Console.WriteLine("點選Cell...");  
  10. }  
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
    Console.WriteLine("點選内容...");
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
    Console.WriteLine("點選Cell...");
}
      

這種方法存在兩個問題:一是,采用CellContentClick事件時,當單擊單元格空白處時,不會選中整行,仍是選中單元格,單擊單元格中文字時,可以選中整行;二是,不支援選中多行,即選多行時,仍是選中多個單元格。

注意:要使事件監聽生效,需要在XXX.designer.cs檔案中InitializeComponent方法中添加注冊事件相關的代碼:

[csharp] view plaincopyprint?

  1. this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);  
  2. this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);  
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);      

3 DataGridView屬性

AllowUserToDeleteRows:是否允許使用者使用“delete”鍵删除選中行。true:允許;false:不允許。

4 DataGridView相關

重新整理DataGridView時,會出現每次重新整理都在最後自動添加一行的問題。如下解決:

[csharp] view plaincopyprint?

  1. this.dataGridView1.Rows.Clear();