Winform-DataGridView

1 常用属性

      // 1.点击后的选中模式
this.dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
// 2.不显示首列
this.dgv.RowHeadersVisible = false;
// 3.设置单元格行高
this.dgv.RowTemplate.Height = 50;
// 4.设置是否显示列标题
this.dgv.ColumnHeadersVisible = false;
// 5.设置单个单元格的背景色 字体颜色
this.dgv.Rows[1].Cells[1].Style.BackColor = Color.Green;
this.dgv.Rows[1].Cells[1].Style.ForeColor = Color.Red;
// 6.隔行换颜色
this.dgv.RowsDefaultCellStyle.BackColor = Color.LightBlue;
this.dgv.AlternatingRowsDefaultCellStyle.BackColor = Color.LightCyan;
// 7.设置列显示位置
this.dgv.Columns["ids"].DisplayIndex = 0;
// 8.列冻结
this.dgv.Columns["ids"].Frozen = true;
// 9.文本对其方式
// 10.列标题居中
this.dgv.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
// 11.单元格值为Null时的默认值
this.dgv.DefaultCellStyle.NullValue = "IsNull";
// 12.单元格边框样式
this.dgv.CellBorderStyle = DataGridViewCellBorderStyle.None;
// 13.数据样式
this.dgv.Columns["Price"].DefaultCellStyle.Format = "C3"; //货币格式,保留3位小数 // 14.取消多选
this.dgv.MultiSelect = false;
// 15.设置选中行
this.dgv.Rows[2].Selected = true;
// 17.选中单元格后的背景色
this.dgvO.DefaultCellStyle.SelectionBackColor = Color.Black;

2 常用方法

    // 1.排序
this.dgv.Sort(dgv.Columns[3], ListSortDirection.Ascending);
// 2.当前单元格内容
string value = this.dgv.CurrentCell.Value.ToString();

3 添加列

   DataGridViewButtonColumn col = new DataGridViewButtonColumn();
col.Text = col.Name = "Delete";
col.UseColumnTextForButtonValue = true; // Button text 显示 Text值
this.dgv.Columns.Add(col); //添加到最后边
this.dgv.Columns.Insert(index,col); //添加到指定列

4 获取 DataGridViewCheckBoxColumn 选中项

注意:当添加DataGridViewCheckBoxColumn 列时,需要勾选 Enable Editing,否则不能选中

       List<string> listIds = new List<string>();
for (int i = 0; i < dgv.Rows.Count; i++)
{
if (dgv.Rows[i].Cells[0].EditedFormattedValue.ToString().ToLower().Equals("true"))
{
listIds.Add(this.dgv.Rows[i].Cells["ids"].Value.ToString());
}
}
string str= string.Join(";", listIds.ToArray());
MessageBox.Show(str);

5 DataGridViewButtonColumn 列按钮点击事件

        private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex <= -1) return; //异常处理,如果是第一行的话 if (this.dgv.Columns[e.ColumnIndex].HeaderText == "Delete")
{
string str = this.dgv.Rows[e.RowIndex].Cells["ids"].Value.ToString();
MessageBox.Show("Delete:" + str);
}
}

6 正常显示图片

            //获取 Image类的实例,并修改图片的显示方式
DataGridViewImageColumn col = dgv.Columns["Image"] as DataGridViewImageColumn;
col.ImageLayout = DataGridViewImageCellLayout.Zoom;
//修改行高来设置高度
this.dgv.RowTemplate.Height = 60;
this.dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

7.换行显示

设置属性:

this.dgv.DefaultCellStyle.WrapMode = DataGridViewTriState.True;

在需要换行的地方添加:Environment.NewLine

Winform-DataGridView的更多相关文章

  1. 关于C# Winform DataGridView 设置DefaultCellStyle无效的原因与解决方案

    上周在开发Winform 项目中,我曾遇到一个看似简单,但一直都没有解决的问题,那就是:设置winform DataGridView控件的行DefaultCellStyle,但却没有任何变化,我也曾求 ...

  2. C#实现WinForm DataGridView控件支持叠加数据绑定

    我们都知道WinForm DataGridView控件支持数据绑定,使用方法很简单,只需将DataSource属性指定到相应的数据源即可,但需注意数据源必须支持IListSource类型,这里说的是支 ...

  3. WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决

    背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...

  4. C# winform DataGridView 常见属性

    C# winform DataGridView 属性说明① 取得或者修改当前单元格的内容 ② 设定单元格只读 ③ 不显示最下面的新行 ④ 判断新增行 ⑤ 行的用户删除操作的自定义 ⑥ 行.列的隐藏和删 ...

  5. Winform Datagridview 单元格html格式化支持富文本

    Winform Datagridview 单元格html格式化支持富文本 示例: 源码:https://github.com/OceanAirdrop/DataGridViewHTMLCell 参考: ...

  6. [WinForm]DataGridView列头右键菜单

    [WinForm]DataGridView列头右键菜单 前言 继续"不误正业" - - #,记录一下.有时候有这样的需求:DataGridView的列头菜单可以选择具体显示哪些列, ...

  7. winform datagridview 绑定泛型集合变得不支持排序的解决方案

    原文:winform datagridview 绑定泛型集合变得不支持排序的解决方案 案例: 环境:Winform程序 控件:Datagridview 现象:Datagridview控件绑定到List ...

  8. [转]WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决

    背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...

  9. WinForm DataGridView分页功能

    WinForm 里面的DataGridView不像WebForm里面的GridView那样有自带的分页功能,需要自己写代码来实现分页,效果如下图: 分页控件  .CS: 1 using System; ...

  10. [Winform]DataGridView列自适应宽度

    引言 在做winform项目中,数据控件DataGridView的使用多多少少是会用到的,如果不设置它的属性,默认情况下是不会自适应宽度的,你想查看某项的数据,就不得不将标题栏拖来拖去,挺烦的. 方法 ...

随机推荐

  1. verilog-产生axis数据流

    首先这是产生aixs数据流的代码 `timescale 1ps/1ps `default_nettype none module axis_switch_0_example_master #( ) ( ...

  2. 【es】创建索引和映射

    参考:http://www.cnblogs.com/sheeva/p/4837881.html 创建索引: curl -XPUT 'http://localhost:9200/some_index' ...

  3. laravel 中with关联查询限定查询字段

    学习了下laravel5.6框架,果然很优雅,比如ActiveJieSuan model中作如下关联:(laravel模型关联关系可以查看https://laravelacademy.org/post ...

  4. poj1185 状态压缩经典题

    状态压缩的好题,直接求会爆内存,先把所有可能的状态求出来存在stk里,然后f[i][k][t]表示i行状态为t,i-1状态为k,由i-1状态来推出i状态即可 注意要打好边际条件的状态,并且某个可行状态 ...

  5. Git使用五:回到过去

    reset:将仓库里面的内容恢复回暂存区,类似于从仓库里检出文件到暂存区checkout:将暂存区的文件恢复回工作区,即,把暂存区的文件检出到工作区 下面是之前三次提交的内容 三个区域的文件状态: 执 ...

  6. Python游戏编程(Pygame)

    安装Pygame pip install pygame C:\Users> pip install pygame Collecting pygame Downloading https://fi ...

  7. 多版本python安装第三方库

    1.先进入对应版本的python 2.使用命令安装:./pip install xxx

  8. 配置web pack loader 报错:Module build failed: Error: The node API for `babel` has been moved to `babel-core`.

    报错如下 Module build failed: Error: The node API for `babel` has been moved to `babel-core`. 在我配置loader ...

  9. windows下Qt播放flash

    Qt  版本:5.1.0 32bit 代码部分,参考1即可.配置发行时,需要将NPSWF32放在项目的plugins\目录中. 否则,可能找不到,导致无法运行. QWebSettings *setti ...

  10. 51Nod 1264 线段相交(计算几何)

    1264 线段相交  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出平面上两条线段的两个端点,判断这两条线段是否相交(有一个公共点或有部分重合认为相 ...