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

Change Value on ItemDataBound RadGrid

3 Answers 806 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anil Kumar
Top achievements
Rank 2
Anil Kumar asked on 09 Dec 2010, 05:35 PM
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.
 
            }
        }

3 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 1
answered on 10 Dec 2010, 07:08 AM
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.
0
Pradeep
Top achievements
Rank 1
answered on 10 Oct 2013, 06:01 AM
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.
0
Princy
Top achievements
Rank 1
answered on 10 Oct 2013, 08:02 AM
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
Tags
Grid
Asked by
Anil Kumar
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 1
Pradeep
Top achievements
Rank 1
Share this question
or