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 或其他,也无法编辑: 故作如下处 ...
随机推荐
- Hadoop基础教程-运行环境搭建
一.Hadoop是什么 一个分布式系统基础架构,由Apache基金会所开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储. Hadoop实现了一个分布式 ...
- html5 语音搜索
开始以为是接口什么的,原来这就是语言搜索. 只需要在input加上x-webkit-speech <input type="text" class="text&qu ...
- ajax:serialize() 获取表单集合
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- SQL语句 递归
--正向递归查询(根据ID查到自己和自己以下的所有数据) connect by prior a.id = a. parentid --反向递归查询(根据叶子ID查出自己和自己以上的根数据) ...
- 利用Apriori算法对交通路况的研究
首先简单描述一下Apriori算法:Apriori算法分为频繁项集的产生和规则的产生. Apriori算法频繁项集的产生: 令ck为候选k-项集的集合,而Fk为频繁k-项集的集合. 1.首先通过单遍扫 ...
- Android InputMethodManager输入法简介
正文 一.结构 public final class InputMethodManager extends Object Java.lang.Object android.view.inputmeth ...
- Asterisk的配置详解
Asterisk的配置文件都在/etc/asterisk目录下,重要的配置文件有: sip.conf sip电话基本配置 extensions.conf ...
- samba linux windows 请联系管理员
在使用Samba进行建立Window与Linux共享时,要是不能访问,出现“您可能没有权限使用网络资源”, 那就是SELinux在作怪了 要是想让共享目录能访问,可以使用命令 #setenforce ...
- 12.allegro环境设置[原创]
一.菜单简介 --- 分割电源,分割平面 ------- ------- ------- ----- --------- ---- --------------- ----------------- ...
- 基于Linux的oracle数据库管理 part1( 准备及linux基础 )
主要内容 1. 安装VMware tools (好处, 显示效果增强, 从虚拟机出来不需要alt+ctrl 切换) 2. Linux 启动过程, 方便以后oracle 自动启动与关闭 3. Linux ...