0

I have a DataGridView that loads data from a file. I also want to use the DropDownFilter Column - I've used the one from Karl Erickson on Microsoft. It works all good while the columns are set in the Designer Mode.

I need to set the DGV and DropDownFilter columns programmatically since I don't always want to have all the columns with Filter. The problem is that the DGV is autogenerating its column type automatically. The solution might be the DGV.AutoGenerateColumns = false, however this will throw a BindingSource error while adding new DropDownFilter column to the DGV, let me describe:

The DropDownFilter column has a property FilteringEnabled, where the following code returns null to the BindingSource data object, even when the DGV.DataSource contains relevant data:

BindingSource data = this.DataGridView.DataSource as BindingSource; You can see in the image, that the DataSource is not empty, it has its columns and data as needed: DataSource visualization

Is it possible to create the DTG with custom column type in the way I intend, or am I missing something? How can DataSource return null to BindingSource when this DataSource contains regular data?

Update 1

Minimal code to reproduce the err:
Form1.cs


// Create WinForm app, add DataGridView control and the code below

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            LoadToDtg();
        }

        private void LoadToDtg()
        {
            dataGridView1.AutoGenerateColumns = false;

            // Prepare DataTable columns fill with data
            DataTable dt = new DataTable();
            dt.Columns.Add("Col1");
            dt.Columns.Add("Col2");
            dt.Columns.Add("Col3");
            dt.Columns.Add("Col4");

            dt.Rows.Add("ValueA1", "ValueA2", "ValueA3", "ValuA4");
            dt.Rows.Add("ValueB1", "ValueB2", "ValueB3", "ValueB4");
            dt.Rows.Add("ValueC1", "ValueC2", "ValueC3", "ValueC4");

            // The DataSource binding could be used here too, however it yelds the same error
            //dataGridView1.DataSource = dt;

            foreach (DataColumn col in dt.Columns)
            {
                DataGridViewAutoFilterTextBoxColumn newColumn = new DataGridViewAutoFilterTextBoxColumn
                {
                    HeaderText = col.ColumnName,
                    DataPropertyName = col.ColumnName
                };

                dataGridView1.Columns.Add(newColumn);
            }

            dataGridView1.DataSource = dt;
        }
    }

The DataGridViewAutoFilterTextBoxColum refers to already mentioned project @ Microsoft page. I didn't paste it here since it has 1400+ lines of code. You can download the zip directly here

5
  • ”How can DataSource return null to BindingSource when this DataSource contains regular data?” .. ? … It can’t and you show no code that demonstrates this. Please show us a reproducible example where data is null. Currently your question shows a picture of the code and we can see a DataTable with some data. But that is it… we have to take your word that data is null and in my small tests, I was unable to get a null value as you describe. I suggest you create a minimal reproducible example that demonstrates what you claim. Commented Jan 15, 2022 at 4:11
  • @JohnG Sorry about that, the reproducible example with instructions were added into the original post. Commented Jan 16, 2022 at 0:36
  • Well… after looking at the custom column you linked to… one thing I noted in the “Dependencies” section is that in order for it to work … ”The DataGridView.DataSource must be set to a BindingSource component.” … I do not see this in your code. The posted code is setting the grids DataSource as a DataTable. Have you considered making a BindingSource and use the DataTable as a DataSource to the BindingSource and then set the grids DataSource to the BindingSource as required? Commented Jan 16, 2022 at 1:01
  • After further testing, I do not get the error you described… Setting the BindingSource’es DataSource to the DataTable dt and then setting the “grids” DataSource to the BindingSource worked as expected as to filtering and without errors. The initial error I got was from the custom column complaining that the grid’s data source was not a binding source. Can you show an example where you get the error you described? Commented Jan 16, 2022 at 4:45
  • @JohnG I know that in the given example I didn't use BindingSource, however I tried to use BS in the original project, obviously incorrectly implemented. After reading your answers, I reviewed the code once again and there it was - I've assigned BindingSource to the DataGridView at the wrong time for some reason. I'm a goofus. Thank you for your guidance, it solved my problem! Commented Jan 16, 2022 at 18:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.