Solved

C# syntax to get value from Grid row

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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