日期:2014-05-18  浏览次数:20687 次

datagridview某行里面各单位的值
我现在用datagridview绑定了数据库中的一张表
在datagridview中选中某行后,我要把这一行中的数据分别取出,然后赋值给几个textbox
可是除一开始赋值的某列以后的列都不能赋值,这是为什么呢

比如说
txtItem.Text = dgrFilter.CurrentRow.Cells[1].Value.ToString();
txtPrice.Text =dgrFilter.CurrentRow.Cells[2].Value.tostring();
txtAmount.Text = dgrFilter.CurrentRow.Cells[3].Value.ToString();

这种情况下txtitem能够得到值
下面的textbox就出错了,应该怎么解决。

------解决方案--------------------
dgrFilter.CurrentRow.Cells[1]
那就说明 cell[2]
,没有!
------解决方案--------------------
首先要确认你取出来的表的列足够你所需要的(你的例子中最少要4列),再确定Cell[i].Value是不是null,如果是null就不用调用ToString()了,
楼主最好把错误信息贴出来,看下报的是什么错。
------解决方案--------------------
txtItem.Text = dgrFilter.CurrentRow[dgrFilter.CurrentCell.RowIndex].Cells[1].Value.ToString();
txtPrice.Text =dgrFilter.CurrentRow[dgrFilter.CurrentCell.RowIndex].Cells[2].Value.tostring();
txtAmount.Text = dgrFilter.CurrentRow[dgrFilter.CurrentCell.RowIndex].Cells[3].Value.ToString(); 

这样?
是不是你读取的有空值?

试试加上
try
{
txtItem.Text = dgrFilter.CurrentRow[dgrFilter.CurrentCell.RowIndex].Cells[1].Value.ToString();
}
cacth{}