Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# syntax to get value from Grid row

Posted on 2013-02-04
4
Medium Priority
?
448 Views
Last Modified: 2013-02-10
I have a Rad Grid with a Hyperlinkcolumn that I would like to have a conditional image displayed based on the value of another field. I am getting all kinds of errors and was hoping for some guidance.

My ASPX page is

                <telerik:GridHyperLinkColumn AllowFiltering="False" AllowSorting="False" DataNavigateUrlFields="AUTOID" DataNavigateUrlFormatString="MCDPermitView.aspx?ID={0}" DataTextField="AUTOID" EditFormHeaderTextFormat="test {0}:" FilterControlAltText="Filter AUTOIDCOLUMN column" HeaderText=" " ImageUrl="/images/Info-Small.png" UniqueName="AUTOIDCOLUMN">

                    <HeaderStyle Width="30px" />

                </telerik:GridHyperLinkColumn>





                <telerik:GridBoundColumn DataField="GIS" Display="False" FilterControlAltText="Filter GIS column" HeaderText="GIS" ReadOnly="True" UniqueName="GIS" Visible="False">
                </telerik:GridBoundColumn>

Open in new window






My Code behind

 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem dataItem = e.Item as GridDataItem;

             

                if (dataItem["GIS"].Text == "yes")
                {
                     (dataItem["AUTOIDCOLUMN"].Controls[0] as Image).ImageUrl = "/images/YES.png";

                }
                else
                {
                     (dataItem["AUTOIDCOLUMN"].Controls[0] as Image).ImageUrl = "/images/NO.png";
                }
            }
        }

Open in new window

0
Comment
Question by:Jason Jones
[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
4 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 38853923
if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem || e.Item.ItemType == GridItemType.SelectedItem)   
            {   
                GridDataItem item = (GridDataItem)e.Item;   
                Image img = (Image)dataItem["AUTOIDCOLUMN"].Controls[0];     
                if (item ["GIS"].Text == "yes")
                {     
                   img.ImageUrl =  "/images/YES.png";
                }
                else
                {
                   img.ImageUrl =  "/images/NO.png";
                }
}

Open in new window

0
 
LVL 1

Author Comment

by:Jason Jones
ID: 38854590
Thanks for the help and code.. A copy/paste of your code I am getting a syntax error on the "if" line that dataItem does not exist in the current context
0
 
LVL 1

Accepted Solution

by:
Jason Jones earned 0 total points
ID: 38856081
After all day I have figured out a working solution. there was more to the error, which by the way                 GridDataItem item = (GridDataItem)e.Item;  
need to be changed to                 GridDataItem dataitem = (GridDataItem)e.Item;   hence the first error.  The 2nd problem I found was the Column type. Since it is a Hyperlink type this changed the way the code had to be written too.  

Hope this helps anyone else



 if (e.Item is GridDataItem)
            {
                GridDataItem dataitem = (GridDataItem)e.Item;
                TableCell cell = (TableCell)dataitem["GIS"];
                
                HyperLink hyplnk = (HyperLink)dataitem["AUTOIDCOLUMN"].Controls[0];

              
                if (cell.Text == "yes")
                {
                    hyplnk.ImageUrl = "/images/Info-Small.png";
                }

                else
                {
                    hyplnk.ImageUrl = "/images/NO.png";
                   //  Or hide all together...    hyplnk.Visible = Convert.ToBoolean("False");
                }

            }

Open in new window

0
 
LVL 1

Author Closing Comment

by:Jason Jones
ID: 38872783
no one else helped so I had to figure the solution out myself
0

Featured Post

Independent Software Vendors: 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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

715 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