Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 441
Alexander Avatar asked Apr 24 '12 09:04

Alexander


People also ask

How to get GridView row value in RowCommand event?

int index = Convert. ToInt32(e. CommandArgument); GridViewRow row = gvCurrentBanners. Rows[index];

How to get cell value of GridView in asp net on row databound?

Get cell value of GridView in RowCommand event in ASP.Net The row index can be easily determined using the CommandArgument property of GridViewCommandEventArgs object and using the row index, the GridView Row is referenced. Finally using the reference of the GridView Row, the values from the cells are fetched.


1 Answers

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.

enter image description here

like image 68
giri-webdev Avatar answered Sep 21 '22 22:09

giri-webdev