DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):
一、DataGridView属性设置
1、我们单击选中行的时候,默认是选择一个单元格,不能选择一整行,我们只需设置DataGridView的属性SelectionMode为FullRowSelect 。用代码表示:this.dataGridView1.SelectionMode =DataGridViewSelectionMode.FullRowSelect;

2、选择多行,可设置DataGridView的属性MultiSelect为false 。 用代码表示:this.dataGridView1.MultiSelect = false;这样就使DataGridView不能够选择多行,只能选择一行了

3、是否自动创列:dataGridView1.AutoGenerateColumns = false;
4、DataGridView这个控件会默认的在第一行第一列选中。这个问题我在网上看到了很多回答,也有很多人问。可能是我用的方法不对,效果不是很好后来找到了这种答案:
dataGridView1.ClearSelection();一行就可以。
5、设置标题样式&字体:首先把这个“EnableHeadersVisualStyles”属性设置为false。

在然后设置标题的样式,设置这个属性“ColumnHeadersDefaultCellStyle”:


6、设置行的样式,设置DefaultCellStyle属性:


二、DataGridView事件
1、单击项或双击行时,获取行的数据(CellClick)
private void DGV_CellClick(object sender, DataGridViewCellEventArgs e)
{ if (e.RowIndex != -)//判断是否点在行上
{
txt_No.Text = this.DGV["Col_No", e.RowIndex].Value.ToString();
txt_Name.Text = this.DGV["Col_Name", e.RowIndex].Value.ToString();
txt_Type.Text = this.DGV["Col_Type", e.RowIndex].Value.ToString();
txt_Time.Text = this.DGV["Col_Time", e.RowIndex].Value.ToString();
}
}
“Col_No”。“Col_Name”,“Col_Type”,“Col_Time”是列名。
2、在单元格的内容需要设置格式以便于显示时发生。(CellFormatting)
/// <summary>
/// 如果列名Col_IsEnabled列的值为1则显示启用,否则显示禁用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DGV_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (DGV.Columns[e.ColumnIndex].Name == "Col_IsEnabled")
{
if ((e.Value).ToString().Trim() == "")
{
e.Value = "启用";
}
else
{
e.Value = "禁用";
}
}
}
改变列值
代码示例:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if ((e.RowIndex >= ) && (e.ColumnIndex >= ))
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "Col_IsEnable")
{
if ((e.Value).ToString().Trim() == "")
{
e.Value = "启用";
e.CellStyle.ForeColor = Color.Red;
dataGridView1.Columns[e.ColumnIndex].Width = ; //设置列宽 }
else if ((e.Value).ToString().Trim() == "")
{
e.Value = "禁用";
e.CellStyle.ForeColor = Color.Green;//设置字体前景
e.CellStyle.BackColor = Color.Purple;//设置背景色
dataGridView1.Rows[e.RowIndex].Height = ;//设置行高
}
}
}
} private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex != -)
{
textBox1.Text = this.dataGridView1["Col_Id", e.RowIndex].Value.ToString();
textBox2.Text = this.dataGridView1["Col_Name", e.RowIndex].Value.ToString();
textBox3.Text = this.dataGridView1["Col_Age", e.RowIndex].Value.ToString();
textBox4.Text = this.dataGridView1["Col_Sex", e.RowIndex].Value.ToString();
comboBox1.Text = this.dataGridView1["Col_IsEnable", e.RowIndex].Value.ToString();
}
} private void button1_Click(object sender, EventArgs e)
{
DataGridViewRow dr = new DataGridViewRow();
dr.CreateCells(dataGridView1);
dr.Cells[].Value = textBox1.Text.ToString().Trim();
dr.Cells[].Value = textBox2.Text.ToString().Trim();
dr.Cells[].Value = textBox3.Text.ToString().Trim();
dr.Cells[].Value = textBox4.Text.ToString().Trim();
dr.Cells[].Value = comboBox1.Text.ToString();
// dataGridView1.Rows.Insert(0, dr); //插入的数据作为第一行显示
dataGridView1.Rows.Add(dr); //插入的数据作为最后一行显示
} }
}

3、某一列不可编辑:
DGV.Columns["列名"].ReadOnly = true;
或:
DGV.Columns[i].ReadOnly = true;
DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):的更多相关文章
- 关于Datagridview控件用法的一些总结(设置列chicun)
1. 关于Datagridview控件用法的一些总结:http://www.cnblogs.com/mingjiatang/p/4968049.html
- 关于Datagridview控件用法的一些总结
一.引言 Datagridview控件在winform开发中还是比较常用,一般的数据库系统都会使用它,但是想要友好的展示数据,形成良好的用户界面,那么就要对c#库中默认的Datagridview设置进 ...
- WinForm开发(1)——DataGridView控件(1)——C# DataGridView控件用法介绍
DataGridView控件在实际应用中非常实用,特别需要表格显示数据时.可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行.假如需要动态为DataGridView控件添加新行, ...
- DataGridView控件用法二:常用属性
通常会设置的DataGridView的属性如下: AllowUserToAddRows - False指示是否向用户显示用于添加行的选项,列标题下面的一行空行将消失.一般让其消失.AllowUserT ...
- DataGridView控件用法合集
1.当前的单元格属性取得.变更 Console.WriteLine(DataGridView1.CurrentCell.Value) Console.WriteLine(DataGridView1.C ...
- winform DataGridView控件判断滚动条是否滚动到当前已加载的数据行底部 z
http://www.zuowenjun.cn/post/2015/05/20/162.html 判断 DataGridView控件滚动条是否滚动到当前已加载的数据行底部,其实方法很简单,就是为Dat ...
- DataGridView控件
DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需要的特 ...
- DataGridView控件-[引用]
DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需要的特 ...
- DataGridView控件使用大全说明-各种常用操作与高级操作
DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需要的特 ...
随机推荐
- 【转】group by多个字段理解
来源:http://uule.iteye.com/blog/1569262 首先group by 的简单说明: group by 一般和聚合函数一起使用才有意义,比如 count sum avg等,使 ...
- [ActionScript] AS3利用SWFObject与JS通信
首先介绍SWFObject的用法: swfobject.embedSWF(swfUrl, id, width, height, version, expressInstallSwfurl, flash ...
- linux 去掉 ^M
要去除他,最简单用下面的命令: dos2unix filename 亲测可用 以下方式不可以: set ff=unix %s/^M//g 可能是^M输入方式有问题 ^M 输入方法: ctrl+V ...
- aria-expanded,aria-hidden到底做什么用?
aria-expanded表示展开状态.默认为undefined, 表示当前展开状态未知.其它可选值:true表示元素是展开的:false表示元素不是展开的. aria-hidden字符串.可选值为t ...
- django 获取系统当前时间 和linux 系统当前时间不一致 问题处理。
问题场景: 在django admin models 实体对象添加一个属性最后修改时间,用户在添加.修改是系统自动修改操作时间. UpdateTime自动获取系统时间.并且自动修改. 代码设置如下. ...
- 学习Webservice测试
2014-04-01 可用Myeclipse10自带工具生成客户端, 也可用CXF生成,注意,不要用CXF3.0.0milestone,该版本不能生成,请用CXF2.2.8 2015-01-12 下载 ...
- 30. Distinct Subsequences
Distinct Subsequences OJ: https://oj.leetcode.com/problems/distinct-subsequences/ Given a string S a ...
- 下载和使用 Open XML PowerTools
安装 Open XML SDK 2.5 首先,需要安装 Open XML SDK 2.5 ,从这个地址下载安装程序:http://www.microsoft.com/en-in/download/de ...
- VC让对话框显示就最大化
方法一:在OnInitDialog()函数中 ShowWindow(SW_SHOWMAXIMIZED); 初始化的时候 方法二: 当然,你可以获取屏幕大小,然后设置窗口位置/大小 //ShowWind ...
- CODESOFT中怎样打印数据库中的特定数据?
CODESOFT可用于打印.标记和跟踪的零售库存标签软件,每种产品的售卖都代表着需要打印大量的条码标签.通常我们采用的方法就是在CODESOFT连接数据库批量打 印.但是如果数据量很大,该如何选择 ...