Solved

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

Posted on 2011-03-17
5
674 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I hide MVVM menu items in WPF/XAML? 1 39
how to check mis-spellings in a select SQL 2 39
Return array 3 21
Telerik RadEditor Control Save 8 20
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

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