Copy data from DataGridView
I have two forms having each one a datagridview with the same columns; and I need to copy the same data from the开发者_Python百科 DataGridView in form1 to the DataGridView in the form2.
Any ideas ?
Thanks.
Once you have parsed your data from the text file, ensure that it is in an IEnumerable
format - i.e. an array or List of strings or your custom data objects, let's call it myListOfStuff
.
You can then bind this to as many DataGrids as you like. As you will be binding the same object reference to multiple DataGrids, any changes you make to myListOfStuff in one form will be visible (but not necessarily rendered) to anything else using myListOfStuff in another form.
How you pass myListOfStuff around depends upon whether you are using Web or WinForms.
If you are using WinForms then you can pass myListOfStuff between forms in several different ways - you could have a property on the forms which you assign the variable to, or you could pass it in on a constructor of the form, etc etc.
If you are using WebForms, then the simplest way to pass myListOfStuff between forms (pages) is to store it in Session, and both pages can access it from there. Or you could store it in Cache and access it via a static method.
I like slugster's idea and yes you could have a List of objects that would have a type of "myListOfStuff" that could in fact have properties that could hold the information you are parsing from your text file. Each grid would then be able to bind to perhaps a generic list, List and the fields mapped to the properties represented by the myListOfStuff class.
精彩评论