Solved

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

Posted on 2011-03-17
5
665 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
  • 3
5 Comments
 
LVL 10

Expert Comment

by:Nash2334
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
This is all I needed...

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


Thx
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

14 Experts available now in Live!

Get 1:1 Help Now