This is a migrated thread and some comments may be shown as answers.

ItemDatabound RadGrid

31 Answers 5929 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Trong
Top achievements
Rank 1
Trong asked on 16 Jun 2008, 03:59 AM
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!

31 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 16 Jun 2008, 04:46 AM
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.
0
Trong
Top achievements
Rank 1
answered on 16 Jun 2008, 05:45 AM
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!
0
Princy
Top achievements
Rank 2
answered on 16 Jun 2008, 05:50 AM
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
0
Trong
Top achievements
Rank 1
answered on 16 Jun 2008, 06:07 AM
Hi Princy,
I try this, But I've error like this "Cannot find a cell bound to column name 'MarketName'"

0
Princy
Top achievements
Rank 2
answered on 16 Jun 2008, 06:13 AM

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.
0
Trong
Top achievements
Rank 1
answered on 16 Jun 2008, 06:25 AM
Hi Princy,

Yes, I've add UniqueName = MarketName.

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

str always  equal empty string.

Thanks Princy!
0
Shinu
Top achievements
Rank 2
answered on 16 Jun 2008, 06:39 AM
Hi Trong,

Sending your aspx will be more good.

Shinu.
0
Trong
Top achievements
Rank 1
answered on 16 Jun 2008, 06:48 AM
Hi Shinu,
My source below

Source

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

Please help me,
Thanks!
0
Shinu
Top achievements
Rank 2
answered on 16 Jun 2008, 07:01 AM

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

Shinu.
0
Trong
Top achievements
Rank 1
answered on 16 Jun 2008, 07:06 AM
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!


0
Trong
Top achievements
Rank 1
answered on 16 Jun 2008, 07:27 AM
 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,
0
Shinu
Top achievements
Rank 2
answered on 16 Jun 2008, 07:45 AM
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.
0
newbie
Top achievements
Rank 1
answered on 18 Jun 2008, 08:42 PM
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?
0
Yavor
Telerik team
answered on 19 Jun 2008, 05:45 AM
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
0
Jeff
Top achievements
Rank 1
answered on 10 Jul 2009, 09:01 PM
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
0
JoAnna
Top achievements
Rank 1
answered on 15 Oct 2010, 12:50 AM
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
0
Vasil
Telerik team
answered on 19 Oct 2010, 12:55 PM
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
0
Rachana
Top achievements
Rank 1
answered on 29 Mar 2012, 10:48 AM
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");
        }
    }
0
Princy
Top achievements
Rank 2
answered on 29 Mar 2012, 10:55 AM
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.
0
Rachana
Top achievements
Rank 1
answered on 11 Apr 2012, 02:12 PM
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
0
Vasil
Telerik team
answered on 16 Apr 2012, 08:35 AM
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.
0
Moinul Haque
Top achievements
Rank 1
answered on 08 Aug 2012, 12:59 PM
DataRowView v = (DataRowView)e.Item.DataItem;
string str = v.DataView[e.Item.ItemIndex]["uniqueKey"].ToString();
0
Mark anthony
Top achievements
Rank 1
answered on 15 Aug 2012, 03:09 AM
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
0
Andrey
Telerik team
answered on 20 Aug 2012, 06:05 AM
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.
0
Dean
Top achievements
Rank 1
answered on 24 Aug 2012, 07:52 AM
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
0
Andrey
Telerik team
answered on 27 Aug 2012, 03:36 PM
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.
0
Dean
Top achievements
Rank 1
answered on 28 Aug 2012, 12:38 AM
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
0
Andrey
Telerik team
answered on 28 Aug 2012, 02:37 PM
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.
0
Dean
Top achievements
Rank 1
answered on 29 Aug 2012, 12:39 AM
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
0
Andrey
Telerik team
answered on 29 Aug 2012, 02:51 PM
Hi,

We noticed the accessibility errors that you are talking about and we will continue working on improving our accessibility compliance.

For the moment we are only compliant with the Wave tool. In future we will strive to expand our compliance to cover more tools.

Thank you for your commitment.

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.
0
Sudeep
Top achievements
Rank 1
answered on 14 Oct 2015, 04:15 PM
what if I want to select a singl radio button from the radio button list and basing on its value I have to show or hide a particular drop down list?
Tags
Grid
Asked by
Trong
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Trong
Top achievements
Rank 1
Princy
Top achievements
Rank 2
newbie
Top achievements
Rank 1
Yavor
Telerik team
Jeff
Top achievements
Rank 1
JoAnna
Top achievements
Rank 1
Vasil
Telerik team
Rachana
Top achievements
Rank 1
Moinul Haque
Top achievements
Rank 1
Mark anthony
Top achievements
Rank 1
Andrey
Telerik team
Dean
Top achievements
Rank 1
Sudeep
Top achievements
Rank 1
Share this question
or