DataGridView操作

采用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);
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值