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

GridDropDown column in edit mode

1 Answer 13 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alyssa
Top achievements
Rank 1
Alyssa asked on 06 Nov 2013, 01:02 PM
hi
how to bind a GridDropDown column in edit mode using a value from datatable.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 06 Nov 2013, 01:09 PM
Hi Alyssa,

Please try the following code snippet.

ASPX:
<telerik:GridDropDownColumn HeaderText="ShipCity" UniqueName="ShipCity" DataField="ShipCity"
    ListTextField="ShipCity" ListValueField="ShipCity" DataSourceID="SqlDataSource1">
</telerik:GridDropDownColumn>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    //Populate in EditMode
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem edititem = (GridEditableItem)e.Item;
        GridEditManager editMan = edititem.EditManager;
        GridDropDownListColumnEditor editor = (GridDropDownListColumnEditor)(editMan.GetColumnEditor("ShipCity"));
        RadComboBox rComboBox = editor.ComboBoxControl;
        string selectedVal = DataBinder.Eval(edititem.DataItem, "ShipCity").ToString();
        DataTable table = GetRelatedRecords("SELECT ShipCity FROM [Orders]");
        editor.DataSource = table;
        rComboBox.DataSource = table;
        rComboBox.DataTextField = "ShipCity";
        rComboBox.DataValueField = "ShipCity";
        rComboBox.DataBind();
        rComboBox.SelectedValue = selectedVal;
    }
}
 
public static DataTable GetRelatedRecords(string query)
{
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(query, conn);
    DataTable myDataTable = new DataTable();
    conn.Open();
    try
    {
        adapter.Fill(myDataTable);
    }
    finally
    {
        conn.Close();
    }
    return myDataTable;
}

Hope this helps.Let me know if any concern.
Thanks,
Princy
Tags
Grid
Asked by
Alyssa
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or