https://blog.csdn.net/alisa525/article/details/7350471

dataGridView1.ReadOnly = true ;      //禁用编辑功能

方法一:通过手动添加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 = 0; i < 3; 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 = 4;
        dataGridView1.ColumnHeadersVisible = true;

// Set the column header style.
        DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();

columnHeaderStyle.BackColor = Color.Beige;
        columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Bold);
        dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;

// Set the column header names.
        dataGridView1.Columns[0].Name = "Recipe";
        dataGridView1.Columns[1].Name = "Category";
        dataGridView1.Columns[2].Name = "Main Ingredients";
        dataGridView1.Columns[3].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(0, column);

C# DataGridView 动态添加列和行的更多相关文章

  1. DataGridview动态添加列

    1.获取数据源(select * from table名称) 2.动态绑定数据源 private void GetTableInfo(DataTable dt) { listBh = new List ...

  2. Wpf DataGrid动态添加列,行数据(二)

    这是第二中方法,可直接绑定,我这里只是做出了一种思路,并不是最完美. 这里注意一下,因为我里面引用了MVVMLight,所以可能代码不是复制过去就能用了的. 样式也是,所以复制过去看不是我贴出来的界面 ...

  3. Wpf DataGrid动态添加列,行数据(一)

    由于最近有这方面的需求,而且刚接触wpf不久,在网上找了很多方法,都不是使用MVVM模式的,因为DataGrid的列不能绑定 这就难受了,我想了个折中的方法,这个是使用了MVVMLight的消息机制, ...

  4. C# DataGridView 动态添加列和调整列顺序

    https://yq.aliyun.com/articles/421700 // DataGridView1的ColumnDisplayIndexChanged事件处理方法private void D ...

  5. asp.net gridview动态添加列,并获取其数据;

    1,绑定数据前先动态添加列,见方法CreateGridColumn(只在第一次加载动态添加): 2,gvlist_RowDataBound为对应列添加控件: 前台代码: <%@ Page Lan ...

  6. [转]RDLC报表——动态添加列

    本文转自:http://www.cnblogs.com/pszw/archive/2012/07/19/2599937.html 前言 最近接到一个需求:在给定的数据源中,某(些)列,可能需要单独统计 ...

  7. GridView动态添加列之后,导致PostBack(回发)页面数据丢失问题解决

    直入主题,首先声明,这个问题是无法解决的,特此在这说明 一.如何动态添加列,如下: 在页面重写OnInit事件,至于为什么要在这个事件写,根据页面的声明周期和经验可知(不用去别的地方找了,这个我找了之 ...

  8. C# ASP 动态添加Html Table行

    用JS放法实现以下效果: 前端文件Questionnaire23.aspx: <%@ Page Title="题目" Language="C#" Mast ...

  9. DataGridView动态添加新行的两种方法

    简单介绍如何为DataGridView控件动态添加新行的两种方 法: 方法一: int index=this.dataGridView1.Rows.Add();this.dataGridView1.R ...

随机推荐

  1. qemu通过控制台向虚拟机输入组合键

    ctrl+alt+2 开启控制台 ctrl+alt+1 关闭控制台 在控制台中输入 sendkey ctrl-alt-delete 发送指令

  2. VUE导入Excel

    import FilenameOption from './components/FilenameOption' import AutoWidthOption from './components/A ...

  3. c代码审查软件

    1. Coccinelle http://coccinelle.lip6.fr/

  4. zookeeper在windows的常用命令

    首先简要讲下安装 安装jdk 安装Zookeeper. 在官网http://zookeeper.apache.org/下载zookeeper.我下载的是zookeeper-3.4.6版本. 解压zoo ...

  5. java 读取CSV数据并写入txt文本

    java 读取CSV数据并写入txt文本 package com.vfsd; import java.io.BufferedWriter; import java.io.File; import ja ...

  6. properties配置文件参数获取

    package com.opslab.util; import org.apache.log4j.Logger; import java.io.File;import java.io.IOExcept ...

  7. Flink 在IDEA执行时的webui

    不过Flink IDEA中执行的webui 需要 flink-runtime-web 包的支持 pom 如下: <dependency> <groupId>org.apache ...

  8. Swift4.0复习类型定义、类型投射等操作

    1.类型定义: /// 这里将MyInt定义为Int32类型 typealias MyInt = Int32   /// 这里将MyArrayInt定义为[MyInt]数组类型 typealias M ...

  9. jquery 单击选中 再次选中取消选中

    html: <div id="full" class='weui-popup__container' style="background: #fff"&g ...

  10. MultiDesk远程桌面连接

    MultiDesk 是一个选项卡(TAB标签)方式的远程桌面连接 (Terminal Services Client),可以管理组远程桌面连接,更改连接端口. 功能特性 绿色软件,只有一个很小的可执行 ...