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();