C# 如何在行数据绑定事件中获取单元格的值?以及如何检查单元格是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10295401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:14:25  来源:igfitidea点击:

How to get value of a cell in row data bound event? and how to check if a cell is empty?

c#asp.netsqlwebformsrowdatabound

提问by Alexander

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

我正在使用 sqldatasource 和GridView。我想从RowDataBound事件中的 GridView 获取单元格的值?,因为我不能使用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.

如果单元格为空,如何检查更新事件?我用过if != null,但没有用,所以我需要检查它是否为空。

thanks

谢谢

采纳答案by giri-webdev

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

在 RowdataBound 事件中,您可以使用以下代码从 gridview 获取单元格的值:

[1]//getting username rfom particular row

[ 1]//从特定行获取用户名

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:

在 RowUpdating 事件中,您可以使用以下代码检查单元格是否为空:

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

上面的代码在行更新事件中使用了 Datakey。如果您不想使用数据键,则检查特定单元格是否为空的代码是

 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 RowDataboundevent, 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.

这个答案很棒。不过我想补充一点评论。在RowDatabound事件中检查行单元格数据时,DataItem不能直接访问该行的ItemArray 属性。所以当我们做这样的事情时,这是毫无意义的:string val = e.Row.Cells[2].Text.ToString();并抛出一个错误。

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

这就是这个答案的第一行。[ 1]

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

当您在调试模式下进行观察时,以下屏幕显示了 Row 底层属性的树/层次结构。

enter image description here

在此处输入图片说明