Solved

C# syntax to get value from Grid row

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

15 Experts available now in Live!

Get 1:1 Help Now