Solved

Get value of e.Row.Cells[2] and write to Label1

Posted on 2011-03-17
5
676 Views
Last Modified: 2013-12-17
Please help me solve this... I want to get data from e.Row.Cells[2] and send value to Label1 on .aspx see below... Thank you!


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            string CellValue = (e.Row.Cells[1].Text);
            string CellValue2 = (e.Row.Cells[0].Text);


            if (CellValue2 == "BMW")
 ------->>>>>               Label1.Text = "value of e.Row.Cell[2]";   <<<<<------ How do I write this?
               
            else if (CellValue == "FORD")
                e.Row.BackColor = System.Drawing.Color.Green;
            e

   
            }

        }
    }
0
Comment
Question by:portalvale
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 10

Expert Comment

by:Nash2334
ID: 35159638
To get the value of the cell the easiest thing to do would be to use an ItemTemplate in the Gridview with a Label, then find the Label using the FindControl method in the RowDataBound method.
0
 
LVL 10

Expert Comment

by:Nash2334
ID: 35159671
Example:

Your GridView:

                   <asp:TemplateField HeaderText="MyHeader">
                        <ItemTemplate>
                            <asp:Label ID="MyLabel" runat="server"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

In your RowDataBoundMethod:

Label myLabel = (Label)e.Row.FindControl("MyLabel"); // ID of the control you are seeking

if (myLabel != null)
    Label1.Text  = myLabel.Text;
0
 
LVL 7

Accepted Solution

by:
nbove earned 500 total points
ID: 35159734
It is not strictly required that you use a label.  If you wanted to avoid using a label control it would look something like this:

<asp:TemplateField HeaderText="MyHeader">
  <ItemTemplate>
    <%# Eval("DataboundField") %>
  </ItemTemplate>
</asp:TemplateField>

Label1.Text = e.Row.Cells[2].Text
0
 
LVL 10

Expert Comment

by:Nash2334
ID: 35159786
That's a good point nbove, but I tend to avoid using ordinals when referencing objects.  What happens if the template is modified and a column is added before the 3rd column?  The RowBound is suddenly referencing the wrong column, unless the code in that method is modified as well.

By using a Label and FindControl, you don't have to worry about column orders.  Whether the Label is in column 0 or column 100, the reference will never change in the RowBound method.
0
 

Author Closing Comment

by:portalvale
ID: 35159870
This is all I needed...

Label1.Text = e.Row.Cells[2].Text


Thx
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

696 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