Solved

ASP.NET GridView RowDataBound event

Posted on 2010-09-23
5
772 Views
Last Modified: 2012-05-10
How to get access to the cell using column name.
Thanks.


0
Comment
Question by:quasar_ee
  • 2
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
Dustin Hopkins earned 334 total points
ID: 33750789
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
 
LVL 16

Assisted Solution

by:MikeMCSD
MikeMCSD earned 166 total points
ID: 33751014

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
 
LVL 14

Assisted Solution

by:Dustin Hopkins
Dustin Hopkins earned 334 total points
ID: 33753084
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
 

Author Comment

by:quasar_ee
ID: 33755603
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
 

Author Closing Comment

by:quasar_ee
ID: 33843827
thx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now