如何将单元格值从datagridview传递给变量 [英] How to pass a cell value to a variable from datagridview

查看:142
本文介绍了如何将单元格值从datagridview传递给变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我无法将单元格[1]的值赋给变量v_orderno。我得到的错误就像'bool没有单元格的定义..'。

In the following code I am unable to assign the value at cell[1] to a variable v_orderno. I get error like 'bool does not have a definition for cells..'.

protected void dataGridView1_CellClick (object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            if (dgv.CurrentRow.Selected)
            {
                v_orderno = (dgv.CurrentRow.Selected.Cells[1].Text);
            }

推荐答案

1.由于您使用的是类型为bool的错误属性,因此生成错误: dgv.CurrentRow.Selected



2.您应该使用以下方法访问您的行单元格: dw。 CurrentRow.Cells [1]

或更好直接使用: dw.CurrentCell
1.Your error is generated because you are using a wrong property that has type bool: dgv.CurrentRow.Selected

2.You should access your row cells by using: dw.CurrentRow.Cells[1]
or better by using directly: dw.CurrentCell


试试这个...

try this...
protected void dataGridView1_CellClick (object sender, DataGridViewCellEventArgs e)
{
     int orderno = 0;
     Int32.TryParse(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(), out orderno);
     v_orderno = orderno;
}



快乐编码!

:)


Happy Coding!
:)


这篇关于如何将单元格值从datagridview传递给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆