Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using VS2008, a Window Forms C# application

Any one help me to create a copy of already created binding-source and assign it to newly created DGV because I am using several DGV in my application created in run time and every time it needs new data so I want to filter the existing data source and create a copy of it and assign it to DGV.

Please help me.

Thanks.
Posted
Updated 31-Oct-20 23:22pm
v2

Once you have done the copy, do you want both the copies to work as one source or independent of each other? If you want them to behave independent, create a copy of the existing DataSource using following code and then filter it and apply to the other DGVs.


C#
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(memorySteram, dataSourceObject);
newDataSource = formatter.Deserialize(memoryStream);
 
Share this answer
 
Comments
Member 14981275 2-Nov-20 0:46am    
some Data Sets like binding sources won't copy/clone like this specially in ADO.Net.
Unless I have misunderstood your question, there is no need to 'copy' the existing BindingSource.

Something like:
C#
BindingSource newBindingSource = new BindingSource();
newBindingSource.DataSource = oldBindingSource.DataSource;
newBindingSource.Filter = "FirstName = Smith";  // Or whatever

newDataGridView.DataSource = newBindingSource;
 
Share this answer
 
Comments
sher ali 10-Apr-11 4:45am    
becaus this binding source is binded to other DGV and when any change made to it then that change reflects to other DGV source there i want a copy of this binding source please help me thanks
Henry Minute 10-Apr-11 7:24am    
When you filter a BindingSource you alter the underlying data source (usually an in memory DataTable) so you do not, in fact, want to copy the BindingSource you want to copy the DataSource and for this the answer from d@nish would probably be the quickest way to do that, with one slight modification.

Try:
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(memoryStream, bsOld.DataSource);
memoryStream.Position = 0; // <=========== CHANGE FROM d@nish code
BindingSource bsNew = new BindingSource();
bsNew.DataSource = (DataTable)formatter.Deserialize(memoryStream);
bsNew.Filter = "LastName LIKE 'D*'"; // Or whatever

dgvNew.DataSource = bsNew;
Espen Harlinn 10-Apr-11 18:02pm    
Like it, my 5 :)
Member 14981275 2-Nov-20 0:48am    
some Data Sets like binding sources won't copy/clone like this specially in ADO.Net.
because They are not serializable
Try this to make a copy of a BindingSource object.

BindingSource newBindingSource = new BindingSource(oldBindingSource.DataSource, oldBindingSource.DataMember);
 
Share this answer
 
Comments
Nima MirzaAliKhan 6-Apr-20 7:54am    
@RaviRanjanKr this is not a clone or copy. this is Bind.
I had this Question For long time until now. I made an Extension Method for this.
For BindingSource Filled With Data From Database.
This Code Will Clone The Structure And Will Copy The Data. With No Bind Between them.
and fast.
edit: Now You Can Copy Just Filtered Rows.

C#
public static BindingSource Clone(
			this BindingSource bindingSource, 
			bool justFiltered)
		{
			try
			{
				DataTable tbl, tbl2;
				//
				if (justFiltered)
				{
					var dv =
					   ((System.Data.DataTable)bindingSource.DataSource).DefaultView;
					//Filter
					dv.RowFilter = bindingSource.Filter;
					tbl = dv.ToTable();
					tbl2 = tbl.Copy();
				}
				else
				{
					tbl = (DataTable)bindingSource.DataSource;
					tbl2 = tbl.Copy();
				}
				//
				BindingSource newBindingSource =
					new BindingSource(tbl2, bindingSource.DataMember);
				//
				return newBindingSource;
			}
			catch
			{
				
			}
			return null;
		}
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900