Solved

C# syntax to get value from Grid row

Posted on 2013-02-04
4
425 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:upgraders
  • 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:upgraders
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:
upgraders 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:upgraders
ID: 38872783
no one else helped so I had to figure the solution out myself
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Memory Usage 2 49
Class object 2 26
Data is not showing from images 15 36
Web Form VB.Net  import CSV 4 17
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
A short film showing how OnPage and Connectwise integration works.

930 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

13 Experts available now in Live!

Get 1:1 Help Now