C# dataGridView的使用

来源:互联网 发布:服务器远程桌面端口 编辑:程序博客网 时间:2024/06/06 13:56

1、往dataGridView中添加多行数据:

               for (int i = 0; i < listCar.Count; i++)                {                    Car car = listCar[i];                    int index = this.dataGridView1.Rows.Add();                    this.dataGridView1.Rows[index].Cells[0].Value = car.Type;                    this.dataGridView1.Rows[index].Cells[1].Value = car.CarType;                    this.dataGridView1.Rows[index].Cells[2].Value = car.PlateNum;                    this.dataGridView1.Rows[index].Cells[3].Value = car.Price;                    this.dataGridView1.Rows[index].Cells[4].Value = car.IsDelete;                }
2、清空DataGridView中的所有行:

               this.dataGridView1.Rows.Clear();

3、删除DataGridView中指定行:

                this.dataGridView1.Rows.RemoveAt(1);

4、删除最后一行空白行:

将属性中AllowUserToAddRows置为为false。


5、往DataGridView添加一列按钮:

                DataGridViewButtonColumn button = new DataGridViewButtonColumn();                button.HeaderText = "操作";//列标题名                button.Text = "删除";//按钮显示名字                button.UseColumnTextForButtonValue = true;                this.dataGridView1.Columns.AddRange(button);
效果:

5.1、对所添加的按钮进行事件监听:

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)        {            int CIndex = e.RowIndex;//获取所点击的行数            MessageBox.Show(CIndex+"");//使用提示框显示当前行数        }

效果:



0 0
原创粉丝点击