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();
时间: 2024-08-05 15:22:12

1. DataGridView设置字体、行高、列宽、单列居中的相关文章

如何动态改变报表的行高列宽

报表应用中有时需要动态控制行高或列宽以满足特定需要.如分组报表中要求每个分组的总高度 / 宽度固定不变,这样一来就能实现每页纸打印相同数量的分组. 润乾报表可以在行高列宽属性中编写表达式,从而实现动态行高列宽,下面通过一个例子说明使用过程. 1.连接数据源 使用润乾报表设计器,连接自带数据源 DEMO 2.设置数据集 新建报表并设置数据集,使用 SQL 取数 数据集 ds1 如下: SELECT STATE,EID,NAME,SURNAME,GENDER,DEPT,SALARY FROM EMP

Excel快速删除空白行与调整行高列宽的方法,学会了很实用

Excel表格作为大家经常使用的办公文档,在使用的过程中我们也会遇到各种问题,比如常见的行高列宽的调整.空白行的删除的等等.今天小编就为大家带来了Excel快速删除空白行与行高列宽的方法,希望可以帮助到大家. 一:调整行高列宽 作为Excel表格最常见的问题之一,我们在办公中常用到的调整行高列宽的方法主要有两种,都非常的简单方便. 方法1: 第一种方法也是最简单的一种方法,把鼠标光标放置在行高或者是列宽线条上时,就会出现一个[黑色双箭头]的标志,点击即可拉动线条调整行高.列宽.(唯一的弊端是行高

datagridview 行高列宽的自动设置

1) 设定行高和列宽自动调整 [C#]// 设定包括Header和所有单元格的列宽自动调整 DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; // 设定包括Header和所有单元格的行高自动调整 DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; AutoSizeColumnsMode 属性的设定值枚

div css文字字体行高行距 深入理解css行间距设置

div css文字字体行高行距 深入理解css行间距设置 div css文字字体行高行距 DIVCSS5带您深入理解css行间距设置,字体行距如何最简单地设置. 在文章中,要实现调节设置每行文字字体间距(行距)是使用line-height属性.(体感音乐) 要使得每行的文字一定的间距距离所以可以通过line-height样式实现.接下来DIVCSS5通过DIV CSS实例实现字体文字上下间距. 相关扩展CSS教程: css line-height div css字间距 为了观察到行距设置效果,新

Swift设置自动行高

1 // 设置行高自适应 2 tableView.rowHeight = UITableViewAutomaticDimension 3 4 // 设置预估行高 5 tableView.estimatedRowHeight = 200 1 // 底部的toolBar 2 toolBar.snp_makeConstraints { (make) in 3 make.top.equalTo(originalView.snp_bottom) 4 make.leading.trailing.equalT

[Flex] 组件Tree系列 —— 运用variableRowHeight和wordWrap设置可变行高

mxml: 1 <?xml version="1.0" encoding="utf-8"?> 2 <!--功能描述:运用variableRowHeight和wordWrap设置可变行高--> 3 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 4 xmlns:s="library://ns.adobe.com/flex/spark"

ListControl 设置表格行高与字体

设置行高: CImageList   m_l; m_l.Create(1,18,TRUE|ILC_COLOR32,1,0);   listCtrl.SetImageList(&m_l,LVSIL_SMALL); 设置字体: CFont m_font; m_font.CreatePointFont(50, "新宋体"); m_listCtrl.SetFont(&m_font); 原文地址:https://www.cnblogs.com/qingtian224/p/9248

div居中 边框设置 文字行高设置

<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>无标题文档</title><style>.divCen{    width:200px; height:100px;     margin:90px auto; /*div居中*/  

行内元素(inline标签)设置了行高为什么不生效,还是表现为父盒子的行高?行内元素行高问题终极解释

最近在看张鑫旭大佬的<css世界>,读到5.2.4  内联元素 line-height 的“大值特性” ,产生了疑惑, 在开发中确实也遇到了同样的问题,深入探究后得出结果,先说结论吧,论证内容有点长,结论: 行内元素自身并没有行高这个属性,虽然设置可以设置行高,但是最终作用的地方并不是自身,而是其自身所在的行框盒子上!如果你不知道什么是行框盒子,建议你好好读读张大佬的<css世界>,这里我简单的画一画 如上如所示,我的结论是: 内联盒子没有行高这一属性,给它设置line-heigh