赞
踩
代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DataGridView { public partial class Form1 : Form { int count = 0; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //根据Header和所有单元格的内容自动调整行的高度 dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; //添加三列 for (int i = 0; i < 3; i++) { dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()); dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; //设置所有列自适应宽度 } //三列的标题 dataGridView1.Columns[0].HeaderText = "序号"; dataGridView1.Columns[1].HeaderText = "日期"; dataGridView1.Columns[2].HeaderText = "说明"; //设置对齐方式和字体 dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView1.Font = new Font("宋体", 11); } //添加行 private void button1_Click(object sender, EventArgs e) { int index = this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].Cells[0].Value = count++; this.dataGridView1.Rows[index].Cells[1].Value = "2"; this.dataGridView1.Rows[index].Cells[2].Value = "监听"; } //获取信息 private void button2_Click(object sender, EventArgs e) { //获取当前活动单元格内容 Console.WriteLine("当前活动单元格内容:" + dataGridView1.CurrentCell.Value); //取得当前单元格的列Index Console.WriteLine("当前单元格的列Index:" + dataGridView1.CurrentCell.ColumnIndex); //取得当前单元格的行Index Console.WriteLine("取得当前单元格的行Index:" + dataGridView1.CurrentCell.RowIndex); // 设定 (1, 1) 为当前单元格 dataGridView1.CurrentCell = dataGridView1[1, 1]; } } }
测试:
点击添加:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。