dataGridView添加列行

//全局设置文本对齐方式。
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);

完!

 

转载于:https://www.cnblogs.com/wwz-wwz/p/7019283.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: DataGridView 是一个用于在 Windows 窗体应用程序中显示和编辑表格数据的控件。若要添加数据到 DataGridView 中,可以按照以下步骤进: 1. 通过 `DataGridView.Rows` 属性获取 DataGridView集合。 2. 使用 `Add` 方法添加一个新的空,例如:`dataGridView.Rows.Add()`。 3. 将数据填充到新添加中。可以通过索引和索引访问每个单元格并设置其值,例如:`dataGridView.Rows[rowIndex].Cells[columnIndex].Value = cellValue`。 完成上述步骤后,新的数据就会显示在 DataGridView 中。需要注意的是,为了确保 DataGridView 中的数据和数据源的同步,还需要更新数据源中的数据。 ### 回答2: DataGridView是Windows 应用程序中的一个常见控件,其功能十分强大,能够快速、方便地编辑和显示大量的数据。一个DataGridView控件可以包含数千数据,但是在实际的使用中,我们往往需要手动向这些数据中添加或删除一些数据。下面,我将介绍如何在DataGridView添加数据。 DataGridView的数据是通过DataTable或者BindingSource对象来绑定和显示的。所以,在添加数据之前,首先需要创建一个DataTable对象或者BindingSource对象,并将其绑定到DataGridView控件上。 以下是使用DataTable对象绑定DataGridView的示例代码: ```C# //创建DataTable对象 DataTable dt = new DataTable("MyTable"); //向DataTable中添加 dt.Columns.Add("Column1", typeof(int)); dt.Columns.Add("Column2", typeof(string)); dt.Columns.Add("Column3", typeof(DateTime)); //将DataGridView控件与DataTable绑定 dataGridView1.DataSource = dt; ``` 在绑定好DataTable对象之后,就可以通过以下代码向DataGridView添加数据: ```C# //创建一数据 DataRow row = dt.NewRow(); //为每个赋值 row["Column1"] = 1; row["Column2"] = "Hello World"; row["Column3"] = DateTime.Now; //将这数据添加到DataTable中 dt.Rows.Add(row); ``` 以上代码中,首先创建了一个新的DataRow对象,并为其各个赋值,然后通过DataTable的Rows属性将这数据添加到DataTable中。此时就可以在DataGridView中看到新添加数据了。 当然,如果使用的是BindingSource对象来绑定DataGridView,那么上述代码稍作修改即可使用。只需要将第一步创建DataTable对象的代码修改为: ```C# BindingSource bs = new BindingSource(); bs.DataSource = typeof(MyDataModel); dataGridView1.DataSource = bs; ``` 此时,将DataRow对象添加到BindingSource对象的DataSource属性中即可。 ### 回答3: DataGridView是Windows Forms中的一个控件,用于展示和编辑数据。在添加的操作中,我们需要先确定数据源。如果是绑定数据源,可以通过对数据源添加,再刷新DataGridView来实现。如果是手动添加,需要首先创建一个新的DataGridViewRow对象,添加数据到该中,再将该添加DataGridView控件中。下面我们分别介绍这两种方法。 一、绑定数据源 1.添加数据到数据源 首先确定数据源,例如一个DataTable,我们可以通过如下代码添加数据到该表: ``` DataTable dt = dataGridView1.DataSource as DataTable; //获取数据源为DataTable的DataGridView DataRow newRow = dt.NewRow(); //创建新 newRow["名"] = "新数据"; //设置新数据 dt.Rows.Add(newRow); //将新添加到数据源中 ``` 2.刷新DataGridView添加完数据到数据源之后,我们需要调用DataGridView的Refresh方法来刷新控件,从而在UI中看到新增的数据。 ``` dataGridView1.Refresh(); //刷新DataGridView ``` 二、手动添加 1.创建新的DataGridViewRow对象 我们首先需要创建一个新的DataGridViewRow对象,用于存储我们要添加的数据。 ``` DataGridViewRow newRow = new DataGridViewRow(); //创建新 ``` 2.添加数据到该中 然后我们需要将数据添加到该的单元格中,这可以通过设置单元格的Value属性来实现,例如: ``` newRow.Cells[0].Value = "数据1"; //设置第一单元格数据 newRow.Cells[1].Value = "数据2"; //设置第二单元格数据 ``` 3.将该添加DataGridView控件中 最后,我们需要将该添加DataGridView控件中,这可以通过使用Add方法来实现,例如: ``` dataGridView1.Rows.Add(newRow); //将新添加DataGridView ``` 以上就是DataGridView添加数据的两种方法,可以根据实际需求来选择哪种方法来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值