//全局设置文本对齐方式。
dataGridView1.RowsDefaultCellStyle.Alignment= DataGridViewContentAlignment.MiddleCenter;
添加列时,必须设置列的CellTemplate,否则应该报错!
一、只添加text样式
1 //每一列必须设置CellTemplate
2 //第一列
3 dataGridView1.Columns.Add(new DataGridViewColumn() {Name="name", HeaderText = "姓名", Width = 100,CellTemplate = new DataGridViewTextBoxCell(), MinimumWidth=100});
4
5
6 //第二列
7 DataGridViewColumn ageColumn = new DataGridViewColumn()
8 {
9 Name = "age",
10 HeaderText = "年龄",
11 Width = 100,
12 CellTemplate = new DataGridViewTextBoxCell()
13 };
14 //设置文本对齐方式
15 ageColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
16 //设置该列背景颜色
17 ageColumn.DefaultCellStyle.BackColor = Color.Red;
18 dataGridView1.Columns.Add(ageColumn);
19
20 //添加行
21 for (int i = 0; i < 10; i++)
22 {
23 //第一种添加行的方式:得到添加新行的索引号,然后通过该索引号操作该行的各个单元格
24 //int index = dataGridView1.Rows.Add();
25 //dataGridView1.Rows[index].Cells["name"].Value = "宝宝"+i;
26 //dataGridView1.Rows[index].Cells["age"].Value = "宝年"+ i;
27
28 // 第二种添加行的方式:直接加入新行
29 dataGridView1.Rows.Add(new string[] { "宝宝" + i, "宝年" + i });
30
31 //第三种添加行的方式: 一个单元格一个单元格的加,这样方便为单独某个单元格赋值
32 //DataGridViewRow dr = new DataGridViewRow();
33 //DataGridViewTextBoxCell textcell1 = new DataGridViewTextBoxCell();
34 //textcell1.Value = "宝宝" + i;
35 //dr.Cells.Add(textcell1);
36 //DataGridViewTextBoxCell textcell2 = new DataGridViewTextBoxCell();
37 //textcell2.Value = "宝宝" + i;
38 //dr.Cells.Add(textcell2);
39 //dataGridView1.Rows.Add(dr);
40
41
42 }
View Code
二、添加DataGridViewCheckBoxCell
//全局设置文本对齐方式
//dataGridView1.RowsDefaultCellStyle.Alignment= DataGridViewContentAlignment.MiddleCenter;
//每一列必须设置CellTemplate
//第一列
dataGridView1.Columns.Add(new DataGridViewColumn() {Name="name", HeaderText = "姓名", Width = 100,CellTemplate = new DataGridViewTextBoxCell(), MinimumWidth=100});
//第二列
DataGridViewColumn ageColumn = new DataGridViewColumn()
{
Name = "age",
HeaderText = "年龄",
Width = 100,
CellTemplate = new DataGridViewTextBoxCell()
};
//设置文本对齐方式
ageColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//设置该列背景颜色
ageColumn.DefaultCellStyle.BackColor = Color.Red;
dataGridView1.Columns.Add(ageColumn);
//第三列,即CheckBox列
DataGridViewColumn sexColumn = new DataGridViewCheckBoxColumn()
{
Name = "sex",
HeaderText = "男女",
Width = 50,
CellTemplate = new DataGridViewCheckBoxCell(),
TrueValue =true,
FalseValue=false,
};
sexColumn.DefaultCellStyle.BackColor = Color.Beige;
sexColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns.Add(sexColumn);
//添加行
for (int i = 0; i < 10; i++)
{
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells["name"].Value = "宝宝" + i;
dataGridView1.Rows[index].Cells["age"].Value = i ;
//将所有的CheckBox单元格设置为选中状态。
DataGridViewCheckBoxCell dcc = (DataGridViewCheckBoxCell)dataGridView1.Rows[index].Cells["sex"];
if (!Convert.ToBoolean(dcc.Value))
{
dcc.Value = true;
}
}
View Code
添加上DataGridViewCheckBoxCell之后并没有完事,需要自己在dataGridView1的CellContentClick事件中来写点击该DataGridViewCheckBoxCell的选中状态哦。
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//确定是CheckBox的那一列
if (e.ColumnIndex == 2)
{
//得到该CheckBox单元格
DataGridViewCheckBoxCell dcc = dataGridView1.Rows[e.RowIndex].Cells["sex"] as DataGridViewCheckBoxCell;
//如果是选中状态就让其更换value的值(即不选中),否则,反之。
if (Convert.ToBoolean(dcc.Value))
{
dcc.Value = false;
}
else
{
dcc.Value = true;
}
}
}
View Code
三、添加combox列(含有另一种checkbox列的加法)
1 //用户自己调整列的宽度和高度
2 dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
3 //设置列头的高度
4 dataGridView1.ColumnHeadersHeight = 50;
5
6
7
8 //设置数据源
9 DataTable dt = new DataTable();
10 dt.Columns.Add("code");
11 dt.Columns.Add("name");
12 for (int i = 0; i < 5; i++)
13 {
14 DataRow dr = dt.NewRow();
15 dr["code"] = "code" + i;
16 dr["name"] = "宝族" + i;
17 dt.Rows.Add(dr);
18 }
19
20
21 //第1列
22 DataGridViewColumn nameColumn = new DataGridViewColumn()
23 {
24
25 Name = "name",
26 HeaderText = "名称",
27 CellTemplate = new DataGridViewTextBoxCell(),
28 Width = 100
29 };
30 nameColumn.DefaultCellStyle.BackColor = Color.Aqua;
31
32 dataGridView1.Columns.Add(nameColumn);
33 //第二列
34 DataGridViewColumn sexColumn = new DataGridViewCheckBoxColumn()//注意是Cloumn
35 {
36 Name = "sex",
37 HeaderText = "性别",
38 TrueValue = true, FalseValue = false,
39 CellTemplate = new DataGridViewCheckBoxCell(),//注意是Cell
40 Width = 100,
41 };
42 sexColumn.DefaultCellStyle.BackColor = Color.Bisque;
43 sexColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
44 dataGridView1.Columns.Add(sexColumn);
45 //第三列
46 DataGridViewColumn nationColumn = new DataGridViewComboBoxColumn()
47 {
48 Name = "nation",
49 HeaderText = "民族",
50 CellTemplate = new DataGridViewComboBoxCell(),
51 Width = 200,
52 FlatStyle=FlatStyle.Popup,
53 DisplayStyle=DataGridViewComboBoxDisplayStyle.DropDownButton,//设置显示样式
54
55 };
56 nationColumn.DefaultCellStyle.BackColor = Color.LightPink;
57
58 // nationColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
59 dataGridView1.Columns.Add(nationColumn);
60
61 //添加行数。checkbox和combox自动添加了,所有此处只添加第一列的行数即可
62 for (int i = 0; i < 10; i++)
63 {
64 DataGridViewRow dr = new DataGridViewRow();
65 //行的第一列
66 DataGridViewTextBoxCell nameCell = new DataGridViewTextBoxCell();
67 nameCell.Value = "宝宝" + i;
68 dr.Cells.Add(nameCell);
69 dataGridView1.Rows.Add(dr);
70 dataGridView1.Rows[i].Height = 50;//设置行高
71
72 }
73 //设置checkbox列和combox列
74 foreach (DataGridViewRow dr in dataGridView1.Rows)
75 {
76 //行的第二列
77 DataGridViewCheckBoxCell sexCell =dr.Cells["sex"] as DataGridViewCheckBoxCell;
78 sexCell.Value = true;
79 //行的第三列
80 DataGridViewComboBoxCell nationCell = dr.Cells["nation"] as DataGridViewComboBoxCell;
81 nationCell.DataSource = dt;
82 nationCell.DisplayMember = "name";
83 nationCell.ValueMember = "code";
84 nationCell.Value = "code2";//设置默认值。valueMember的值
85 nationCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;//设置展示样式
86 }
View Code
(1)此处可能会出现,点击最后一行的时候,表会自动添加一行。为了阻止,需
//防止控件自动添加新的一行
dataGridView1.AllowUserToAddRows = false;
(2)当点击combox列时,会发现,只有点击两侧才能出现下拉框,需在cell_click事件中
先设置DataGridvDataGridView的属性:EditMode=EditOnEnter,再添加:
1 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
2 {
3 if (e.ColumnIndex >= 0 && e.RowIndex >= 0 && dataGridView1[e.ColumnIndex, e.RowIndex] != null && !dataGridView1[e.ColumnIndex, e.RowIndex].ReadOnly)
4 {
5 DataGridViewComboBoxColumn comboBoxColumn = dataGridView1.Columns[e.ColumnIndex] as DataGridViewComboBoxColumn;
6 if (comboBoxColumn != null)
7 {
8 this.dataGridView1.CurrentCell = dataGridView1[e.ColumnIndex, e.RowIndex];
9 dataGridView1.BeginEdit(true);
10 DataGridViewComboBoxEditingControl comboBoxEditingControl = dataGridView1.EditingControl as DataGridViewComboBoxEditingControl;
11 if (comboBoxEditingControl != null)
12 {
13 comboBoxEditingControl.DroppedDown = true;
14 }
15 }
16 }
17 }
View Code
(3)取值
1 DataGridViewCheckBoxCell dcb = dataGridView1.Rows[0].Cells["sex"] as DataGridViewCheckBoxCell;
2 string dcbValue = dcb.Value.ToString();//获得某一单元格的checkbox的值
3 DataGridViewComboBoxCell dbc= dataGridView1.Rows[0].Cells["nation"] as DataGridViewComboBoxCell;
4 string dbcValue = dbc.Value.ToString();//获得某一单元格的combox的value值
5 MessageBox.Show(dcbValue);
完!