采用DataGridView的CellClick事件去设置行被选中及其绑定文本框:
this.dataGridView1.Rows[e.RowIndex].Selected = true;
文本框.Text = this.dataset.Tables[0].Rows[this.dataGridView1.CurrentCell.RowIndex][0].ToString();
文本框.Text = this.dataset.Tables[0].Rows[this.dataGridView1.CurrentCell.RowIndex][1].ToString();
DataGridView选中一行进行删除操作:
DialogResult dr = MessageBox.Show("确定删除?","提示",MessageBoxButtons.OKCancel);
if(dr==DialogResult.OK)
{
this.dataset.Tables[0].Rows[this.dataGridView1.CurrentCell.RowIndex].Delete();
this.db.UpdateDataSet(this.dataset);
}
this.data.rows[e.rowindex].selected=true;
绑定DataGridView控件的方法:
this.dataset=this.dbhelp.方法名(SQL语句);
this.datagridview.DataSource=this.dataset.Tables[0].DefaultView;
DataGridView的全行选中:
this.dataGridView1.Rows[e.RowIndex].Selected = true;
注意:下列代码中的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);
}