• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 833
  • Last Modified:

ASP.NET GridView RowDataBound event

How to get access to the cell using column name.
Thanks.


0
quasar_ee
Asked:
quasar_ee
  • 2
  • 2
3 Solutions
 
Dustin HopkinsCommented:
The method below isn't so much based on gridview column name but instead the dataitem name(database column name).
Don't forget to run a check to see if the value is dbnull

Hope this helps,
Dustin
If e.Row.RowType = DataControlRowType.DataRow Then
 ' you can use a two line method 
      Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)
      ' Retrieve the state value for the current row. 
      Dim state As String = rowView("state").ToString()

'or a one liner
Dim state1 As String = DataBinder.Eval(e.Row.DataItem, "state")
end if

Open in new window

0
 
MikeMCSDCommented:

use : "(DataBinder.Eval(e.Row.DataItem, "fieldName")"

protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      // example :
      if ((DataBinder.Eval(e.Row.DataItem, "fieldName")).ToString() == "Joe"
      {
        // to put field contents into a label:
        Label link1 = (Label)e.Row.FindControl("lblLink1");
        link1.Text = (DataBinder.Eval(e.Row.DataItem, "fieldName")).ToString()

    ....

Open in new window

0
 
Dustin HopkinsCommented:
Oops didn't notice the language.
if (e.Row.RowType == DataControlRowType.DataRow) {
// you can use a two line method 
	DataRowView rowView = (DataRowView)e.Row.DataItem;
	// Retrieve the state value for the current row. 
	string state = rowView["state"].ToString();

	//or a one liner
	string state1 = DataBinder.Eval(e.Row.DataItem, "state");
}

Open in new window

0
 
quasar_eeAuthor Commented:
Thank you.
Do you know how to get access to the Cell object in e.Row.Cells collection using DataView column name or database column name?
0
 
quasar_eeAuthor Commented:
thx
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now