private void FrmLog_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = SQLiteHelper.GetTable("select * from log");
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.AutoGenerateColumns = false; dataGridView1.ReadOnly = true; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView1.AllowUserToAddRows = false;}
在dataGridView1的RowPostPaint事件中,添加下如下代码。
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor); e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4); }
获取选中行的值
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{ if (e.RowIndex > -1) { int rowindex = e.RowIndex; txtLogId.Text = dataGridView1.Rows[rowindex].Cells[0].Value.ToString(); //获得当前行的第1列的值 } }