6

I am using Winforms DevExpress and I am binding a DataTable to a DataGridView which is working fine. The problem I am having is that I have some functions that will build a new DataTable object which is separate from the original which needs to replace the original DataTable that is bound to the DataGridView.

DataTable originalTable = new DataTable("OriginalTable");

//Populate originalTable 

myDataGridControl.DataSource = originalTable;

Everything works fine with the code above, but the following code creates a new DataTable and needs to be set as the DataSource for myDataGridControl.

DataTable newTable = new DataTable("NewTable");

//Populate newTable

//Set newTable as the DataSource for myDataGridControl
myDataGridControl.DataSource = newTable;

I have tried several different attempts to make this work such as calling RefreshDataSource(), Refresh(), setting DataSource to null. I have not gotten it to work yet. How do I do this?

King King
  • 61,710
  • 16
  • 105
  • 130
TheJediCowboy
  • 8,924
  • 28
  • 136
  • 208

4 Answers4

9

Try using a BindingSource, like this:

DataTable sourceTable = new DataTable("OriginalTable");
BindingSource source = new BindingSource();
source.DataSource = sourceTable;
myDataGridControl.Datasource = source;

Now when you want to re-bind, update the sourceTable variable, like this:

sourceTable = new DataTable("NewTable");

// If the structure of `OriginalTable` and `NewTable` are the same, then do this:
source.ResetBindings(false);

// If the structure of `OriginalTable` and `NewTable` are different, then do this:
source.ResetBindinds(true);

Note: Read BindingSource.ResetBindings Method for more information about ResetBindings().

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • 2
    I tried this exactly, and it doesn't seem to be working. Not sure why this would not work. – TheJediCowboy Aug 28 '13 at 03:21
  • This seems to be updating the rows, but the columns are not updating. I have verified that the columns are in fact in the sourceTable, but they are not showing the change on the grid control. – TheJediCowboy Aug 28 '13 at 18:57
6

Having you tried the following combination?:

myDataGridControl.DataSource = originalTable;
myDataGridControl.DataSource = null;
myDataGridControl.DataSource = newTable;

In my experience, setting the DataSource to null then to the second source does the trick.

Khan
  • 17,904
  • 5
  • 47
  • 59
  • 2
    This is actually the first attempt I tried at making it work, but it did not change it unfortunately. This has worked for me in the past as well. – TheJediCowboy Aug 28 '13 at 02:53
1

In case anybody is having trouble even after trying the other suggestions, the following call to PopulateColumns() on the GridControl.MainView property solved the problem for me.

For example:

myDataGridControl.MainView.PopulateColumns();

This can also be referenced from the following article with DevExpress. http://www.devexpress.com/Support/Center/Question/Details/Q362978

TheJediCowboy
  • 8,924
  • 28
  • 136
  • 208
0

Kinda old topic, but since it bugged me, I decided to share my experience... Binding the source didn't work for me and Datagridview doesn't have "MainView" variable.

I suspect the issue happens, in my case, after running the sorting command:

MyDataTable.DefaultView.Sort = "Column Asc";
MyDataTable = MyDataTable.DefaultView.ToTable();

My solution was to rebind again after actions performed:

myDataGrid.DataSource = MyDataTable;
jclozano
  • 618
  • 6
  • 24
Dima Sherba
  • 152
  • 1
  • 8