I have a DataGridView on my form that I have associated with a data source using the designer. This allows me to customise the columns in the designer, which helps me a lot. My problem is that I now cannot work out how to submit changes made in the grid view to the database.
I have a DataGridView object, a TableAdaptor object and a BindingSource object - I cannot figure out which combination of calls to make to fluck all changes made in the grid to the database. Ideally, I would also be able to refresh the grids with the latest information from the database as well.
Does anyone have any ideas?
Cheers,
Oops - Straigth after posting I stumbled across the solution:
myTableAdaptor.update(MyDataSet);
For me, both the table adaptor and the data set had been auto-generated as class member variables.
Related
I have created a DataSet with multiple tables. I use a few tables on one form, and then 2 other tables on 2 other forms. When I try to do the binding of the data, I cannot figure out how to have all 3 forms refer to the same DataSet object. In the interface (Visual Studio Express 2013) I can select from the Project Data Sources list and get my MainDataSet class and then select whichever table I want. But if I do this, I get a new object. None of the other options seem to allow me to pick an already existing object. I thought I used to see an option to pick my MainDataSet object from somewhere, but I don't currently see that option and even so, it didn't allow me to select a table anyway.
I know I could look closer at the binding code and figure out how to make this work, but since some of that code is in the Designer file, I can't modify it so it seems the best I can do if I do this is replace what it did with new bindings. And it seems like I shouldn't have to do this. There HAS to be a way to do this in the Visual Studio GUI.
You should create a BindingSource in the designer, then set its source to the DataSet in the designer.
You can then change your form's constructor to set the BindingSource's source to a shared DataSet.
Based on SLaks' suggestion, here is what I did.
I dragged a BindingSource onto a form and named it sharedDataSource.
In the Properties of the BindingSource I set the DataSource by selected MyDataSet class under the Project Data Sources heading. I believe this created the DataSource object and bound it to the BindingSource.
In the designer I chose the data source for the the DataGridView objects by selecting sharedDataSource and then the appropriate table under that.
For the DataGridViews in the second form I selected Other Data Sources-->Project Data Sources-->MyDataSet.
I created a constructor for the second form that takes a MyDataSet object as a parameter. In the constructor I set both SomethingOrOtherBindingSource.DataSource and mainDataSet to the paramater. I think the former is required, but the latter may only be required if you refer to it in your code.
When I create the second form (from the first form), I passed in sharedDataSource to the constructor.
So in the designer a data source is bound to the grid and you can use the designer to do whatever you need to (e.g. change column sizes, etc.), but when the program is run, the data source is changed to be the shared one. It seems to work.
My question is regarding ASP.Net Table control.
Is there any way to access a control properties after it is put inside the table?
The problem is control does not seem to appear inside the properties window any way.
I want to know if there any way to access these properties "Other than" going to HTML and changes them there.
From the MSDN docs on asp:Table:
It is important to remember that any programmatic addition or
modification of table rows or cells will not persist across posts to
the server. This is because table rows and cells are controls of their
own, and not properties of the Table control. To persist any changes
to the table, rows and cells must be reconstructed after each
postback. In fact, if substantial modifications are expected, it is
recommended that a DataList, DataGrid, or GridView control be used
instead of the Table control. As a result, the Table class is
primarily used by control developers.
are you trying to build a server control?
I'm quite new to C# (coming from a Java background) and I'm working on a Form to quickly manage different data (e.g. Users, Domains, Computers) along with providing utilities that use the data.
To avoid confusion the following is a summary of the example data structure;
<User>
<Name>joe</Name>
<Domain>europa</Domain>
</User>
<Domain>
<Name>europa<Name>
<Domain>
<Domain>
<Name>group</Name>
</Domain>
<Computer>
<Name>localhost</Name>
</Computer>
In my Form I have tab pages for each data, each containing a DataGridView (containing a column for each member) to allow for simple management. I'm using a single DataSet which reads the data from an XML file (using a schema) when the Form Load event is fired. After reading the data I am setting the tables as the DataSource of their respective DataGridView using the code;
userDataGridView.DataSource = dataSet.Tables["User"];
domainDataGridView.DataSource = dataSet.Tables["Domain"];
computerDataGridView.DataSource = dataSet.Tables["Computer"];
This is working properly for the Domain and Computer DataGridViews as they only have single text columns which are mapped to the Name properties. However, the Domain column of for the User DataGridView is a combo box and I need that to contain all the possible Domains (e.g. europa, group) as well as the selected Domain being bound to User.Domain (also, the User.Domain being initially selected).
My main question is how I achieve the above but I also have some additional questions hopefully someone can answer;
I'm assuming that changes made to a DataGridView are immediately persisted in the underlying XML file as I am reading it from the build's output directory. Is this correct or is additional configuration/process required?
During my attempts at getting this to work I tried to use a DataRelation as follows;
DataRelation dataRelation = new DataRelation("Domain users",
dataSet.Tables["Domain"].Columns["Domain"],
dataSet.Tables["User"].Columns["Domain"]);
dataSet.Relations.Add(dataRelation);
How exactly do DataRelations work and what effect do/can they have?
In case it helps you understand/explain I am using the SharpDevelop IDE for working on this application.
Thanks in advance.
As you have already found out, the DataGridView is very powerful and does most of the work in an automagically way. Unfortunately you run into problems if these defaults doesn't match your preferences (like using a ComboBox for a text property).
To get the DataGridViewComboBoxColumn into the proper place you can do this programmatically within your code or (if possible) do it with the designer (in Visual Studio don't know if SharpDevelop supports it).
Using the (Visual Studio) Designer
For this scenario it is necessary that the structure of the data is known at design time by providing a class holding all (or more) informations as simple properties (like a person class with properties name, age, etc.). If this is available you can add a BindingSource to your control and click in the properties window at the button next to the DataSource property. Select Add Project Data Source and select Object and select your desired object.
Now you have a binding source configured with a specific type of DataSource. Next select your DataGridView and change the DataSource property to the recently created binding source.
After this the DataGridView will automatically be populated with a column for each property. Now you can easily step into the Columns property and change the behaviour and type of each column as you like.
To connect your concrete data with the DataGridView just apply your table to the binding source.
Doing the same at runtime
If you have Visual Studio and followed the above steps you can afterwards easily take a look into the Designer.cs file to find how Visual Studio did all the stuff. There is nothing you can't do also manually. So if you need to just do it.
Simply create a DataGridViewComboBoxColumn, set the DataPropertyName and HeaderText and you've got a good starting point. Get the IndexOf() the column you want to replace, remove it and Insert() your freshly created column at the position you want.
Before the Grid can show some data (in the ComboBoxColumn) you need to provide a list with the possible values. For this purpose the comboBoxColumn has a DataSource property on itself where you can provide the desired values. With the simplest scenario you just provide an ICollection<string>, but you can also give something more complex like List<KeyValuePair<Enum, string>>. If that's the case you should also set the properties ValueMember and DisplayMember to tell the ComboBox which properties of the objects should be used to populate the list.
Maybe with these informations given you should take a look at the MSDN article about the DataGridViewComboBoxColumn and study the example. This should give you some additional hints on how to set it up properly.
I have a GridView which I am binding to my service layer.
I want to be able to allow the user to edit the grid, but I do not want to save the grid as the user is clicking update on each row. I would like to update all of the edited/added/deleted rows when the 'save' button for my entire form is submitted.
I have my service layer configured, and the GridView calls update on a per row edit basis, however I want that to happen all at the end when clicking save.
How can I maintain my ObjectData sources references to update, insert, delete but instead of on a per row basis be able to call a save on everything all at once?
Thanks!
If you're using an object data source, then the behavior of the object bound to the object data source is up to you; it doesn't have to save to the database right away. If you want, you can build the database commands you want to execute, then cache them somewhere until the save button is clicked.
Object data source objects should be static or stateless, so you can't cache there. However, this sounds like a reasonable use of Session cache.
Here is a tutorial on asp.net/learn for wrapping updates in a transaction:
http://www.asp.net/Learn/Data-Access/tutorial-63-cs.aspx
The example uses GridView and ObjectDataSource.
This may or may not be useful with subsonic, but it may help others with a similar problem.
I am working in c# project, i need to update datagridview control after inserting a new record. When i am doing this, the gridview keeps its old values also, what should i do to show only updated records
You need to show what the data source that you are binding to is. If it doesn't have any update functionality (implementing INotifyPropertyChanged, for example), then the grid won't be able to tell when a value changed and update itself.
You haven't posted a snippet of your code for us to analyze so my best guess would be that you are neglecting to re-bind the Data/Gridview after row insert.
You need to call ResetBindings when the bound data source changes unless the data source implements the appropriate notifications to give an update notification to the grid.