动态添加列和行

方法一

通过手动添加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的初始化的更多相关文章

  1. winform DataGridView 通用初始化

    void DGV_Init() { //名称 类型 设备数 累计转发次数 累计转发数据数 状态 ; i < ; i++) { DataGridViewTextBoxColumn dc = new ...

  2. c# DataGridView操作

    #region 操作DataGridView /// <summary> /// 初始化DataGridView属性 /// </summary> /// <param ...

  3. DataGridView 使用精华

    DataGridView控件用法合集 1. 当前的单元格属性取得.变更 [C#] //显示当前单元格的值 Console.WriteLine(DataGridView1.CurrentCell.Val ...

  4. DataTable.Select

    转载请注明出处:http://www.cnblogs.com/havedream/p/4453297.html 方法:DataTable.Select 作用:获取 DataRow 对象的数组. 重载: ...

  5. C# WinForm自定义通用分页控件

    大家好,前几天因工作需要要开发一个基于WinForm的小程序.其中要用到分页,最开始的想法找个第三方的dll用一下,但是后来想了想觉得不如自己写一个玩一下 之前的web开发中有各式各样的列表组件基本都 ...

  6. DataGridView初始化,加载数据

    1,创建winform窗体应用程序 2,在界面上拖入DataGridView控件 3,添加相应的列如图: 4,开始编写后面的代码: private DataTable CountryDt = new ...

  7. [WinForm] DataGridView 绑定 DT && ComboBox 列绑定 Dict

    一  需求介绍 一般像枚举类型的数据,我们在数据库里存储着诸如(1.2.3.4-)或者("001"."002"."003"-)此类,但是界面 ...

  8. [Winform] DataGridView 总结(FAQ)

    Q1.  如何使单元格不可编辑? A:设置 ReadOnly 属性,可以设置的对象包括 DataGridViewRow(行).DataGridViewColumn(列).DataGridViewCel ...

  9. [Winform] DataGridView 中 DataGridViewComboBox 的可编辑

    在 DataGridView 中设置的 DataGridViewComboBox,默认是不可编辑的,即使将其列属性 DisplayStyle 设置成 ComboBox 或其他,也无法编辑: 故作如下处 ...

随机推荐

  1. Fuzzy test

    参考:http://baike.baidu.com/view/3679678.htm http://en.wikipedia.org/wiki/Fuzz_testing 就是模糊测试,在网上看到也叫f ...

  2. SQLHelper.cs的经典代码-存储过程

    using System; using System.Collections.Generic; using System.Text; using System.Collections; using S ...

  3. 一个简单的以User权限启动外部应用程序(用NetUserAdd函数和USER_INFO_1结构体动态添加用户,然后用CreateProcessWithLogonW启动程序)

    版权声明:本文为博主原创文章,未经博主允许不得转载. BOOL ExecuteAsUser(LPCWSTR lpszUserName, LPCWSTR lpszPassword, LPCWSTR lp ...

  4. mysql和oracle的一个汉字占几个字符

    以前一直使用oracle11g,一个汉字占3个字节,所以在操作mysql时也一直这样分配长度. 今天测试了下发现不对了 可以看到第一个的长度确实是15,但是第二个为什么是5? 在网上找到资料:char ...

  5. Redis的hash操作

    user name lisi age 30 height 175 public class User { private String name; private int age; private i ...

  6. Android 时间戳简单转化

    时间戳就是如1377216000000 这种格式我们在mysql数据库中会经常用到把时间转换成时间戳或把时间戳转换成日期格式了,下面我来介绍安卓中时间戳操作转换方法. 一.原理 时间戳的原理是把时间格 ...

  7. ubuntu下搭建hive(包括hive的web接口)记录

    Hive版本 0.12.0(独立模式) Hadoop版本 1.12.1 Ubuntu 版本 12.10 今天试着搭建了hive,差点迷失在了网上各种资料中,现在把我的经验分享给大家,亲手实践过,但未必 ...

  8. 可视化MNIST之降维探索Visualizing MNIST: An Exploration of Dimensionality Reduction

    At some fundamental level, no one understands machine learning. It isn’t a matter of things being to ...

  9. linux下svn的常用代码【转】

    转自:http://www.2cto.com/os/201205/130192.html 1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录)    例 ...

  10. Java实体书写规范

    ** * 用户角色表 */ public class BaseUserRole implements Serializable { private static final long serialVe ...