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?
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().
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.
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
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;
Related
I have a Dataset, which I've filled from a DataAdapter.
Now, I'd like to bind a DataGridView to my DataSet.
I can do the following:
dgvCustomers.AutoGenerateColumns = true;
dgvCustomers.DataSource = ds;
dgvCustomers.DataMember = "Table";
Or I can do this instead:
dgvCustomers.DataSource = new BindingSource(ds, "Table");
Is there any reason why one of these would be preferred to the other? My experiments seem to suggest that they both work fine. Thanks!
I have a DataGridView whose DataSource is bound to a DataView with a table bound to it. If need to be able to insert columns into the table so I just do this:
DataView.Table.Columns.Add(newColumn);
DataView.Table.Columns[columnCount-1].SetOrdinal(desiredIndex);
Trouble is after doing so the DataGridView reflect the change unless I do something silly like this.
DataView.Table = new DataTable("tempTable");
DataView.Table = orginalTable;
Wondering how to properly get the DataGridView to see the index change and redraw itself?
Update() and Refresh() did not work for me.
So I decided to go the following way:
var temp = dataGridView1.DataSource;
dataGridView1.DataSource = null;
dataGridView1.DataSource = temp;
I had a task to bind two DataGridView's, main problem I can't see the changes when I cast BindingSource data source to DataSet (I need this for SqlDataAdapter).
I can workaround this by deleting db table contents, but this is not suitable for me.
I could catch these events, but I need to update DB on disposing the Form.
var bs = (BindingSource)dataGridView1.DataSource;
var set = (DataSet)bs.DataSource; //here DataSet HasChanges() method always returns true
And when I use Tables from DataSet in SqlDataAdapter Update() and Fill() methods do the Insert for me, because values are always shown as new.
It is kind of though to explain it right, I hope I have succeeded.
Thanks.
EDIT
Fixed it by initializing DataSet on class level and adding AcceptChanges method.
Also removed casting from BindingSource.
Code snippet for initialization of DataSet:
dataSetT = new DataSet();
dataSetT.ReadXmlSchema(xmlSchemeFile);
dataSetT.ReadXml(populateDataSet == null ? xmlSchemeFile : xmlDataFile, XmlReadMode.IgnoreSchema);
var autoSource = new BindingSource();
var repairSource = new BindingSource();
autoSource.DataSource = dataSetT;
autoSource.DataMember = "Auto";
repairSource.DataSource = autoSource;
repairSource.DataMember = "VIN_FK";
dataGridView1.AutoGenerateColumns = true;
dataGridView2.AutoGenerateColumns = true;
dataGridView1.DataSource = autoSource;
dataGridView2.DataSource = repairSource;
dataSetT.AcceptChanges();
With AcceptChanges method now dataSetT.HasChanges() returns a realistic value. And then I use DataAdapter Update method.
Case closed.
I have found answer to my question, description is in edited question.
Thanks.
I have a static list of data called Services. I also have two BindingSource objects and this list is bounded to both of them:
this.bindingSources1.DataSource = Instance.Services;
this.cbx1.DataSource = this.bindingSources1;
this.bindingSources2.DataSource = Instance.Services;
this.cbx2.DataSource = this.bindingSources2;
When I set the Filter property in first BindingSource object:
this.bindingSource1.Filter = " Id = 2 ";
in cbx1 there is only one object but in cbx2 there are two. How do I set the binding so that setting the Filter property in one BindingSource does not affect the data source on the second cbx?
Do I have to use DataSet and DataTable?
Thanks
If you try set bindingSource to a DataTable and use DataTable dt=new DataTable();and use dt.DefaultView to make changes this wont affect data on data table
I think that you may need to create a new BindingContext for either bindingSources2 or cbx2 (we don't use BindingSources, which is why I am not 100% which one to apply it to).
I would try adding:
this.bindingSources2.BindingContext = new BindingContext();
immediately before:
this.bindingSources2.DataSource = Instance.Services;
I'm trying to set the dataGridView as a datasource of some other datagridview, but it is not working. Please help me.
this.dataGridView2.DataSource = this.dataGridView1;
No compiler error, but not working as well.
this.dataGridView2.DataSource = this.dataGridView1;
this will show no compile error because this.dataGridView2.DataSource is expecting a type of object and you are assigning an object to it, but no datasource assigned here , to do that
this.dataGridView2.DataSource = this.dataGridView1.DataSource
assign like this
to Solve the Problem of changing data in datagrid one while changing data on first one
Try this
DataTable dt = (DataTable)dataGridView1.DataSource;
dataGridView2.DataSource = dt.Copy();
#Nighil sounds close, I don't know if that worked for you but I would not recomend using dataGridView as a datasource, I suggest you should separate out the logic keeping data separate from user controls, also using a 'BindingSource' gives you one easy method for refreshing data;
Say we had a method called 'getTablefromDatasource()' which got the data to be displayed and returned a 'DataTable'
DataTable table = getTablefromDatasource();
BindingSource source = new BindingSource();
dataGridView1.DataSource = source;
dataGridView2.DataSource = source;
then you can refresh the data with:
source.ResetBindings(false);