C# DataGridView的初始化
动态添加列和行
方法一
通过手动添加Datatable,再绑定dataGridView
DataTable dt = new DataTable();//建立个数据表
dt.Columns.Add(new DataColumn("id", typeof(int)));//在表中添加int类型的列
dt.Columns.Add(new DataColumn("Name", typeof(string)));//在表中添加string类型的Name列
DataRow dr;//行
for (int i = ; i < ; i++)
{
dr = dt.NewRow();
dr["id"] = i;
dr["Name"] = "Name" + i;
dt.Rows.Add(dr);//在表的对象的行里添加此行
} dataGridView1.DataSource =dt;
如果要添加一个textbox效果的列,可做如下处理
dt.Columns.Add(new DataColumn("选中", typeof(bool));
方法二
直接在dataGridView中插入
dataGridView1.ColumnCount = ;
dataGridView1.ColumnHeadersVisible = true; // Set the column header style.
DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle(); columnHeaderStyle.BackColor = Color.Beige;
columnHeaderStyle.Font = new Font("Verdana", , FontStyle.Bold);
dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle; // Set the column header names.
dataGridView1.Columns[].Name = "Recipe";
dataGridView1.Columns[].Name = "Category";
dataGridView1.Columns[].Name = "Main Ingredients";
dataGridView1.Columns[].Name = "Rating"; // Populate the rows.
string[] row1 = new string[] { "Meatloaf", "Main Dish", "ground beef","**" };
string[] row2 = new string[] { "Key Lime Pie", "Dessert", "lime juice, evaporated milk", "****" };
string[] row3 = new string[] { "Orange-Salsa Pork Chops", "Main Dish", "pork chops, salsa, orange juice", "****" };
string[] row4 = new string[] { "Black Bean and Rice Salad", "Salad", "black beans, brown rice", "****" };
string[] row5 = new string[] { "Chocolate Cheesecake", "Dessert", "cream cheese", "***" };
string[] row6 = new string[] { "Black Bean Dip", "Appetizer", "black beans, sour cream", "***" };
object[] rows = new object[] { row1, row2, row3, row4, row5, row6 }; foreach (string[] rowArray in rows)
{
dataGridView1.Rows.Add(rowArray);
}
插入DataGridViewCheckBoxColumn列
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
{
column.HeaderText = "选中";
column.Name = isSelected;
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
column.FlatStyle = FlatStyle.Standard;
column.ThreeState = true;
column.CellTemplate = new DataGridViewCheckBoxCell();
column.CellTemplate.Style.BackColor = Color.Beige;
}
DataGridView1.Columns.Insert(, column);
DGV的常用属性设置
1. 禁用编辑功能
dataGridView1.ReadOnly = true ;
2.不让DataGridView自动生成列
只需要把属性AutoGenerateColumns设为false即可。
需要注意: 在界面设计的属性窗口中是看不到AutoGenerateColumns属性的,
需要在代码中设定,比如在窗口的构造函数中设定:dataGridView1.AutoGenerateColumns = false;
C# DataGridView的初始化的更多相关文章
- winform DataGridView 通用初始化
void DGV_Init() { //名称 类型 设备数 累计转发次数 累计转发数据数 状态 ; i < ; i++) { DataGridViewTextBoxColumn dc = new ...
- c# DataGridView操作
#region 操作DataGridView /// <summary> /// 初始化DataGridView属性 /// </summary> /// <param ...
- DataGridView 使用精华
DataGridView控件用法合集 1. 当前的单元格属性取得.变更 [C#] //显示当前单元格的值 Console.WriteLine(DataGridView1.CurrentCell.Val ...
- DataTable.Select
转载请注明出处:http://www.cnblogs.com/havedream/p/4453297.html 方法:DataTable.Select 作用:获取 DataRow 对象的数组. 重载: ...
- C# WinForm自定义通用分页控件
大家好,前几天因工作需要要开发一个基于WinForm的小程序.其中要用到分页,最开始的想法找个第三方的dll用一下,但是后来想了想觉得不如自己写一个玩一下 之前的web开发中有各式各样的列表组件基本都 ...
- DataGridView初始化,加载数据
1,创建winform窗体应用程序 2,在界面上拖入DataGridView控件 3,添加相应的列如图: 4,开始编写后面的代码: private DataTable CountryDt = new ...
- [WinForm] DataGridView 绑定 DT && ComboBox 列绑定 Dict
一 需求介绍 一般像枚举类型的数据,我们在数据库里存储着诸如(1.2.3.4-)或者("001"."002"."003"-)此类,但是界面 ...
- [Winform] DataGridView 总结(FAQ)
Q1. 如何使单元格不可编辑? A:设置 ReadOnly 属性,可以设置的对象包括 DataGridViewRow(行).DataGridViewColumn(列).DataGridViewCel ...
- [Winform] DataGridView 中 DataGridViewComboBox 的可编辑
在 DataGridView 中设置的 DataGridViewComboBox,默认是不可编辑的,即使将其列属性 DisplayStyle 设置成 ComboBox 或其他,也无法编辑: 故作如下处 ...
随机推荐
- SpringMVC学习总结(七)——SpringMVC注解@..详解
<context:component-scan/> 扫描指定的包中的类上的注解,常用的注解有: @Controller 声明Action组件@Service 声明Service组件 ...
- haproxy重启
ps -aux | grep haproxy.cfg sudo kill -9 pidsudo /data/tools/haproxy/sbin/haproxy -f /data/tools/hapr ...
- iOS开发多线程--(NSOperation/Queue)
iOS实现多线程的方式有三种,分别是NSThread.NSOperation.GCD. 关于GCD,请阅读GCD深入浅出学习 简介 NSOperation封装了需要执行的操作和执行操作所需的数据,提供 ...
- JavaPersistenceWithHibernate第二版笔记Getting started with ORM-002Domain层详解及M etaModel
一.结构 二.配置文件约定 The JPA provider automatically picks up this descriptor if you place it in a META-INF ...
- 在CentOS 7中安装与配置Tomcat-8方法
安装前提 在CentOS 7中安装与配置JDK8 安装tomcat apache-tomcat-8.0.14.tar.gz文件上传到/usr/local中执行以下操作: [root@localhos ...
- USACO Section 3.2: Stringsobits
这题看了网上的答案的.还是很巧妙的 /* ID: yingzho1 LANG: C++ TASK: kimbits */ #include <iostream> #include < ...
- poj - 2431 Expedition (优先队列)
http://poj.org/problem?id=2431 你需要驾驶一辆卡车做一次长途旅行,但是卡车每走一单位就会消耗掉一单位的油,如果没有油就走不了,为了修复卡车,卡车需要被开到距离最近的城镇, ...
- 【AStar】初赛第一场
1. All X1.1 基本思路k和c的范围都不大,因此可以考虑迭代找循环节,然后求余数,判定是否相等.这题还是挺简单的.1.2 代码 /* 5690 */ #include <iostream ...
- 第十篇 PO核心功能及流程详解
详见链接:http://bbs.erp100.com/thread-272866-1-1.html1. P2P lifecycleP2P是procure to pay的缩写,p2p循环值得就是采购到付 ...
- Hadoop集群(第10期)_MapReduce与MySQL交互
2.MapReduce与MySQL交互 MapReduce技术推出后,曾遭到关系数据库研究者的挑剔和批评,认为MapReduce不具备有类似于关系数据库中的结构化数据存储和处理能力.为此,Google ...