Copy data from DataGridView - c#

I have two forms having each one a datagridview with the same columns; and I need to copy the same data from the 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.

Related

Find Object in List which is Data Bound to ComboBox

This question has probably already been answered, but i couldn't find a solution during 30 minutes of research.
The situation is as follows:
I am working on a C# Windows Forms project.
I have a Form called 'viewMain'.
Also these two classes are important:
With the winforms designer i added two bindingSources to the Form. One for objects of Auftrag and one for objects of AuftragErp. The DataSource for auftragBindingSource is set to an Instance of Auftrag. The DataSource for auftragErpBindingSource is set to the AuftraegeErp property of the Auftrag-Instance.
Now i can begin describing the problem. The ComboBox Auswahl on the Form is bound to auftragErpBindingSource and therefore always reflects the objects in the AuftraegeErp list. When i click the button on the form i want to get a reference to the currently selected object in the combobox.
I found that this can be achieved by using uftragErpBindingSource.Current.
But since the BindingSources are private i can't access them from the Auftrag Instance.
And i don't want to get this reference in an eventhandler of the form, since then i still would have to pass it on to the Auftrag instance. And if i where to do that, then i feel as though i could go without data binding altogether. The main reason for me to use data binding is not having to worry about keeping the objects and the gui synced.
So basically i have a list in a class. This list is bound to a comboBox. From inside of the class i want to get the object from the list which is currently selected in the comboBox.
I am thankful for every answer and hope that somebody can help me.
Edit for clarification: This Drawing shows the relation between the classes and the GUI. After the user has made his selection with the combobox he can click the Button. The onClick eventhandler then calls the 'doSomething' method. This method needs the selected object to function. There are multiple ways how the method can get the necessary object. I have shown two in the drawing. a) The method gets the objects from the list in its own class, or b) The method gets the object by using the .Current Property of the bindingSource on the Form.
I want to use approach a, because then i wouldn't have to pass any Variables or Objects between the Model and the View. Data binding alone would make sure that the GUI is always up to date. But i don't know how to implement approach a.

Should I use a ListView for single-row data?

I use Asp.net 4 and C#.
I'm developing a simple CMS, at the moment I want to displays the content for a specific PAGE (Title, BodyContent, and some other fields) at the end user on a web page (like as this website display its questions to its Users).
I need wrap my fields from database in appropriate HTML TAGS example <h1>Title</h1> <div>BodyContent</div>.
The resulting page should be READ ONLY .
I would like to know:
If a ListView web control with setting for a single row would be the
right choice (maybe binded with ObjectDataSource).
If would be better use separate Web Controls like Literals or
Labels to displays my fields sparsely on my webpage
In case of previous point how would I DataBind every single Control? Using different DataBinder or maybe with Linq an projecting using Anonymous Type.
I'm mainly concerned with read-only performance.
If you have data that's a single row, there's not much point in using a ListView. They're designed for showing lists of data (hence the name). If you have one item, consider either a DetailsView or single Labels.
If you have a particular class that often needs to be displayed in a certain way, consider setting up a Web User Control that can be bound to instances of that class.

Silverlight: Use a collection for multiple options in a Data Form?

I have a data form that is working pretty well. One of the properties of my data object is an ObservableCollection. Right now, in the data form, it only displays the ToString() of the colletion. Not very helpful.
I'd like some sort of autocomplete box with valid options, and when the user selects them they will be added to the data object's collection. (I discussed doing that in this question, but it's not using data forms.)
Is there a way to do this, while using the rest of the controls automatically generated by the data form? I'd rather not specify everything myself, just to get this one control.
I'd also like to override a normal text input box for a field to make it an autocomplete box.
You need to bind to a public property of the object in the collection.

Return updated POCO object attached to GridView control using Reflection

I have a Custom GridView Control derived from ASP GridView Control. Now on all the Forms i have been using my CustomGridView Control.
I attach a List object to DataSource of this CustomGrid, and also store this List into ViewState so that i can manipulate this object when ever grid performs any postback.
Now every time i do insertion, updation and deletion i have to manually write code for Data Manipulation in my all Form. i.e. retrive data from ViewState and then update data or delete data or insert data into that object and store it again into ViewState.
I want a way to automatically write Code in my CustomGridView class object i.e. at one place to take care of this task. My CustomGridView Control should able to update, insert and delete for all my forms without Form writing any code at all.
In the end when its time to save the data into persistent storage i should get the final POCO object from CustomGridView Control.
This can be achieved using reflection but how?
Can you please tell me how can i achieve this task? Or what would be the best approach that i should follow?
Thanks,
Huzefa
I question whether it is best practice to store your underlying datasource within the view state. Have you considered putting this list in the session instead? It would be easier to access and you wouldn't burden your users with inflated page sizes due to overweight viewstate.
If you need to go with your stated approach, could you consider putting the code into a base form instead of trying to wedge it in the gridview? That way you only need to write the code once but it's more reasonable than trying to hack the gridview itself.

Explicitly handle or create the data object that’s bound to the control

I read somewhere that one of few weaknesses in data binding model is that you never explicitly handle or create the data object that’s bound to the control and as a result you don’t have a chance to add an extra item. But I’m not sure what is meant by that.
What or how exactly would you be able to add an extra item if you were allow to explicitly create a data object? Would you basically be able to bind control to data source and let the runtime automatically fill the control with data source’s data , and additionally that control could also get an extra item from data source by manually creating it?! How would it be able to do that?!
thanx
When you use the OnItemDataBound event, you have full access to the underlying datasource object via e.Item.DataItem. You can use this data to populate any controls in the ItemTemplate via e.Item.FindControl("controlname"). You can also use functions inside the <%# %> tags to format text or calculate values.
What you have read, in my estimation, is pure crap. Up until the point of binding, I can alter the objects in question. One common scenario, for example, is adding a column to rows in a DataTable object (which is actually a collection of rows and columns). I can, in fact, alter by adding a column (let's say sum) to each row.
I can, with some restrictions on classes, do the same with other types of collections and objects.
After I have bound the object, I can still add items to the output by using the databinding method for a row, so I am still not restricted.
In general, I find those that are expounding this garbage are defending using ASP style code in an ASP.NET page.

Categories

Resources