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. 托布利兹变换 toeplitz 变换

    托布利兹变换 toeplitz 变换 算术平均变换 '''An->C=>(A1+A2+A3+...+An)/n->C,K_ni=1/n=>+[AiK_ni->C (Yn- ...

  2. Underdetermined system Constraint counting Overdetermined system

    https://en.wikipedia.org/wiki/Underdetermined_system https://en.wikipedia.org/wiki/Constraint_counti ...

  3. An optimizer that trains as fast as Adam and as good as SGD. https://www.luolc.com/publications/ad…

    设立3个指针pa.pb和pc,其中pa和pb分别指向La表和Lb表中当前待比较插入的结点,而pc指向Lc表中当前最后一个结点:若pa->data<=pb->data,则将pa所指结点 ...

  4. Xcode工程编译之duplicate symbol问题引发的一些知识

    概括: 文件中重复定义了一个函数.变量(比如全局变量) 工程中包含同名的文件. 一般的解决方法 1 在使用import 引入头文件时,由于疏忽,误引入.m 文件. 2 同名文件放在不同的文件夹下. 3 ...

  5. [未解决:快速滑动collectionveiw请求数据崩溃]:unable to allocate 6553600 bytes for bitmap data

    崩溃:unable to allocate 6553600 bytes for bitmap data

  6. SQLAlchemy_ORM

    ORM 与 SQLAlchemy 简介: ORM 叫对象关系映射,ORM 将对象转换成SQL,然后使用数据API执行SQL并获取执行结果 SQLAlchemy 是Python 社区最知名的 ORM 工 ...

  7. 剑指offer-合并两个排列的链接

    题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则.   public ListNode Merge(ListNode list1,ListNode ...

  8. 接口测试工具-Jmeter使用笔记(三:管理请求服务器信息和Headers参数)

    如果使用Jmeter同时执行多个http请求任务,就需要创建多个HTTP取样器,每一个取样器都来手动填写服务器信息和端口号,会非常消耗时间. 解决方法:Jmeter之HTTP请求默认值 1.添加方式 ...

  9. 【SVD、特征值分解、PCA关系】

    一.SVD    1.含义: 把矩阵分解为缩放矩阵+旋转矩阵+特征向量矩阵. A矩阵的作用是将一个向量从V这组正交基向量的空间旋转到U这组正交基向量的空间,并对每个方向进行了一定的缩放,缩放因子就是各 ...

  10. 【RPC】综述

    RPC定义 RPC(Remote Procedure Call)全称远程过程调用,它指的是通过网络,我们可以实现客户端调用远程服务端的函数并得到返回结果.这个过程就像在本地电脑上运行该函数一样,只不过 ...