Change Value on ItemDataBound RadGrid

4 posts, 1 answers
  1. Anil Kumar
    Anil Kumar avatar
    13 posts
    Member since:
    Mar 2010

    Posted 09 Dec 2010 Link to this post

    I want the code where i can bound the data code behind and access and change all the bind controls on the ItemDataBound Event
    see the code.
    <telerik:RadGrid ID="RadGrid1" runat="server"
            onitemcommand="RadGrid1_ItemCommand" GridLines="None"
            onitemdatabound="RadGrid1_ItemDataBound">
            <MasterTableView>
                <Columns>
                    <telerik:GridTemplateColumn HeaderText="CompanyName" UniqueName="CompanyName">
                        <EditItemTemplate>
                            <asp:TextBox runat="server"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label runat="server"><%# Eval("CompanyName")%></asp:Label>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
            <ClientSettings>
                <Selecting AllowRowSelect="True" />
            </ClientSettings>
        </telerik:RadGrid>
    This is my c# code
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    LoadData();
                }
            }
     
            private void LoadData()
            {
    // done some binding code
                RadGrid1.DataSource = gd.GetDataTable("SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, PostalCode FROM Customers");
            }
            protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
            {
                 // what i have to do for the stuff for example
            }
     
            protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
            {
                if (e.Item.ItemType == GridItemType.AlternatingItem || e.Item.ItemType == GridItemType.Item)
                {
                    GridItem item = default(GridItem);
                    // get the label value on databound and change it.
     
                }
            }
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 10 Dec 2010 Link to this post

    Hello Anil,

    If you wants to access the Label in ItemTemplate from code behind, yo need to set ID for that control and and also set the Text property to get the Label text.
    ASPX:
    <ItemTemplate>
      <asp:Label ID="Label1" runat="server" Text='<%# Eval("CompanyName")%>'></asp:Label>
    </ItemTemplate>

    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
           if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                Label lbl = (Label)item.FindControl("Label1");//accessing Label
                string lbltext = lbl.Text;
            }
        }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Pradeep
    Pradeep avatar
    4 posts
    Member since:
    Jul 2012

    Posted 10 Oct 2013 Link to this post

    I will tell my requirement clearly..!!

    Radcomboxbox1 is in the form ie Page..!!(Outside the grid)

    Radcombobox2 is inside Radgrid

    On Selection of  Radcomboxbox1 i want to populate Radcombobox2 it's basically selected index change.

    Pls help me with the code.
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 10 Oct 2013 Link to this post

    Hi Pradeep,

    Please try the following code snippet which populates RadComboBox in the view mode.if this doesn't help,please elaborate your scenario.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" OnItemCreated="RadGrid1_ItemCreated">
        <MasterTableView>
           <Columns>      
                <telerik:GridTemplateColumn HeaderText="Country">
                    <ItemTemplate>
                        <telerik:RadComboBox ID="RadComboBox1" runat="server">
                        </telerik:RadComboBox>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    <telerik:RadComboBox ID="RadComboBox2" runat="server" DataSourceID="SqlDataSource2"
    DataTextField="ID" DataValueField="ID">
    </telerik:RadComboBox>

    C#:
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
       {
           RadComboBox2.AutoPostBack = true;
           RadComboBox2.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(RadComboBox2_SelectedIndexChanged);
       }
     
    void RadComboBox2_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
       {      
         if( your condition)
        {
           foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
           {          
               RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox1");
               combo.DataSourceID = "SqlDataSource1";
               combo.DataTextField = "Country";
               combo.DataValueField = "Country";
               combo.DataBind();             
           }
         }
       }

    Thanks,
    Princy
Back to Top