如何坐上开往事件行数据的单元格的值?以及如何检查如果单元格是空的? [英] How to get value of a cell in row data bound event? and how to check if a cell is empty?

查看:123
本文介绍了如何坐上开往事件行数据的单元格的值?以及如何检查如果单元格是空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SqlDataSource和一个 GridView控件
我想在GridView中的的RowDataBound 事件单元格的值?因为我不能使用 e.RowIndex



如何在updatng事件检查单元格是空的?
我用如果!= NULL ,但它没有工作,所以我需要检查,如果它是空的。



感谢


解决方案

在RowDataBound事件,你可以使用下面的代码gridview的一个单元格的值:



[ 1 ] //用户名获得特别rfom行

 字符串SERVICENAME = Convert.ToString(DataBinder.Eval的(e.Row.DataItem,姓名)); 

在RowUpdating事件,您可以检查,如果单元格为空或不使用下面的代码:

 字符串SERVICENAME = grdvw_showdetails.DataKeys [row.RowIndex] [1]的ToString(); 



以上代码的行更新事件中使用Datakey。如果你不希望使用datakey,代码检查特定的细胞是空的或为



 文本框txtaname =(文本框)row.FindControl(txt_updname); 

如果(txtaname.text!= NULL)






编辑:



这答案是优秀的。不过,我想补充一点评论的。当检查的RowDataBound 事件中的行单元格的数据,该行的的DataItem 的ItemArray属性是不能直接访问。所以,当我们做这样的事,这是毫无意义:字符串VAL = e.Row.Cells [2] .Text.ToString(); 并引发错误。

这就是这个答案的第一行的用武之地。[ 1 ]



下面的画面显示当你在调试模式手表行的基本属性的树/层次。




I'm using sqldatasource and a GridView. I want to get the value of a cell from the GridView in the RowDataBound event?, Because I can't use e.RowIndex.

How to check in the updatng event if a cell is empty? I used if != null, but it didn't worked so I need to check if it's empty.

thanks

解决方案

In the RowdataBound Event, you can get the value of a cell from gridview using the following Code:

[1]//getting username rfom particular row

string servicename = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Name"));

In the RowUpdating event, you can check if cell is empty or not, by using following code:

string servicename = grdvw_showdetails.DataKeys[row.RowIndex][1].ToString();

Above code uses Datakey in the row updating event. if you don't want to use the datakey, the code to check specific cell is empty or not is

 TextBox txtaname = (TextBox)row.FindControl("txt_updname");

 if(txtaname.text != null)


EDIT:

This answer is excellent. However I would like to add a bit of a comment. When checking row cells data within RowDatabound event, the row's DataItem's ItemArray property is not directly accessible. So when we do something like this, it's pointless : string val = e.Row.Cells[2].Text.ToString(); and throws an error.

That's where the first line of this answer comes in.[1]

Following screen shows the tree/hierarchy of the Row's underlying properties when you do a watch at debug mode.

这篇关于如何坐上开往事件行数据的单元格的值?以及如何检查如果单元格是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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