Search code examples
devexpressaspxgridview

Binding GridViewComboBoxColumn to a datasource at Page_Load


I have a grid with a GridViewComboBoxColumn column. I have a datasource that is a list of objects. I need to assign the data in Page_Load. This does not work:

protected void Page_Load() {
    GridViewDataComboBoxColumn gridColumn = Grid.Columns["NumericData"]
        as GridViewDataComboBoxColumn;

    gridColumn.PropertiedropsComboBox.DataSource = DataSourceMadeUpOfAList;
    gridColumn.PropertiesComboBox.TextField = "SpelledOutNumbers";
    gridColumn.PropertiesComboBox.ValueField = "ActualNumbers";
...

I have looked here: Binding GridViewComboBoxColumn to a datasource

And that does work, but only in the edit form. I have my filter row enabled and those drop downs need to be populated as well. The aforementioned solution does not work with the filters (for obvious reasons). Any ideas are appreciated.

Thanks.


Solution

  • This code works fine here. Please make certain, that the type of the ValueField and the type of the column to which the ComboBoxColumn is bound(column.FieldName) is the same. Finally, here is my example:

        <dx:ASPxGridView ID="ASPxGridView1" Width="100%"  ClientInstanceName="grid"
        DataSourceID="SqlDataSource2" KeyFieldName="ProductID" AutoGenerateColumns="False" runat="server">
        <Columns>
            <dx:GridViewCommandColumn VisibleIndex="0">
                <ClearFilterButton Visible="True">
                </ClearFilterButton>
            </dx:GridViewCommandColumn>
            <dx:GridViewDataTextColumn FieldName="ProductID" ReadOnly="True" VisibleIndex="0">
                <EditFormSettings Visible="False" />
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="1">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn FieldName="SupplierID" VisibleIndex="2">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataComboBoxColumn FieldName="CategoryID" VisibleIndex="3">
                <PropertiesComboBox ValueType="System.String">
                </PropertiesComboBox>
            </dx:GridViewDataComboBoxColumn>
        </Columns>
    </dx:ASPxGridView>
    
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Products]">
            </asp:SqlDataSource>
    
    
    
        protected void Page_Load(object sender, EventArgs e) {
            GridViewDataComboBoxColumn col = ASPxGridView1.Columns["CategoryID"] as GridViewDataComboBoxColumn;
            col.PropertiesComboBox.DataSource = GetDataSource();
            col.PropertiesComboBox.ValueField = "Id";
            col.PropertiesComboBox.TextField = "Text";
        }
    
    ...
    
    
    public class Record {
        public Record(int id) {
            this.id = id;
            this.text = "Row " + id.ToString();
        }
    
        private int id;
        public int Id {
            get { return id; }
            set {
                id = value;
            }
        }
        private string text;
        public string Text {
            get { return text; }
        }