Unbind datasource after populating combobox - c#

basic question - is it possible to populate a combobox with datasource and then cut the link to the datasource, but keep the data in the combobox? this will enable you to reuse your ds.Tables[0] below without affecting the first populated combobox.
comboBox1.DataSource = ds.Tables[0];
(The ds is populated from MS SQL Server with SqlDataAdapter)

No, it is not possible without copying the source data in some way. The DataSource property is a reference to your dataset, datatable or whatever object not a copy of it. Setting DataSource to null will remove any possibilities for the combo to see the data referenced.
Instead you could easily create a copy of your original table using the appropriate method available in the DataTable class
comboBox1.DataSource = ds.Tables[0].Copy();
This create a new copy of the table with the actual structure and content, but it is a copy in another memory area of the informations stored in the first object. At this point you have two distinct objects in memory and you can change the first one without affecting the second.
Let me say also that this is not very smart with big tables. Don't use this approach if your table contains a lot of records for obvious reasons.

Related

How to get the Original Row and the Modified Row of DataRowView using DataRowViewVersion

I have a datatable that is filled from a database. I load a bindingsource with that table.
Sub LoadData()
Dim bsTemp As BindingSource = New BindingSource
bsTemp.DataSource = dtTemp
End Sub
I then have other code programmatically editing the values in the datatable. I NEVER call AcceptChanges() ..let me be clear NEVER.
I do call bsTem.EndEdit() and I also call that on my dtTemp.Row(x).EndEdit() Whenever I make a change to it.
So now all I want to do is compare the two rows (I know I can do this with a for each column but I am not wanting to do that.)
I would like to know how to make this work:
Dim modview As New DataView(dtTemp.Copy, "", "Id", DataViewRowState.ModifiedCurrent)
Dim origView As New DataView(dtTemp.Copy, "", "Id", DataViewRowState.ModifiedOriginal)
So I can then perform something like this:
Dim rowComparer As DataRowComparer(Of DataRow) = DataRowComparer.Default
IsEqual = rowComparer.Equals(origRow.Row, modRow.Row)
When I do this both views show the Modified data, one of them should only show me the Original unmodified Row.
I know I can do this [C# version]:
SomeDataRow[0, DataRowVersion.Original] //by index
SomeDataRow["ColumnName", DataRowVersion.Original]
But again tis works on a column by column basis - me being the iterator - and I see no reason to do that when the DataView supposedly has this built in .
So what could I be doing wrong that I do not see the original version .
The New DataView(..) does not determine which rows to copy, it only says what the state of the rows after they are in the view will have. Your first parameter says which rows dtTemp.Copy.
Since the copy method of a datatable is a copy of all rows, you might want to use something like the select method, which allows you to filter based on state.
dtTemp.Select("","",ModifiedOriginal)
EDIT:
There must be an issue with getting it that way. According to the example given from MSDN, it should work if you use the DataView.RowStateFilter. I tested it and it did work. I think the key is that the DataView controls what you see. If you look at the raw data through the DataRow, it is always current.

Assigning a different ViewSource in WPF

I'm experimenting with C#/.net/WPF all for the first time. I've created a project and set up a datasource (just a table with some sample data) and created two tableadapters named Prods and Prods1 - the latter has a filter applied in the query to return slightly different results. I've dropped both tables on my form and both dutifully display their respective data.
I thought I would then swap the data source for each. So the default generated Window_Loaded:
MSDSTest.prodtestDataSet prodtestDataSet = ((MSDSTest.prodtestDataSet)(this.FindResource("prodtestDataSet")));
// Load data into the table Prods. You can modify this code as needed.
MSDSTest.prodtestDataSetTableAdapters.ProdsTableAdapter prodtestDataSetProdsTableAdapter = new MSDSTest.prodtestDataSetTableAdapters.ProdsTableAdapter();
prodtestDataSetProdsTableAdapter.Fill(prodtestDataSet.Prods);
System.Windows.Data.CollectionViewSource prodsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("prodsViewSource")));
prodsViewSource.View.MoveCurrentToFirst();
// Load data into the table Prods1. You can modify this code as needed.
MSDSTest.prodtestDataSetTableAdapters.Prods1TableAdapter prodtestDataSetProds1TableAdapter = new MSDSTest.prodtestDataSetTableAdapters.Prods1TableAdapter();
prodtestDataSetProds1TableAdapter.Fill(prodtestDataSet.Prods1);
System.Windows.Data.CollectionViewSource prods1ViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("prods1ViewSource")));
prods1ViewSource.View.MoveCurrentToFirst();
I now want to make the first data grid (prodsViewSource) instead display the data for the second table, and ignore the second table entirely. So, I changed that as follows:
MSDSTest.prodtestDataSet prodtestDataSet = ((MSDSTest.prodtestDataSet)(this.FindResource("prodtestDataSet")));
// Load data into the table Prods. You can modify this code as needed.
MSDSTest.prodtestDataSetTableAdapters.Prods1TableAdapter prodtestDataSetProdsTableAdapter = new MSDSTest.prodtestDataSetTableAdapters.Prods1TableAdapter();
prodtestDataSetProdsTableAdapter.Fill(prodtestDataSet.Prods1);
System.Windows.Data.CollectionViewSource prodsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("prodsViewSource")));
prodsViewSource.View.MoveCurrentToFirst();
With the second block having been commented out.
I must be missing something fundamental - what I think I'm doing is redefining the prodtestDataSetProdsTableAddapter variable to use an instance of the prods1 table adapter, and then using that to populate the prodsViewSource grid on the form, but I end up with a blank. Where's my error?
...
Well, I posted this after beating my head against it for an hour and, minutes later, realized the FAR easier thing to do is to just change the datacontext property of the grid in question.
I would still like to understand why doing it the vastly more complicated-bordering-on-Rube-Goldbergian way didn't work, though, so if anyone can explain that, it would still be welcome.

Datasets - Menu to switch between tables in form

I'm having a bit of difficulty with datasets in C#. I know how to load datasets and everything, and I can drag a table from the dataset into the form window and it displays the columns etc of that table. However, I would like to display every single table in the form, but in a clean way. Is there a way to create a dropdownlist for example, that will display all the tables in the data set, and then I can select the one I want and then display the columns, etc? Just need a way to display tables in the form and navigate between them and have them display their data. How would I go about doing this?
First of all you need to create an instance of DataSet (typed dataset) (say DatabaseDataSet)
DatabaseDataSet ds = new DatabaseDataSet();
Then create adapter's instance which is an autogenerated class.
DatabaseDataSetTableAdapters.yourTableTableAdapter adp;
adp=new DatabaseDataSetTableAdapters.yourTableTableAdapter();
Pupulate the datatable object and push into to ds.
adp.Fill(ds.yourTable);
Use DataBinding technique to show data from the dataset object.
comboBox1.DataSource = ds.Tables["yourTable"];
comboBox1.DisplayMember = "column1";
comboBox1.ValueMember = "column2";
Off-topic:Use Collections, LINQ and Entity framework. The DataSet is bit outdated and it has many problems. Please take a look at this MSDN post.
DataSets vs. Collections

How to share DataTable between multiple Datagridviews across Application and save precious memory

I have a list of companies in DataTable format which is static and accessible across my application.
Different Forms in the application just show filtered Data like Customer Companies ,Supplier Companies ,Misc etc in a DataGridView.
The DataGridViews are just for the sake of displaying the Data.
If I query my companies DataTable for only customer companies and assign the result to DatagridView.Datasource, it will just make a copy and assign it DataGridView.
Imagine if I have a list of 10,000 Customers and User open 10 forms for different purpose, I will be just duplicating 10,000 * 10 rows of data.
Also if my main Companies DataTable is Updated , I will have to update all the DataGridViews too.
Will using DataViews help me save the memory ?
Will BindingDatasource do good?
or is there any better method to share DataTable without making copies in memory for just Displaying Purpose.
Regards
Given:
I have a list of companies in
DataTable format which is static and
accessible across my application
I assume 1 DataTable. You should use RowFilter see MSDN: RowFilter:
DataGridView customerView;
DataGridView supplierView; // initialize in form
DataTable companiesTable; // initialized and filled
void SetCustomerCompanyView()
{
DataView cust = new DataView();
cust.Table = companiesTable;
cust.RowFilter = "Type = 'Customer'";
customerView.DataSource = cust;
}
// repeat for SetSupplierCompanyView()
If the concern is that one large DataTable is going to be copied throughout multiple active views, then you should really re-think the architecture. If a form is hidden or inactive, you can always dispose of the query results (in worst case) and re-query if/when the form becomes visible or active again (such as a tabbed form). For what it's worth, 10k records is easily handled in most systems and should accommodate several open views (filtered as above) of that DataTable.
An overtly simplistic comparison:
You can think of a DataView as a List<int> where int would be a row index. When you apply a row filter, the appropriate row indexes are appended to the List. You could easily create a view using LINQ to DataSets - extracting the row indexes you require based on a column or columns of data and append to the List. Now you use that list to reference only the rows (by Table.Rows[RowIndex]) in which you are interested from your primary DataTable.As stated, this is much simpler functionality than what the DataView is actually providing. You have not made a copy of the DataTable - you have simply created a shallow reference mechanism.

VC# 2008: Get Table names from DataTable into ComboList / How to merge tables from 2 MDBs?

My program that I am writing's purpose arose with this issue:
There are two users, each user saves to a .MDB file. One user has half the updated / correct information (the other half is outdated) and the other user has half the information (the other half is outdated).
User1: 25% + 25% = 50% current information needed the other 50% is outdated
User1 works on 2 out of the 4 items.
User2: 25% + 25% = 50% current information needed the other 50% is outdated
User2 works on 2 out of the 4 items.
I need to take that 50% (2 out of the 4 items) from lets say...User1 and add it to User2 making it 100% current (4 out of 4 items).
Their SQL style table structure is (should be anyways) identical (but if possible I would like to provide an event where for some a new table was added I would know)
If I could find out how to get all the table names from the DataTable, I could systematically array through the DataTable and replace the tables with the tables from the other .MDB file that I know need to be updated. I know DataSet has "DataSet.Tables" ...but that doesn't help me very much.
If I can do that, I can also add the tables to a combo box and create functionality to where whatever the combo box says, thats the table I will list on my Datagrid.
If any of you have any ideas on how to go about doing this (or if you even understand what i'm saying) please let me know. I'm 70% done with ths project, and these seem to be my last logic road blocks. I think I explained this right.
How do I list just the Table names
in a DataTable object.
What are your ideas on taking specific Tables out of a .MDB file and adding them to another .MDB file?
How would I go about inputting a ComboList drop-down box that included all the tables names...when I changed the table name it would list those contents on a Datagrid.
Is there a way to list tables on a Datagrid, and when you click on a Table it lists the contents of that table (kind of like a Tree structure).
EDIT:
I think he is right! I think DataTables are just one table whereas DataSets are sets of Tables. With that in mind, how do I list all the tables in a .MDB file into a DataSet? That would fix my problem perfectly.
I thought that a DataTable object was only a single table and a DataSet was what contained one to many DataTables.
If you are looking for the actual name of your DataTable, that would be accessed through the DataTable.TableName property.
Edit: If you are wanting to add DataTables into a DataSet object, just create a new DataSet and then use the .Add() method.
Dim DS as new DataSet
Dim DT as new DataTable("TableName")
DS.Add(DT)
You should then be able to loop through your DataSet and retrieve table names by accessing each DataTable's TableName property:
For each table as DataTable in DS.Tables
Console.Writeline(table.TableName)
Next

Categories

Resources