datagridview显示问题

1.获取索引行与显示问题
int index = dataGridView1.CurrentRow.Index;    //获取点击索引行
dataGridView1.Rows[index].Selected = true;     //加亮显示
string curNo = this.dataGridView1.Rows[index].Cells[0].Value.ToString();   //取索引行的第一个单元格内容
int r = Convert.ToInt32(this.dataGridView1["Record_no", index].Value.ToString());
string no = this.dataGridView1["InBound_no", index].Value.ToString();
2.
 int i = dataGridView1.CurrentRow.Index;       //获取当前记录的索引号 
 this.dataGridView1.Rows[i].Selected = true;  //加亮显示
 string curN = this.dataGridView1["ZGOutNo", i].Value.ToString();
3.
 private void showData()
        {
            try
            {
                conn = new OracleConnection(ConnectionString);
                da = new OracleDataAdapter("SELECT Record_no,Tool_no,entrustDate,finishDate,Factory,beforeSize1,afterSize1,beforeSize2,afterSize2,beforeSize3,"
                    + "afterSize3,beforeSize4,afterSize4,beforeSize5,afterSize5,importer,Insert_Time FROM T_ZG_maintenance", conn);
                ds = new DataSet();
                da.Fill(ds);
                dataGridView2.DataSource = ds;
                dataGridView2.DataMember = ds.Tables[0].ToString();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                ds.Dispose();
            }
        }
4.
 private void ShowData()
        {
            try
            {
                 conn = new OracleConnection(ConnectionString);
                da = new OracleDataAdapter(string.Format("SELECT Record_no,Tool_no,entrustDate,finishDate,Factory,beforeSize1,afterSize1,beforeSize2,afterSize2,beforeSize3,"
                    + "afterSize3,beforeSize4,afterSize4,beforeSize5,afterSize5,importer,Insert_Time FROM T_ZG_maintenance Where Tool_no='{0}'", cmbTool_no.Text), conn);
                ds = new DataSet();
                da.Fill(ds);
                da.Dispose();
                this.bindingSource2.DataSource = ds.Tables[0];
                this.bindingNavigator2.BindingSource = bindingSource2;
                this.dataGridView2.DataSource = bindingSource2;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                ds.Dispose();
            }
        }
5.点击datagridview索引超出范围,必须为非负值并小于集合大小。
dgvTicketInfo.SelectedRows[0].Cells["ID"].Value报错
因为dgvTicketInfo.SelectedRows的默认selectionMode是 RowHeaderSelect,如果你点击行中某一个单元格,是选不到整行的!所以dgvTicketInfo.SelectedRows.Count实际上是零。你的那句话:
dgvTicketInfo.SelectedRows[0] 肯定要报索引超出范围了!
可以把selectionMode设成整行选择!当然也可以改进你的定位行和单元格的方式!比如通过当前单元格的行列下标,定位你的“ID”也可以!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮特大熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值