Solved

C# syntax to get value from Grid row

Posted on 2013-02-04
4
439 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

763 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