========================================================================================
==重点需要掌握==
AutoSizeColumnsMode 设置为File 列填充效果
RowHeadersVisible 设置false 第一列不显示
SelectionModel 设置FullRowSelect 行被选中
AllowUserToResizeColumns 设置False DataGridView是否可以调整列的大小
AllowUserToResizeRows 设置False DataGridView是否可以调整行的大小:
绑定DataGridView控件的方法:
this.datagridview.DataSource=DBHelper.ExecuteTable("sql语句").DefaultView;
取DataGridView的值绑定到文本框
实现方式:在CellClick事件中实现如下代码
方法1
int rowIndex=this.dataGridView控件.CurrentCell.RowIndex;
this.文本框名.Text = this.dataGridView控件[0,rowIndex].Value.ToString();
this.文本框名.Text = this.dataGridView控件[1,rowIndex].Value.ToString();
========================================================================================
注意:下列代码中的i是定义成全局变量
///[第一行] 代码如下:
//获取选中行的索引值
this.i = this.dataGridView1.SelectedCells[0].RowIndex;
if (this.i <= 0)
{
MessageBox.Show("已经是第一行了!",
"提示",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
this.dataGridView1.Rows[i].Selected = false;
this.dataGridView1.Rows[0].Selected = true;
}
///[下一行] 代码如下:
//获取选中行的索引值
this.i = this.dataGridView1.SelectedCells[0].RowIndex;
if (this.i < this.dataGridView1.RowCount - 1)
{
this.dataGridView1.Rows[i].Selected = false;
this.dataGridView1.Rows[++i].Selected = true;
}
else
{
MessageBox.Show("已经是最后一行了!",
"提示",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
///[上一行] 代码如下:
//获取选中行的索引值
this.i = this.dataGridView1.SelectedCells[0].RowIndex;
if (this.i <= 0)
{
MessageBox.Show("已经是第一行了!",
"提示",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
this.dataGridView1.Rows[i].Selected = false;
this.dataGridView1.Rows[--i].Selected = true;
}
///[最后行] 代码如下:
//获取选中行的索引值
this.i = this.dataGridView1.SelectedCells[0].RowIndex;
if (this.i < this.dataGridView1.RowCount - 1)
{
this.dataGridView1.Rows[i].Selected = false;
this.dataGridView1.Rows[this.dataGridView1.RowCount - 1].Selected = true;
}
else
{
MessageBox.Show("已经是最后一行了!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}