davidegironi / advanceddatagridview

A .NET WinForms DataGridView with advanced capabilities
392 stars 123 forks source link

Filter/Sort doesn't work on DataGridViewComboBoxColumn #122

Closed cadmonkey123 closed 11 months ago

cadmonkey123 commented 11 months ago

hi, I initially had alot of issues with getting the filter/sort to work until I saw the comment about adding the data to a data table which I did and it does filter/sort however I also need to filter/sort on ComboBox Columns and I don't know how to add the comboxbox data to the data table could someone point me in the right direction. Thanks (I am using vb.net)

davidegironi commented 11 months ago

Hello,

A ComboBox is not an elementary type, so it's not handled by AdvancedDataGridView. Anyway there are a couple of trick you could use:

1) build a combobox and it's datatable source, with the ValueMember of type int. Then add an int value to the datagridview datatable of value int with the same name.

Taking the sample project as reference, something like below should work: SetTestData()

        private void SetTestData()
        {
            _dataTable = _dataSet.Tables.Add("TableTest");
            _dataTable.Columns.Add("int", typeof(int));
            _dataTable.Columns.Add("decimal", typeof(decimal));
            _dataTable.Columns.Add("double", typeof(double));
            _dataTable.Columns.Add("date", typeof(DateTime));
            _dataTable.Columns.Add("datetime", typeof(DateTime));
            _dataTable.Columns.Add("string", typeof(string));
            _dataTable.Columns.Add("boolean", typeof(bool));
            _dataTable.Columns.Add("guid", typeof(Guid));
            _dataTable.Columns.Add("image", typeof(Bitmap));
            _dataTable.Columns.Add("timespan", typeof(TimeSpan));
            // add the int reference to combobox
            _dataTable.Columns.Add("cmdview", typeof(int));

            bindingSource_main.DataMember = _dataTable.TableName;

            advancedDataGridViewSearchToolBar_main.SetColumns(advancedDataGridView_main.Columns);

            // add the combobox
            DataTable cmdDataSource = new DataTable();
            cmdDataSource.Columns.AddRange(new DataColumn[] { new DataColumn("Value", typeof(int)), new DataColumn("Display", typeof(string)) });
            cmdDataSource.Rows.Add(0, "cmb 0");
            cmdDataSource.Rows.Add(1, "cmb 1");
            cmdDataSource.Rows.Add(2, "cmb 2");
            DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
            cmb.HeaderText = "combobox";
            cmb.DataSource = cmdDataSource;
            cmb.DisplayMember = "Display";
            cmb.ValueMember = "Value";
            cmb.DataPropertyName = "cmdview";
            advancedDataGridView_main.Columns.Add(cmb);
            // disable the int reference view on datagrid view
            advancedDataGridView_main.Columns["cmdview"].Visible = false;
        }

Also don't forget to add a test value in the newrow object of the AddTestData() method. For testing purpose just select a value between 0 and 2 using r.Next(0, 2)

                        TimeSpan.FromHours(10).Add(TimeSpan.FromMinutes(r.Next(maxMinutes))),
                        // select a random value for the combobox view
                        r.Next(0, 2)
                    };

2) Another way could be override the CellValueChanged of the DataGridView to repait the value as a combobox, but it takes more resources.

cadmonkey123 commented 11 months ago

that worked thanks!