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
dataisnull. Currently your question shows a picture of the code and we can see aDataTablewith some data. But that is it… we have to take your word thatdataisnulland in my small tests, I was unable to get anullvalue as you describe. I suggest you create a minimal reproducible example that demonstrates what you claim.DataSourceas aDataTable. Have you considered making aBindingSourceand use theDataTableas aDataSourceto theBindingSourceand then set the gridsDataSourceto theBindingSourceas required?BindingSource’esDataSourceto theDataTable dtand then setting the “grids”DataSourceto theBindingSourceworked 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?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 assignedBindingSourceto theDataGridViewat the wrong time for some reason. I'm a goofus. Thank you for your guidance, it solved my problem!