DataGridView常用属性:

只读属性设定
    datagridview.ReadOnly = True 
行自动追加
    datagridview.AllowUserToAddRows = False
删除行允许
    datagridview.AllowUserToDeleteRows = False
行幅设置
    datagridview.AllowUserToResizeRows = False
    datagridview.ColumnHeadersHeightSizeMode =
    DataGridViewColumnHeadersHeightSizeMode.DisableResizing
行表示
    datagridview.RowHeadersVisible = False
行选择模式
    datagridview.SelectionMode = DataGridViewSelectionMode.FullRowSelect
复数行选择
    datagridview.MultiSelect = True
选择状态解除
    datagridview.ClearSelection()
文字设置位置
    datagridview.ColumnHeadersDefaultCellStyle.Alignment = 
    DataGridViewContentAlignment.MiddleCenter
选择后行的颜色
    datagridview.DefaultCellStyle.SelectionBackColor = Color.GreenYellow
    datagridview.DefaultCellStyle.SelectionForeColor = Color.Black
行幅自动调整
    datagridview.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill

DataGridView常用方法=======================================================================================

1、自定义列
Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their
Behavior and Appearance
Host Controls in Windows Forms DataGridView Cells
继承 DataGridViewTextBoxCell 类生成新的Cell类,然后再继承 DataGridViewColumn 生成新的Column类,并指定
CellTemplate为新的Cell类。新生成的Column便可以增加到DataGridView中去。
2、自动适应列宽
Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control
Samples:
DataGridView.AutoSizeColumns(
DataGridViewAutoSizeColumnCriteria.HeaderAndDisplayedRows);
DataGridView.AutoSizeColumn(
DataGridViewAutoSizeColumnCriteria.HeaderOnly,
2, false);
DataGridView.AutoSizeRow(
DataGridViewAutoSizeRowCriteria.Columns,
2, false);
DataGridView.AutoSizeRows(
DataGridViewAutoSizeRowCriteria.HeaderAndColumns,
0, dataGridView1.Rows.Count, false);
3、可以绑定并显示对象
Bind Objects to Windows Forms DataGridView Controls
4、可以改变表格线条风格
Change the Border and Gridline Styles in the Windows Forms DataGridView Control
Samples:
this.dataGridView1.GridColor = Color.BlueViolet;
this.dataGridView1.BorderStyle = BorderStyle.Fixed3D;
this.dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
this.dataGridView1.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
this.dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
5、动态改变列是否显示,和动态改变列的显示顺序
Change the Order of the Columns in the Windows Forms DataGridView Control
Samples:
customersDataGridView.Columns["CustomerID"].Visible = false;
customersDataGridView.Columns["ContactName"].DisplayIndex = 0;
customersDataGridView.Columns["ContactTitle"].DisplayIndex = 1;
customersDataGridView.Columns["City"].DisplayIndex = 2;
customersDataGridView.Columns["Country"].DisplayIndex = 3;
customersDataGridView.Columns["CompanyName"].DisplayIndex = 4;
6、可以在列中显示图像
Display Images in Cells of the Windows Forms DataGridView Control
Samples:
Icon treeIcon = new Icon(this.GetType(), "tree.ico");
DataGridViewImageColumn iconColumn = new DataGridViewImageColumn ();
iconColumn.Image = treeIcon.ToBitmap();
iconColumn.Name = "Tree";
iconColumn.HeaderText = "Nice tree";
dataGridView1.Columns.Insert(2, iconColumn);
7、格式化显示内容:
Format Data in the Windows Forms DataGridView Control
Samples:
this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle.Format = "c";
this.dataGridView1.Columns["ShipDate"].DefaultCellStyle.Format = "d";
this.dataGridView1.DefaultCellStyle.NullValue = "no entry";
this.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewWrapMode.Wrap;
this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleRight;

8、在拖动列的滚动条时可以将指定的列冻结
Freeze Columns in the Windows Forms DataGridView Control
Samples:将指定列及以前的列固定不动
this.dataGridView1.Columns["AddToCartButton"].Frozen = true;
9、获取选择的单元格,行,列
Get the Selected Cells, Rows, and Columns in the Windows Forms DataGridView Control
Samples:
见msdn。
10、显示录入时出现的错误信息
Handle Errors that Occur During Data Entry in the Windows Forms DataGridView Control
Samples:
private void dataGridView1_DataError(object sender,
DataGridViewDataErrorEventArgs e)
{
// If the data source raises an exception when a cell value is
// commited, display an error message.
if (e.Exception != null &&
e.Context == DataGridViewDataErrorContext.Commit)
{
MessageBox.Show("CustomerID value must be unique.");
}
}
11、大数据量显示采用Virtual Mode
Implement Virtual Mode in the Windows Forms DataGridView Control
12、设置指定的列只读
Make Columns in the Windows Forms DataGridView Control Read-Only
Samples:
dataGridView1.Columns["CompanyName"].ReadOnly = true;

13、移去自动生成的列
Remove Autogenerated Columns from a Windows Forms DataGridView Control
Sample:
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = customerDataSet;
dataGridView1.Columns.Remove ("Fax");
或:
dataGridView1.Columns["CustomerID"].Visible = false;
14、自定义选择模式
Set the Selection Mode of the Windows Forms DataGridView Control
Sample:
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.MultiSelect = false;
15、自定义设定光标进入单元格是否编辑模式(编辑模式)
Specify the Edit Mode for the Windows Forms DataGridView Control
this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
16、新行指定默认值
Specify Default Values for New Rows in the Windows Forms DataGridView Control
Sample:
private void dataGridView1_DefaultValuesNeeded(object sender, System.Windows.Forms.DataGridViewRowEventArgs e)
{
e.Row.Cells["Region"].Value = "WA";
e.Row.Cells["City"].Value = "Redmond";
e.Row.Cells["PostalCode"].Value = "98052-6399";
e.Row.Cells["Region"].Value = "NA";
e.Row.Cells["Country"].Value = "USA";
e.Row.Cells["CustomerID"].Value = NewCustomerId();
}
17、数据验证
Validate Data in the Windows Forms DataGridView Control
Samples:
private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
   // Validate the CompanyName entry by disallowing empty strings.
    if (dataGridView1.Columns[e.ColumnIndex].Name == "CompanyName")
    {
        if (e.FormattedValue.ToString() == String.Empty)
       {
          dataGridView1.Rows[e.RowIndex].ErrorText ="Company Name must not be empty";
          e.Cancel = true;
       }
    }
}

原文出处:https://blog.csdn.net/ibmfahsion/article/details/7929439

DataGridView常用属性和方法的更多相关文章

  1. Node.js process 模块常用属性和方法

    Node.js是常用的Javascript运行环境,本文和大家发分享的主要是Node.js中process 模块的常用属性和方法,希望通过本文的分享,对大家学习Node.js http://www.m ...

  2. ios基础篇(四)——UILabel的常用属性及方法

    UILabel的常用属性及方法:1.text //设置和读取文本内容,默认为nil label.text = @”文本信息”; //设置内容 NSLog(@”%@”, label.text); //读 ...

  3. UITableView常用属性和方法 - 永不退缩的小白菜

    UITableView常用属性和方法 - 永不退缩的小白菜 时间 2014-05-27 01:21:00  博客园精华区原文  http://www.cnblogs.com/zhaofucheng11 ...

  4. UIView的一些常用属性和方法

    UIView的一些常用属性和方法 1. UIView的属性 UIView继承自UIResponder,拥有touches方法. - (instancetype)initWithFrame:(CGRec ...

  5. SVG DOM常用属性和方法介绍(1)

    12.2  SVG DOM常用属性和方法介绍 将以Adobe SVG Viewer提供的属性和方法为准,因为不同解析器对JavaScript以及相关的属性和方法支持的程度不同,有些方法和属性是某个解析 ...

  6. 第190天:js---String常用属性和方法(最全)

    String常用属性和方法 一.string对象构造函数 /*string对象构造函数*/ console.log('字符串即对象');//字符串即对象 //传统方式 - 背后会自动将其转换成对象 / ...

  7. UIView常用属性与方法/UIKit继承结构

    UIView常用属性与方法 @interface UIView : UIResponder<NSCoding, UIAppearance, UIAppearanceContainer, UIDy ...

  8. JavaScript中Number常用属性和方法

    title: JavaScript中Number常用属性和方法 toc: false date: 2018-10-13 12:31:42 Number.MAX_VALUE--1.79769313486 ...

  9. 12-27 UITableView常用属性及方法

    UITableView也有自己的代理协议,它本身继承自UIScrollView 一:代理要遵守代理协议<UITableViewDelegate>,代理协议中的代理方法: 1.改变某一行的行 ...

随机推荐

  1. day4:数据结构list

    1,一直输入用户名,输入Q退出,注意用户的输入别忘了加strip,和upper不区分大小写,list最后一位添加append li = [] while 1: name = input("& ...

  2. day2:day1作业 字符编码

    1,使用while循环输出1,2,3,4,5,6,8,9 # 使用while循环输出1,2,3,4,5,6,8,9 num = 1 while num <= 10: if num != 7: p ...

  3. [math][mathematica] archlinux 下 mathematica 的安装 (科学计算软件 mathematica/matlab/sagemath)

    ONLINE: http://www.wolframalpha.com/ GPL: segamath: http://www.sagemath.org/ famous and not free: ma ...

  4. [troubleshoot][daily][redhat] 设备反复重启故障排查

    一台服务器设备,反复重启,每天重启数次. 一: 原因分析及初步排异. 1.  硬件,内存主板,一一更换,甚至除了硬盘将整台机器都换掉了,依然重启. 2.  排除电源问题,换了电源线,换了插座,还是重启 ...

  5. dp进阶——饥饿的奶牛

    饥饿的奶牛oj上n只有1000,过于水,O(n^2)的算法很容易水过,洛谷上这是一道提高加的题,很难啊,所以要好好拿来练习今天写博客再次复习一下,oi最怕遗忘了. 这道题呢实质是一个区间覆盖的dp,首 ...

  6. 【Java】一台服务器配置多个Tomcat

    需求缘由 最近接收了一个新的工具业务:ipublish发布系统,刚接手这个业务的时候,发现每次发布新的代码 需要到群里告知大家,我要停服务几分钟,准备更新代码啦.这尼玛 哪个公司都不敢这么牛逼的和用户 ...

  7. Idea下载安装

    安装 下载 下载地址:http://www.jetbrains.com/idea/#chooseYourEdition,选择付费版的zip格式的下载 安装过程中除了勾选64版本之外其他一路next,直 ...

  8. excel之导出

    1.Maven依赖的jar包 <dependency>     <groupId>org.apache.poi</groupId>     <artifact ...

  9. BZOJ3613 南园满地堆轻絮 二分/贪心

    正解:贪心 解题报告: 传送门! 这题似乎是可以二分水过的,,,但数据可以加强一下就能简单把二分卡住了,或者修改下空间限制什么的反正就很容易能卡住 所以这里介绍一个优秀的贪心做法,O(n)的时间复杂度 ...

  10. passwd 修改密码

    [root@localhost ~]# passwd # 修改 root 用户的密码 [root@localhost ~]# passwd test # 修改指定用户的密码