Solved

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

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 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