11

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

bonCodigo
  • 14,268
  • 1
  • 48
  • 91
Alexander
  • 187
  • 2
  • 5
  • 15
  • add some sample code to show what u have done with code – sp_m Apr 24 '12 at 09:48
  • I'm not home and I don't have the code but I took the value from the texbox control of the cell for the gridview then chekc if is !=null ! . And for the first I don't know what shall I do – Alexander Apr 24 '12 at 09:57

1 Answers1

16

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

bonCodigo
  • 14,268
  • 1
  • 48
  • 91
giri-webdev
  • 525
  • 10
  • 20
  • +1 to OP and @giri. I don't know what I would do if it wasn't for SO community. It's good to be born later it seems ;) when people have gone through these walls and found great answers :D I just udpated your answer to give some visual impact, if you don't mind. – bonCodigo Jul 16 '14 at 10:44