1. DataGridView設置字體、行高、列寬、單列居中

 

DataGridView表格內容列寬行高字體的設置,設置某一列居中。通常地,會將行高設爲統一的,列寬根據不一樣狀況設定。函數

 

[csharp] view plain copy print ?
  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...。字體

 

可能存在的問題:設置行高後若須要刷新兩次後才顯示爲新設置的行高,則能夠經過把設置行高部分的代碼拷貝到構造函數中解決。this

2. DataGridView單擊選中整行

方法一

 

[csharp] view plain copy print ?
  1. //設置爲整行被選中  
  2. this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;  
//設置爲整行被選中
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

 

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

 

方法二:添加事件(可是不完美).net

 

[csharp] view plain copy print ?
  1. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)  
  2. {  
  3.     dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;  
  4.     Console.WriteLine("點擊內容...");  
  5. }  
  6.   
  7. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)  
  8. {  
  9.     dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;  
  10.     Console.WriteLine("點擊Cell...");  
  11. }  
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事件時,當單擊單元格空白處時,不會選中整行,還是選中單元格,單擊單元格中文字時,能夠選中整行;二是,不支持選中多行,即選多行時,還是選中多個單元格。code

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

[csharp] view plain copy print ?
  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:不容許。blog

4 DataGridView相關

刷新DataGridView時,會出現每次刷新都在最後自動添加一行的問題。以下解決:事件

 

[csharp] view plain copy print ?
  1. this.dataGridView1.Rows.Clear();  
相關文章
相關標籤/搜索