ItemDatabound RadGrid

31 posts, 0 answers
  1. Trong
    Trong avatar
    20 posts
    Member since:
    May 2008

    Posted 15 Jun 2008 Link to this post

    Hi All,
    I use RadGrid control,
    I want each row add attribute, I use ItemDatabound
    But I don't know how ItemDatabound work

    Please help me,
    Thanks!

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 15 Jun 2008 Link to this post

    Hi Trong,

    Set the EventHandler for the ItemDataBound event in the aspx and try to access the GridDataItem in the ItemDataBound event as shown below.

    ASPX:

      <telerik:radgrid id="RadGrid1" runat="server" OnItemDataBound="RadGrid1_ItemDataBound" > 
     

    CS:
      protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            if (e.Item is GridDataItem) 
            { 
                GridDataItem item = (GridDataItem)e.Item; 
                item.Attributes.Add("OnClick", "return show();"); 
            } 
       } 

    Thanks
    Shinu.

  3. DevCraft Release
  4. Trong
    Trong avatar
    20 posts
    Member since:
    May 2008

    Posted 16 Jun 2008 Link to this post

    Hi Shinu,

    Thanks for your answer.

    I've another problem.
    I want get data filed from Item


    if (e.Item is Telerik.Web.UI.GridDataItem)
            {
                Telerik.Web.UI.GridDataItem item = (Telerik.Web.UI.GridDataItem)e.Item;
                item.Attributes.Add("onmouseover", "ddrivetip('Data from filed',300)");
                item.Attributes.Add("onmouseout", "hideddrivetip()");                      
            }

    I want "
    Data from filed"  get dynamic from field in Item.
    I try to use:
    string str = item["MarketName"].ToString();

    But fail

    Please help,
    Thanks!

  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 16 Jun 2008 Link to this post

    Hi Trong,

    Try accessing the cell value in the ItemDataBound event as shown below.

    CS:

     
      protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)   
        {   
            if (e.Item is GridDataItem)   
            {   
                GridDataItem item = (GridDataItem)e.Item;   
                string strTxt=item["MarketName"].Text.ToString();   
            }   
       }  


    Thanks
    Princy

  6. Trong
    Trong avatar
    20 posts
    Member since:
    May 2008

    Posted 16 Jun 2008 Link to this post

    Hi Princy,
    I try this, But I've error like this "Cannot find a cell bound to column name 'MarketName'"

  7. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 16 Jun 2008 Link to this post

    Hi Trong,

    Can you check in your aspx whether you have set 'MarketName' as the UniqueName for the required column?

    ASPX:

    <telerik:GridBoundColumn UniqueName="MarketName" HeaderText="MarketName" DataField="MarketName" > 
           </telerik:GridBoundColumn> 


    Thanks
    Princy.

  8. Trong
    Trong avatar
    20 posts
    Member since:
    May 2008

    Posted 16 Jun 2008 Link to this post

    Hi Princy,

    Yes, I've add UniqueName = MarketName.

    But:
     string str = item["MarketName"].Text.ToString();

    str always  equal empty string.

    Thanks Princy!

  9. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 16 Jun 2008 Link to this post

    Hi Trong,

    Sending your aspx will be more good.

    Shinu.

  10. Trong
    Trong avatar
    20 posts
    Member since:
    May 2008

    Posted 16 Jun 2008 Link to this post

    Hi Shinu,
    My source below

    Source

    http://cid-5956e24e43a23017.skydrive.live.com/self.aspx/Public/Source.rar

    Please help me,
    Thanks!

  11. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 16 Jun 2008 Link to this post


    Sorry Trong, I am not able to open the source.rar. Can you just copy and paste the aspx code?

    Shinu.

  12. Trong
    Trong avatar
    20 posts
    Member since:
    May 2008

    Posted 16 Jun 2008 Link to this post

    HTML Code:
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="SimListByGroup.ascx.cs"
        Inherits="Controls_MD_Sim_SimListByGroup" %>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <telerik:RadGrid ID="grvSim" runat="server" AutoGenerateColumns="False" GridLines="None"
                AllowPaging="True" OnPageIndexChanged="grvSim_PageIndexChanged" Width="100%" PageSize="2" OnItemDataBound="grvSim_ItemDataBound">
                <MasterTableView>
                    <RowIndicatorColumn Visible="False">
                        <HeaderStyle Width="20px" />
                    </RowIndicatorColumn>
                    <ExpandCollapseColumn Resizable="False" Visible="False">
                        <HeaderStyle Width="20px" />
                    </ExpandCollapseColumn>
                    <Columns>
                        <telerik:GridBoundColumn DataField="SIMNoDisplay" HeaderText="Danh s&#225;ch Sim"
                            UniqueName="column">
                        </telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn HeaderText="Gi&#225; b&#225;n"
                            DataField="Price">
                            <ItemStyle HorizontalAlign="Center" Width="80px" />
                            <ItemTemplate>
                                <%# Eval("Price")%>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn HeaderText="T&#224;i khoản"
                            DataField="AccountValue">
                            <ItemStyle HorizontalAlign="Center" Width="80px" />
                            <ItemTemplate>
                                <%# Eval("AccountValue")%>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn HeaderText="Nơi b&#225;n"
                            DataField="MarketName" UniqueName="MarketName" >
                            <ItemStyle HorizontalAlign="Center" Width="80px" />
                            <ItemTemplate>
                                <%# Eval("MarketName")%>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                          <telerik:GridTemplateColumn HeaderText="Đặt mua"
                            DataField="AccountValue">
                            <ItemStyle HorizontalAlign="Center" Width="80px" />
                            <ItemTemplate>
                              <asp:HyperLink ID="HyperLink1" runat="server">Đặt mua</asp:HyperLink>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>                             
                    </Columns>
                    <EditFormSettings>
                        <PopUpSettings ScrollBars="None" />
                    </EditFormSettings>
                </MasterTableView>
                <PagerStyle Mode="NextPrevAndNumeric" NextPageText="Sau"
                    PagerTextFormat="<div style='float:left;'>Tổng <span style='color:red;'>{5}<span></div><div style='float:right'>Trang: {4}</div>"
                    PrevPageText="Truoc" Position="TopAndBottom" />
            </telerik:RadGrid>
            <asp:UpdateProgress ID="UpdateProgress1" runat="server">
                <ProgressTemplate>
                    <img alt="" src="<%=WebLibs.Globals.ApplicationVRoot() %>/Images/SiteImages/Loading.gif" />Đang
                    thực hiện ...
                </ProgressTemplate>
            </asp:UpdateProgress>
        </ContentTemplate>
    </asp:UpdatePanel>

    CS Code:
    protected void grvSim_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {

            if (e.Item is Telerik.Web.UI.GridDataItem)
            {
                Telerik.Web.UI.GridDataItem item = (Telerik.Web.UI.GridDataItem)e.Item;
                item.Attributes.Add("onmouseover", "ddrivetip('eee',300)");
                item.Attributes.Add("onmouseout", "hideddrivetip()");

                string str = item["MarketName"].Text.ToString();
            }
    }

    Thanks Shinu!


  13. Trong
    Trong avatar
    20 posts
    Member since:
    May 2008

    Posted 16 Jun 2008 Link to this post

     Hi  Shinu,
    I found this solution,
    But I think this very bad.

            if (e.Item is Telerik.Web.UI.GridDataItem)
            {
                Telerik.Web.UI.GridDataItem item = (Telerik.Web.UI.GridDataItem)e.Item;
                item.Attributes.Add("onmouseover", "ddrivetip('eee',300)");
                item.Attributes.Add("onmouseout", "hideddrivetip()");

                string str = item["MarketName"].Text.ToString();




                DataRowView v = (DataRowView)e.Item.DataItem;
                str = v.DataView[e.Item.ItemIndex]["MarketName"].ToString();
                
            }

    I can get value but spent much time. This machine have anather difference asp:Gridview control

    Thanks,

  14. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 16 Jun 2008 Link to this post

    Hi Trong,

    Can you try adding a Label control in the ItemTemplate of the GridTemplateColumn and try accessing the text of the label in the ItemdataBound event as shown below.

    ASPX:

     
     <telerik:GridTemplateColumn HeaderText="Nơi b&#225;n" 
                            DataField="MarketName" UniqueName="MarketName" > 
           <ItemStyle HorizontalAlign="Center" Width="80px" /> 
            <ItemTemplate> 
              <asp:Label ID="Label1" runat="server" Text='<%# Eval("OrderID")%>' ></asp:Label> 
                    
             </ItemTemplate> 
        </telerik:GridTemplateColumn> 
     

    CS:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            if (e.Item is GridDataItem) 
            { 
                GridDataItem item = (GridDataItem)e.Item; 
                Label lbl = (Label)item["MarketName"].FindControl("Label1"); 
                string strtxt = lbl.Text.ToString(); 
            } 
        } 
             
     


    Thanks
    Shinu.

  15. newbie
    newbie avatar
    355 posts
    Member since:
    Feb 2008

    Posted 18 Jun 2008 Link to this post

    I tried this approach and it works.

    However it shows incorrect totals when in edit mode.
    I want to either show the correct totals or just hide the totals when in edit/insert mode.(I am using in place editing)

    In edit mode it does not access to the labels declared in the ItemTemplate.

    any suggestions?

  16. Yavor
    Admin
    Yavor avatar
    11 posts
    Member since:
    Dec 2013

    Posted 19 Jun 2008 Link to this post

    Hi,

    With inplace editing the itemtemplate will not be visible, and hence the value will not be calculated properly. therefore, you may consider hiding the footer:

    .cs

     protected void RadGrid1_PreRender1(object sender, EventArgs e)  
        {  
            if (RadGrid1.EditIndexes.Count > 0)  
            {  
                GridFooterItem footer = (GridFooterItem)RadGrid1.MasterTableView.GetItems(GridItemType.EditItem);  
                footer["TotalColumn"].Visible = false;  
            }  
        } 

    I hope this helps.

    All the best,
    Yavor
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center

  17. Jeff
    Jeff avatar
    26 posts
    Member since:
    Nov 2008

    Posted 10 Jul 2009 Link to this post

    princy,

    do i need to add anything if i've converted the column to a item template, i've got it exactly like this and it's not seeing a value when i put a break there.

    thanks,

    Jeff

  18. JoAnna
    JoAnna avatar
    3 posts
    Member since:
    Dec 2008

    Posted 14 Oct 2010 Link to this post

    Hi,

    I am trying to do this also and am getting null exceptions in my code. I want to reference a Label control in my DetailTables. I tried your solution above, (for MasterTable only?) and it doesn't find it - get error. I then tried sticking the Label control in MasterTable's template column and it was able to find it, but it didn't/couldn't find it every time and then errored again.

    Could you please provide an example on how to access a control (Label) from DetailTables collection? (I need to set its Text property on ItemDataBound.)

    The reason I am doing this is to try to set 2 radio button column group names properly - only allow one button to be selected per column. I have two columns: Col1 radio button and Col2 radio button and currently can select them all despite having a GroupName set for each. An example I found to solve this used a GridView with a Label - you set a Label.Text property = <input type=radio...> and it apparently worked. 

    If there is an easier way to fix radio buttons - I'd MUCH prefer it! :)

    Thanks!
    JoAnna

  19. Vasil
    Admin
    Vasil avatar
    1420 posts
    Member since:
    Sep 2012

    Posted 19 Oct 2010 Link to this post

    Hi JoAnna,

    I think this code library will help you.

    All the best,
    Vasil
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

  20. Rachana
    Rachana avatar
    18 posts
    Member since:
    Jul 2011

    Posted 29 Mar 2012 Link to this post

    hi
       i am using radgrid,
       i want access DataKeyValue in Item data bound..
       how to acess DataKeyValue in Item data bound can u please tell me
      i am trying somthing like this but am not getting
     protected void gvSectionList_ItemDataBound(object sender, GridItemEventArgs e)
        {
          if (e.Item is GridDataItem)
            {
             e.item.GetDataKeyValue("Myvalue");
            }
        }

  21. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 29 Mar 2012 Link to this post

    Hello Rachana,

    Try the following code in ItemDataBound event to access datakeyvalue.
    C#:

    void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {

     if
    (e.Item is GridDataItem)
     {
     GridDataItem item = (GridDataItem)e.Item;
     string value = item.GetDataKeyValue("ID").ToString();
     }

    }

    Thanks,
    Princy.

  22. Rachana
    Rachana avatar
    18 posts
    Member since:
    Jul 2011

    Posted 11 Apr 2012 Link to this post

    how to display inside the radgrid one more radgrid with drag and drop option...that drag should be only particular grid..find the attachment file..i need to drag either terms or courses under the particular term or sections  under the particular course

  23. Vasil
    Admin
    Vasil avatar
    1420 posts
    Member since:
    Sep 2012

    Posted 16 Apr 2012 Link to this post

    Hi Rachana,

    See this online demo about dragging in RadGrid and examine it's code, especially the client side:
    http://demos.telerik.com/aspnet-ajax/grid/examples/programming/draganddrop/defaultcs.aspx
    It might help you in achieving the desired functionality.

    All the best,
    Vasil
    the Telerik team

    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.

  24. Moinul Haque
    Moinul Haque avatar
    2 posts
    Member since:
    Nov 2009

    Posted 08 Aug 2012 Link to this post

    DataRowView v = (DataRowView)e.Item.DataItem;
    string str = v.DataView[e.Item.ItemIndex]["uniqueKey"].ToString();

  25. Mark anthony
    Mark anthony avatar
    1 posts
    Member since:
    Aug 2012

    Posted 14 Aug 2012 Link to this post

    hi,
    I made a code project where in i used session in my itemdatabound method...
    but my session wont work at page load unless i refresh my page.

    please help. thanks

  26. Andrey
    Admin
    Andrey avatar
    836 posts
    Member since:
    Sep 2012

    Posted 20 Aug 2012 Link to this post

    Hello,

    You could check what is the value of the SessionStateMode attribute in the Web.Config file, it should be set to "InProc" in order for your code to work.

    Additionally, how do you set the value in the Session object and when? It is normal on initial load of the page the Session object to be empty if you are not filling it from unrelated to the page resource like database or local resource.

    Please share your full page source code along with the code-behind content.

    All the best,
    Andrey
    the Telerik team

    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.

  27. Dean
    Dean avatar
    10 posts
    Member since:
    Jun 2012

    Posted 24 Aug 2012 Link to this post

    hi telerik,

    is it possible to add id to <td> with expand/collapse button that s created by radgrid when there is a heirarchy table? i need to to this to meet the requirement of WCAG ID and Headers.

    Thanks,

    Dean

  28. Andrey
    Admin
    Andrey avatar
    836 posts
    Member since:
    Sep 2012

    Posted 27 Aug 2012 Link to this post

    Hi,

    I am not sure that I understand correctly what is your requirement.

    If you want to add a button that will expand/collapse the hierarchy levels of RadGrid you could either use the built-in GridExpandCollapse column with the default image or you could change the image with a custom text that you want to put.

    In order to substitute the image with custom text, you could use the approach shown in this help topic.

    Greetings,
    Andrey
    the Telerik team

    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.

  29. Dean
    Dean avatar
    10 posts
    Member since:
    Jun 2012

    Posted 27 Aug 2012 Link to this post

    hi andrey,

    i just need to add an unique identifier to the <td> that contains the expand/collapse button to be able to meet the WCAG compliance error "table with multiple headers should contains ID and Header attributes." i can attach the exact code generated by radgrid and the error message from AChecker.com. Thank you.

    Regards,

    Dean

  30. Andrey
    Admin
    Andrey avatar
    836 posts
    Member since:
    Sep 2012

    Posted 28 Aug 2012 Link to this post

    Hi,

    We are using this online tool to verify accessibility of our products -- Wave. I tested RadGrid with hierarchy structure and the tool has returned that there is no accessibility errors.

    Could you try this tool with your project and let me know what is the result?

    Regards,
    Andrey
    the Telerik team

    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.

  31. Dean
    Dean avatar
    10 posts
    Member since:
    Jun 2012

    Posted 28 Aug 2012 Link to this post

    Hi Andrey,

    My team's requirement is to test our page's WCAG compliance using Wave and another online tool AChecker.com (http://achecker.ca/checker/index.php) Wave does not detect any incompliance but Achecker have a different result and it detects the error of the radgrid with heirarchy.

    Please try checking Radgrid Heirarchy using Achecker:
    -Go to the site.
    -Select "Paste HTML Markup"
    -Click options
    -Select WCAG 2.0 (Level A) under the "Guidelines to check against" section.


    Regards,

    Dean

Back to Top
DevCraft Release