c#中DataGridView简介

动态创建列和行

 DataGridView dataGridView2 = new DataGridView();
            dataGridView2.Dock = DockStyle.Fill;
            //动态添加列
            DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
            dataGridViewTextBoxColumn.HeaderText = "索引";
            DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn = new DataGridViewCheckBoxColumn();
            dataGridViewCheckBoxColumn.HeaderText = "变量";
            DataGridViewButtonColumn dataGridViewButtonColumn = new DataGridViewButtonColumn();
            dataGridViewButtonColumn.HeaderText = "参数";
            DataGridViewComboBoxColumn dataGridViewComboBoxColumn = new DataGridViewComboBoxColumn();
            dataGridViewComboBoxColumn.HeaderText = "别名";
            DataGridViewImageColumn image = new DataGridViewImageColumn();
            image.HeaderText = "别称";
            DataGridViewLinkColumn link = new DataGridViewLinkColumn();
            link.HeaderText = "雅号";
            dataGridView2.Columns.Add(dataGridViewTextBoxColumn);
            dataGridView2.Columns.Add(dataGridViewCheckBoxColumn);
            dataGridView2.Columns.Add(dataGridViewButtonColumn);
            dataGridView2.Columns.Add(dataGridViewComboBoxColumn);
            dataGridView2.Columns.Add(image);
            dataGridView2.Columns.Add(link);
            dataGridView2.Parent = tabPage3;//把dataGridView2放到tabPage3上
            //动态添加行


            DataGridViewRow row = new DataGridViewRow();//创建行对象
            DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
            textboxcell.Value = "1";
            row.Cells.Add(textboxcell);
            DataGridViewCheckBoxCell checkBoxCell = new DataGridViewCheckBoxCell();
            checkBoxCell.Value = true;
            row.Cells.Add(checkBoxCell);
            DataGridViewButtonCell buttonCell = new DataGridViewButtonCell();
            buttonCell.Value = "浏览"; //添加button没有事件,有什么用啊!
            row.Cells.Add(buttonCell);
            DataGridViewComboBoxCell comboBoxCell = new DataGridViewComboBoxCell();
            comboBoxCell.Items.Add("123");
            comboBoxCell.Items.Add("456");
            comboBoxCell.Value = "123";//必须是前边添加了
            row.Cells.Add(comboBoxCell);
            DataGridViewImageCell imageCell = new DataGridViewImageCell();
            Image imagetest = Image.FromFile("D:\\vsproject\\csharpproject\\WindowsFormsApplication\\FourTab\\hilworks\\instrment.png");
            imageCell.Value = imagetest;
            row.Cells.Add(imageCell);


            DataGridViewLinkCell linkCell = new DataGridViewLinkCell();
            linkCell.Value = "www.baidu.com";
            row.Cells.Add(linkCell);
            dataGridView2.Rows.Add(row);

//  dataGridView2.ReadOnly = true;//取消编辑 或如下图:


也可以手动创建完了列再创建行

//方法一
                        int index = this.dataGridView1.Rows.Add();
                        this.dataGridView1.Rows[index].Cells[0].Value = "1";
                        this.dataGridView1.Rows[index].Cells[1].Value = "糖醋排骨";
                        this.dataGridView1.Rows[index].Cells[2].Value = true;
                       // this.dataGridView1[0, 3].Value = "new value";
                       // this.dataGridView1.Rows[index].Cells[3].Value = "test";


                        //方法2
                        DataGridViewRow row = new DataGridViewRow();//创建行对象
                        DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
                        textboxcell.Value = "aaa";
                        row.Cells.Add(textboxcell);
                        DataGridViewButtonCell buttonCell = new DataGridViewButtonCell();
                        buttonCell.Value = "浏览"; //添加button没有事件,有什么用啊!
                        row.Cells.Add(buttonCell);
                        DataGridViewImageCell imageCell = new DataGridViewImageCell();
                        Image imagetest = Image.FromFile("D:\\vsproject\\csharpproject\\WindowsFormsApplication\\FourTab\\hilworks\\instrment.png");
                        imageCell.Value = imagetest;
                        row.Cells.Add(imageCell);
                        dataGridView1.Rows.Add(row);

 /* 方法二我该了,现编注释对不上
                         方法二比方法一要复杂一些,但是在一些特殊场合非常实用
                         * 例如,要在新行中的某些单元格添加下拉框、按钮之类的控件时,
                         * 该方法很有帮助。DataGridViewRow row = new DataGridViewRow();是创建DataGridView的行对象
                         * DataGridViewTextBoxCell是单元格的内容是个TextBox,
                         * DataGridViewComboBoxCell是单元格的内容是下拉列表框,
                         * 同理可知,DataGridViewButtonCell是单元格的内容是个按钮,等等。
                         * textboxcell是新创建的单元格的对象,可以为该对象添加其属性。
                         * 然后通过row.Cells.Add(textboxcell)为row对象添加textboxcell单元格。
                         * 要添加其他的单元格,用同样的方法即可。
                         * 最后通过dataGridView1.Rows.Add(row)为dataGridView1控件添加新的行row。
                         */

总是多一行可以使用属性

AllowUserToAddRows 设成false就没用了

如果相应单元格中的事件可以使用 CellClickCellContentClick
 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Rows[1].Cells[1].Selected)
            {
                MessageBox.Show("dfddddddddddddsgertfg");
            }
        }
如果想平均每列的宽度可以
 private void dateGridView1_Resize(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.ColumnCount; i++)
            {
                this.dataGridView1.Columns[i].Width = dataGridView1.Width / dataGridView1.ColumnCount;
            }
        }
如果想 列于列之间互换
可是把属性 AllowUserToOrderColumns=true,但这只改变了顺序,不能把第一个和第三个交换。
如果有人知道,请留言
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

woquNOKIA

谢谢老板!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值