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

[Solved] Adding items to dropdown in grid

4 Answers 207 Views
Grid
This is a migrated thread and some comments may be shown as answers.
PM
Top achievements
Rank 1
PM asked on 26 Nov 2009, 09:04 AM

Hi All,

                        

I have a dropdown column called "Year" in grid.


in edit mode i am setting readonly property to true for this column,but in insert mode i want to add the years to dropdown from databasetable, how can i do this in RadGrid1_ItemCommand event.

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 26 Nov 2009, 09:28 AM
Hello Praveen,

Try the following code snippet.

ASPX:
 
<Columns> 
            <telerik:GridDropDownColumn UniqueName="Dropdown" DataSourceID="SqlDataSource1" DataField="CustomerID" ListTextField="CustomerID" .. > 
           </telerik:GridDropDownColumn> 

CS:
 
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        foreach (GridColumn col in RadGrid1.Columns) 
        { 
            if (col.ColumnType == "GridDropDownColumn" && col.UniqueName == "Dropdown"
            { 
                if (e.CommandName == RadGrid.EditCommandName) 
                { 
                    (col as GridDropDownColumn).ReadOnly = true
                    col.Visible = false
                } 
                else 
                { 
                    (col as GridDropDownColumn).ReadOnly = false
                    col.Visible = true
                } 
            } 
        }         
    } 

-Shinu.
0
PM
Top achievements
Rank 1
answered on 26 Nov 2009, 09:35 AM
Shinu,

My issue is to fill the dropdown with the values taking from database table when i click on add new record.
0
Princy
Top achievements
Rank 2
answered on 26 Nov 2009, 11:55 AM
Hello Praveen,

To populate the combobox when the DropDownColumn is in insert mode, you can try out the following code:
c#:
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted) 
        { 
            // Replace GridEditFormInsertItem with GridDataInsertItem if EditMode="InPlace" 
            GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;  
            RadComboBox combo = (RadComboBox)insertItem["GridDropDownColumnUniqueName"].Controls[0]; 
            combo.DataSource = dt; 
            combo.DataTextField = "Year"
            combo.DataValueField = "Year"
            combo.DataBind(); 
        } 
    } 

Thanks
Princy.
0
PM
Top achievements
Rank 1
answered on 27 Nov 2009, 05:51 AM
thank you.
Tags
Grid
Asked by
PM
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
PM
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or