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

telerik:GridDropDownColumn is not displaying the value in View Mode

12 Answers 622 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Nirmala
Top achievements
Rank 1
Nirmala asked on 26 Apr 2012, 10:06 PM
Hi ,

 I have a telerikgrid inside that i have a
  <telerik:GridDropDownColumn  DropDownControlType="RadComboBox" HeaderText="Expiration Units" DataField="qual_req_units"
                                SortExpression="qual_req_units" />

OnItemDataBound and when it is in the edit mode i bind the combobox with the values.

OnItemCreated i tried to display the value from that is stored in the db. 
 GridDataItem item = (GridDataItem)e.Item;
 DataRowView drv = (DataRowView)e.Item.DataItem;
 item["qual_req_units"].Text = drv.Row["qual_req_units"].ToString();

Still it doesn't show any value in the view mode. In edit mode it shows the radcombo box's correctly. But even when select and update it doesn't show any value.

Please let me know what i need to do to display the value in the view mode.


Thanks,
Nirmala Kalakonda

12 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 27 Apr 2012, 11:28 AM
Hi Nirmala,

Please check the following code snippet I tried to show the updated value in view mode.

ASPX:
<telerik:GridDropDownColumn UniqueName="DropDownColumn"  DropDownControlType="RadComboBox" HeaderText="Expiration Units" />

C#:
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    GridEditableItem editItem = (GridEditableItem)e.Item;
    string city = (editItem["DropDownColumn"].Controls[0] as RadComboBox).Text;
    string updateQuery = "Update Orders set ShipCity='"+city+"' where OrderID="10356";
    conn.Open();
    SqlCommand.CommandText = updateQuery;
    SqlCommand.Connection = conn;
    SqlCommand.ExecuteNonQuery();
    conn.Close();
}
protected void rdgdr1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        DataRowView row = (DataRowView)e.Item.DataItem;
        item["DropDownColumn"].Text = row["ShipCity"].ToString();
    }
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem item = (GridEditableItem)e.Item;
        RadComboBox com = (RadComboBox)item["DropDownColumn"].Controls[0];
        com.DataSourceID = "SqlDataSource1";
        com.DataTextField = "ShipCity";
        com.DataValueField = "ShipCity";
        com.DataBind();
    }
}

Thanks,
Princy.
0
Nirmala
Top achievements
Rank 1
answered on 27 Apr 2012, 02:29 PM
Thanks Pricy. It worked like a charm :)
0
Nirmala
Top achievements
Rank 1
answered on 30 Apr 2012, 07:57 PM
Hi Pricy,

  Now i have a different issue. Whenever the grid id doing a postback the values of dropdown are again not displaying because it doesn't go to the itemdatabound and the View state is lost. 

The postback that i refering is the grid selectedindexchanged. On this event also, i still want display the dropdown values.

Thanks,
Nirmala 
0
Accepted
Richard
Top achievements
Rank 1
answered on 02 May 2012, 04:58 PM
Nirmala:

You can take a look at the Grid/Accessing Cells and Rows online demo. It uses the GridDropDownColumn for the OrderID column.

It provides the suggested method for populating the RadComboBox, as follows:

protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        //the dropdown list will be the first control in the Controls collection of the corresponding cell
        RadComboBox list = (e.Item as GridEditableItem)["ddlOrderID"].Controls[0] as RadComboBox;
 
        //attach SelectedIndexChanged event for the combobox control
        list.AutoPostBack = true;
        list.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(list_SelectedIndexChanged);
    }
}
 
void list_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    //first reference the edited grid item through the NamingContainer attribute
    GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
 
    //the dropdown list will be the first control in the Controls collection of the corresponding cell
    //for custom edit forms (WebUserControl/FormTemplate) you can find the column editor with the FindControl(controlId) method
    RadComboBox ddList = editedItem["ddlQuantity"].Controls[0] as RadComboBox;
 
    // change the data source for ContactTitle with custom code here
    DataTable table = GetRelatedRecords("SELECT OrderID, Quantity FROM [Order Details] WHERE OrderID = " + (editedItem["ddlOrderID"].Controls[0] as RadComboBox).SelectedValue);
    ddList.ClearSelection();
    ddList.DataSource = table;
    ddList.DataBind();
 
    RadGrid1.Controls.Add(new LiteralControl("<b>The available options for 'Quantites in stock' has been changed</b>"));
}

I hope this helps!
0
Manju
Top achievements
Rank 1
answered on 06 Feb 2013, 02:41 PM
Please let me know how to trigger the combo box changed event in vb.net
0
Hristo Valyavicharski
Telerik team
answered on 07 Feb 2013, 12:06 PM
Hi Manju,

TextChanged event occurs when the text in the input area of RadComboBox changes. This means that AllowCustomText must be set to true in order to be able to change the text in the combo input. Finally set AutoPostBack to true and subscribe for this event in the code behind:

<telerik:RadComboBox ID="RadComboBox1" runat="server" AllowCustomText="True" AutoPostBack="true">
</telerik:RadComboBox>
Protected Sub RadComboBox1_TextChanged(sender As Object, e As EventArgs) Handles RadComboBox1.TextChanged
End Sub

I hope this helps.
Regards,
Hristo Valyavicharski
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
Manju
Top achievements
Rank 1
answered on 07 Feb 2013, 02:34 PM
I have a radgrid having GridDropdown Column , which selected index changed event needs to be triggered. I have been adding the values to dropdown dynamically in ItemDataBound event of RadGrid. If I change the dropdown list selection and click the Add button, insert command event gets the default value, instead of changed value.
0
Princy
Top achievements
Rank 2
answered on 08 Feb 2013, 06:55 AM
Hi Manju,

You can attach the RadComboBox SelectedIndexChanged event in the RadGrid_ItemCreated event as follows.
C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem Item = (GridEditableItem)e.Item;
            RadComboBox cmb = (RadComboBox)Item["DropDownListColumn"].Controls[0];
            cmb.AutoPostBack = true;
            cmb.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(cmb_SelectedIndexChanged);
        }
    }
void cmb_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        
    }
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
        GridEditFormInsertItem item = (GridEditFormInsertItem)e.Item;
        RadComboBox combo = (RadComboBox)item["DropDownListColumn"].Controls[0];
        string value = combo.SelectedItem.Text;//accessing new values in InsertCommand
}

Thanks,
Princy.
0
Hristo Valyavicharski
Telerik team
answered on 12 Feb 2013, 11:26 AM
Hi Manju,

Princy provided you with the correct approach. Here is the code converted into VB.NET:
Protected Sub RadGrid1_ItemCreated(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemCreated
    If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
        Dim Item As GridEditableItem = DirectCast(e.Item, GridEditableItem)
        Dim cmb As RadComboBox = DirectCast(Item("DropDownListColumn").Controls(1), RadComboBox)
        cmb.AutoPostBack = True
 
        AddHandler cmb.SelectedIndexChanged, AddressOf cmb_SelectedIndexChanged
    End If
End Sub
 
Private Sub cmb_SelectedIndexChanged(sender As Object, e As RadComboBoxSelectedIndexChangedEventArgs)
End Sub
 
 
Protected Sub RadGrid1_InsertCommand(sender As Object, e As GridCommandEventArgs)
    Dim item As GridEditFormInsertItem = DirectCast(e.Item, GridEditFormInsertItem)
    Dim combo As RadComboBox = DirectCast(item("DropDownListColumn").Controls(1), RadComboBox)
    Dim value As String = combo.SelectedItem.Text
    'accessing new values in InsertCommand
End Sub
To convert C# code to VB.NET code you can use Telerik's Code Converter.
 
Kind regards,
Hristo Valyavicharski
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
Kenneth
Top achievements
Rank 1
answered on 26 Mar 2013, 06:27 PM
I have a similar problem.  I create the listitems for my dropdown in the gridDevView_ItemDataBound method and set the Text displayed in the column.  And that works great. 

protected void gridDevView_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridDataItem && !e.Item.IsInEditMode)
    {
        GridDataItem item = (GridDataItem)e.Item;
        DataRowView row = (DataRowView)e.Item.DataItem;
        item["RevenueGenerationDropDownColumn"].Text = row["REVENUE_GENERATING"].ToString();
        item["ClientApprovedDropDownColumn"].Text = row["CLIENT_APPROVED"].ToString();
        item["StatusDropDownColumn"].Text = row["STATUS_TEXT"].ToString();
    }
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        GridEditManager editManagaer = editedItem.EditManager;

        GridDataItem item = (GridDataItem)e.Item;
        DataRowView row = (DataRowView)e.Item.DataItem;

        GridDropDownListColumnEditor revenue_editor = (GridDropDownListColumnEditor)(editManagaer.GetColumnEditor("RevenueGenerationDropDownColumn"));
        RadComboBox ddRevenueList = revenue_editor.ComboBoxControl;
        ddRevenueList.Items.Clear();

        ddRevenueList.Items.Insert(0, new RadComboBoxItem("", ""));
        ddRevenueList.Items.Insert(1, new RadComboBoxItem("Yes", "Y"));
        ddRevenueList.Items.Insert(2, new RadComboBoxItem("No", "N"));
        ddRevenueList.SelectedValue = row["REVENUE_GENERATING"].ToString();

        GridDropDownListColumnEditor client_approved_editor = (GridDropDownListColumnEditor)(editManagaer.GetColumnEditor("ClientApprovedDropDownColumn"));
        RadComboBox ddClientApprovedList = client_approved_editor.ComboBoxControl;
        ddClientApprovedList.Items.Clear();

        ddClientApprovedList.Items.Insert(0, new RadComboBoxItem("", ""));
        ddClientApprovedList.Items.Insert(1, new RadComboBoxItem("Yes", "Y"));
        ddClientApprovedList.Items.Insert(2, new RadComboBoxItem("No", "N"));
        ddClientApprovedList.SelectedValue = row["CLIENT_APPROVED"].ToString();

        GridDropDownListColumnEditor status_editor = (GridDropDownListColumnEditor)(editManagaer.GetColumnEditor("StatusDropDownColumn"));
        RadComboBox ddStatusList = status_editor.ComboBoxControl;
        ddStatusList.Items.Clear();

        ddStatusList.Items.Insert(0, new RadComboBoxItem("", ""));
        ddStatusList.Items.Insert(1, new RadComboBoxItem("In Test", "0"));
        ddStatusList.Items.Insert(2, new RadComboBoxItem("In Development", "2"));
        ddStatusList.Items.Insert(3, new RadComboBoxItem("Not Scheduled", "3"));
        ddStatusList.Items.Insert(4, new RadComboBoxItem("Complete", "4"));
        ddStatusList.Items.Insert(5, new RadComboBoxItem("Pending Development", "5"));
        ddStatusList.Items.Insert(6, new RadComboBoxItem("Pending Spec", "6"));
        ddStatusList.Items.Insert(7, new RadComboBoxItem("Rejected", "7"));
        ddStatusList.Items.Insert(8, new RadComboBoxItem("Deferred", "8"));
        ddStatusList.SelectedValue = row["STATUS"].ToString();

    }
}

However, when the "ExpandCollapse" Item Command is fired the values displayed for my GridDropDownColumn set are empty.  

How do I keep my displayed values?



0
Princy
Top achievements
Rank 2
answered on 27 Mar 2013, 09:04 AM
Hi,

One option is to try setting the GridDropDownColumn's DataField as DataKeyNames  and access the DataKeyValue in the PreRender event of the RadGrid and set the text as follows.

ASPX:
<telerik:RadGrid ID="RadGrid2" . . .
   <MasterTableView  Name="Master" DataKeyNames="REVENUE_GENERATING">
   . . .

C#:
protected void RadGrid2_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
          GridEditableItem editedItem = e.Item as GridEditableItem;
          GridEditManager editManagaer = editedItem.EditManager;
          GridDropDownListColumnEditor revenue_editor = (GridDropDownListColumnEditor)(editManagaer.GetColumnEditor("RevenueGenerationDropDownColumn"));
          RadComboBox ddRevenueList = revenue_editor.ComboBoxControl;
          ddRevenueList.Items.Clear();
          ddRevenueList.Items.Insert(0, new RadComboBoxItem("", ""));
          ddRevenueList.Items.Insert(1, new RadComboBoxItem("Yes", "Y"));
          ddRevenueList.Items.Insert(2, new RadComboBoxItem("No", "N"));
          ddRevenueList.SelectedValue = (string)DataBinder.Eval(e.Item.DataItem, "REVENUE_GENERATING").ToString();
    }
}
 
protected void RadGrid2_PreRender(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid2.MasterTableView.Items)
    {
            string datakey = item.GetDataKeyValue("REVENUE_GENERATING").ToString();
            item["RevenueGenerationDropDownColumn"].Text = datakey;
    }
}

Thanks,
Princy.
0
Kenneth
Top achievements
Rank 1
answered on 15 Apr 2013, 02:42 PM
Thanks!!!
Tags
ComboBox
Asked by
Nirmala
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Nirmala
Top achievements
Rank 1
Richard
Top achievements
Rank 1
Manju
Top achievements
Rank 1
Hristo Valyavicharski
Telerik team
Kenneth
Top achievements
Rank 1
Share this question
or