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

Grid Drop Down List Is Empty on Edit

4 Answers 47 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steve Todd
Top achievements
Rank 1
Steve Todd asked on 13 Aug 2009, 02:44 PM
I'm trying to use the Automatic Update/Insert/Delete to save a lot of time, however I'm stuck with a grid that will not populate the DropDownList. When I go into edit mode, the dropdown displays a blank list but the ddList is populated in the c# code. What am I doing wrong?

<MasterTableView EditMode="PopUp" CommandItemDisplay="Top" DataKeyNames="Make,Model">  
                <Columns> 
                    <telerik:GridDropDownColumn ColumnEditorID="Make"   
                                                DataField="Make" 
                                                DataType="System.String"    
                                                Display="true"    
                                                DropDownControlType="RadComboBox"   
                                                HeaderText="Make" 
                                                ReadOnly="false"   
                                                SortExpression="Make"   
                                                UniqueName="Make">  
                    </telerik:GridDropDownColumn> 
                </Columns> 
                <EditFormSettings CaptionFormatString="Edit " 
                    PopUpSettings-Modal="true" /> 
            </MasterTableView> 



This is the code behind on the ItemDataBound

        if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))  
        {  
            GridEditableItem eeditedItem = e.Item as GridEditableItem;  
            GridEditManager editMan = editedItem.EditManager;  
            GridDropDownListColumnEditor editor = editMan.GetColumnEditor("Make") as GridDropDownListColumnEditor;                  
 
            DataTable data = new DataTable();  
            SqlConnection conn = null;  
 
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Demo Database NAV (6-0)ConnectionString"].ConnectionString);  
            conn.Open();  
 
            SqlCommand cmd = new SqlCommand("Makes", conn);  
            cmd.CommandType = CommandType.StoredProcedure;  
            cmd.Parameters.Add(new SqlParameter("@strCriteriaMake", Convert.ToString(editor.SelectedValue).ToUpper()));  
 
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))  
            {  
                da.Fill(data);  
            }  
 
            DropDownList ddList = editor.DropDownListControl;  
            for (int i = 0; i < data.Rows.Count; i++)  
            {  
                ddList.Items.Add(data.Rows[i]["Make"].ToString());  
            }  
              
            ddList.DataBind();  
        } 

Regards

Steve

4 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 1
answered on 14 Aug 2009, 07:32 AM
Hello Steve,

You just have to modify your code a bit inorder to have the RadComboBox control populated in edit mode:
c#:
 if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))   
        {   
            GridEditableItem eeditedItem = e.Item as GridEditableItem;   
            GridEditManager editMan = editedItem.EditManager;   
            GridDropDownListColumnEditor editor = editMan.GetColumnEditor("Make"as GridDropDownListColumnEditor;                   
  
            DataTable data = new DataTable();   
            SqlConnection conn = null;   
  
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Demo Database NAV (6-0)ConnectionString"].ConnectionString);   
            conn.Open();   
  
            SqlCommand cmd = new SqlCommand("Makes", conn);   
            cmd.CommandType = CommandType.StoredProcedure;   
            cmd.Parameters.Add(new SqlParameter("@strCriteriaMake", Convert.ToString(editor.SelectedValue).ToUpper()));   
  
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))   
            {   
                da.Fill(data);   
            }   
  
            RadComboBox ddList = editor.ComboBoxControl; // Modify this line in your code
                     // since you have set the DropDownControlType to RadComboBox  
 
            for (int i = 0; i < data.Rows.Count; i++)   
            {   
                ddList.Items.Add(data.Rows[i]["Make"].ToString());   
            }   
               
            ddList.DataBind();   
        }  

Thanks
Princy.
0
Steve Todd
Top achievements
Rank 1
answered on 14 Aug 2009, 07:49 AM
Excellent thansks you very much Princy.

I had to modify the code slightly but it pointed me in the right direction

RadComboBox ddList = editor.ComboBoxControl;
for (int i = 0; i < data.Rows.Count; i++)
{
RadComboBoxItem lstItem = new RadComboBoxItem();
lstItem.Text = data.Rows[i]["Make"].ToString(); 
lstItem.Value = data.Rows[i]["Make"].ToString();
ddList.Items.Add(lstItem);
}

0
Steve Todd
Top achievements
Rank 1
answered on 14 Aug 2009, 09:14 AM
Is there a way this Combo can be editable i.e. type a value into the box when inserting a new record or is it pureply a lookup box?
0
Princy
Top achievements
Rank 1
answered on 14 Aug 2009, 11:44 AM
Hello Steve,

To type a value into the RadComboBox youcan set its AllowCustomText property to true as shown below:
c#:
ddList.AllowCustomText = true;

Thanks
Princy.
Tags
Grid
Asked by
Steve Todd
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Steve Todd
Top achievements
Rank 1
Share this question
or