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);
DataGridView
控件在实际应用中非常实用,特别需要表格显示数据时。可以静态绑定数据源,这样就自动为
DataGridView
控件
添加
相应的
行
。假如需要
动态
为
DataGridView
控件
添加
新
行
,方法有很多种,下面简单介绍如何为
DataGridView
控件
动态
添加
新
行
的两种方法:
方法
一
:intindex=this.
dataGridView
1.Rows.Add();this.dataGri...
datagridview
添加
行
,在已经绑定数据源的情况下
(否则会报错误: 当控件被数据绑定时,无法以编程方式向
DataGridView
的
行
集合中
添加
行
)
方法
一
:
DataRow dr = ((DataTable)
dataGridView
1.DataSource).NewRow();
((DataTable)
dataGridView
1.DataSource).Rows.Add(dr)
DataGridView
控件在实际应用中非常实用,特别需要表格显示数据时。可以静态绑定数据源,这样就自动为
DataGridView
控件
添加
相应的
行
。假如需要
动态
为
DataGridView
控件
添加
新
行
,方法有很多种,下面简单介绍如何为
DataGridView
控件
动态
添加
新
行
的两种方法:
方法
一
:
int index...
可以静态绑定数据源,这样就自动为
DataGridView
控件
添加
相应的
行
。假如需要
动态
为
DataGridView
控件
添加
新
行
,方法有很多种,下面简单介绍如何为
DataGridView
控件
动态
添加
新
行
的两种方 法:
方法
一
: 代码如下:int index=this.
dataGridView
1.Rows.Add();this.
dataGridView
1.Rows[index].Cells[0].Value = “1”; this.
dataGridView
1.Rows[index].Cells[1].Value = “2”; this.
dataGridView
1.Rows[index].Cells
DataGridView
动态
添加
新
行
:
DataGridView
控件在实际应用中非常实用,特别需要表格显示数据时。可以静态绑定数据源,这样就自动为
DataGridView
控件
添加
相应的
行
。假如需要
动态
为
DataGridView
控件
添加
新
行
,方法有很多种,下面简单介绍如何为
DataGridView
控件
动态
添加
新
行
的两种方法:
方法
一
:
int index=this.
dataGridView
1.Ro...
#region
添加
列
(时间)
List<string> time;
private void SentryDataTime(string strStart, string strStop)
//deteall(时间参数1,时间参数2) 获取事时间数组 https://blog.c...
复制代码 代码示例:int index=this.
dataGridView
1.Rows.Add();
this.
dataGridView
1.Rows[index].Cells[0].Value = "1";
this.
dataGridView
1.Rows[index].Cells[1].Value = "2";
this.
dataGridView
1.Row
//全局设置文本对齐方式。
dataGridView
1.RowsDefaultCellStyle.Alignment=
DataGridView
ContentAlignment.MiddleCenter;
添加
列
时,必须设置
列
的CellTemplate,否则应该报错!
一
、只
添加
text样式
1 //每
一列
必须设置CellTemplate
2 ...
http://blog.csdn.net/ibmfahsion
张迅雷总结:
--------------------------------------------------------------------------------
C#
DataGridView
控件
动态
添加
新
行
DataG