C# - DataSets and Data Binding in multiple Forms - c#

I have the following scenario:
A database
A DataSet that is filled by the database
Two Forms:
The first Form contains a ComboBox that shows a certain set of categories. The ComboBox is binded to a DataTable "categories" in the DataSet.
The second Form is supposed to be a category manager where you can add, edit and delete categories from the database. It contains an editable DataGridView that is also binded to the same DataTable.
Now, when I change the DataSet in the second Form, both the DataGridView and the database are being updated, however the ComboBox in the first Form is not. What's the best way to keep it up to date? Somebody on the MSDN forums suggested something like that:
public void updateDataBindings()
{
// doesn't seem to do anything:
categoriesComboBox.DataSource = categoriesBindingSource;
}
This method of Form1 would be called whenever the DataGridView in Form2 changes. The ComboBox however remains unchanged.
Greetings!
wib

You've databound the dataset to the combobox, but have you also told the combobox what it's datamembers are, etc. so that it knows what value to watch?

Related

Transferring values from one Datagridview to another Datagridview on separate forms C#

I want to have five fields from my datagrid on my first form to be transferred to five columns on a separate datagridview on a different form. The rest of the data is going into textboxes. I have it so when I click next the selected row's information is sent to the next form.
Below is the code that I'm having issues with .
spectrumapp openForm = new spectrumapp();
openForm.datagridview1 = dtSearch.CurrentRow.Cells[13].Value.ToString();
I would simply pass the base DataRow from your first form as a parameter into your second form and then populate the DataSource for the DataGridView from that DataRow.

Bound DataGridView with multiple sources

i have been strugeling with this for some time now and its driving me crazy. This is the situation:
I have a bounded DataGridView i designed this with the visual studio designer. All the information shows accordingly.
in my DataGridView i have 2 ComboBoxes in here the data is also correctly displayed. I want that if i click on the ComboBoxea list of options shows.
Because the DataGridView is bounded to a source i can not use the ComboBox.Items.Add() method. So i created another Datasource in the designer and in runtime i change the datasource for that specific combobox. Now the list shows shows the options that i want, yeey !
Now, i want to save this newly added or changed row to the database.. so i use the following method for this (i call the method on the RowLeave event from the DataGridView):
if (tasksDataSet.HasChanges()
{
try
{
tasksBindingSource.EndEdit();
tasksDataSet.GetChanges();
tasksTableAdapter.Update(tasksDataSet);
}
catch (Exception ex)
{
}
}
This won't work for the ComboBoxes since this is another datasource.
So basicly what i want is:
Use a datasource for the DataGridView
Change/Add items to the DataGridViewComboBox
Save the changed made to the (complete) DataGridView to the database
How can i make this work ?
if your problem is to save current data which is given to Grid view.
than i suggest try to use session . while binding data to DataSource assign it to
session["SomeID"] . if your changing binding then again assign same session to it.
next step convert session to what ever you want to save.
ex:
//Datasource.
list<User> DataBoundSource = new list<User>();
DataGrid.DataSource = DataBoundSource;
DataGrid.DatBind();
//Assign to Session where you are binding this
//every time
Session["SameID"] = lsDataBoundSOurce;
//your code.
...
...
...
//covert that session to Datasource you want to save.
list<User> saveData = (list<User>) Session["SameID"];
this is the basic idea i got. i hop this will help you.
please give it +1 if it help you

edit multiple rows selected in a GridControl in TextEdits bound to the same bindingsource

My setup:
C#.Net 4.0, Windows Forms, DevExpress 13.1.5 although I doubt its a DX issue
I have a form with a GridControl (with GridView) on the top and a detail area that holds TextEdits and other edits in a LayoutControl below.
Both the grid and the edits below are bound to the properties of the objects contained in a list in the binding source.
The grid is set to ReadOnly, MultiSelect, RowSelect and all its columns are set to ReadOnly, not focusable.
Editing is only happening in the detail area below.
The behavior I want to create:
When multiple rows are selected in the grid the edits below should show the following:
the value of field in question is the same in all selected rows -> show the value
the value of the field in question differs between the rows -> show nothing
if the user writes into the TextEdit while multiple rows are selected:
the value of the edit should update the values of the same field of all the selected rows
Where I am with this:
I'm working on a solution by building a custom BindingSource that would be aware of the selection. It would bind the list of object to the grid and a single object that is not part of the list to the edits. Depending on the selection I would set the properties of that single object or forward its changes to the selected objects in the list.
I got that working for a single property with 2 binding sources and would now extend it to use reflection to do this for all of the public properties. I also want to encapsulate the whole behavior into a class that looks and acts like BindingSource just with that added behavior.
The question:
Is there a simpler way to achieve this? Does something already exist which can do this that I overlooked in either .Net or DevExpress? Are there traps to my approach that I should consider or why I should go about this totally differently?
I think that you can achieve your goal in a simpler manner:
Just bind a single BindingSource with all the data that you need to your grid. That should display the data.
Then, bind the required fields from that same BS do the edits through the DataBindings propriety.
You can then implement a save object (through a control or programatically) so the changes made in the edits are shown in the grid.
To check the grid values, you can use:
//get the handles of the rows
gridView.GetSelectedRows();
//get the value of the desirable cells
gridView.GetRowCellValue(handle, column);
Also, in future projects, consider using the Entity Framework to construct a data-aware model and custom objects based on the elements of your Database.
Hope this helped!

How to populate and select data in comboboxcell in datagridview

Friends, I've a datagridview in my windows application. It has 6 columns, out of which the 2nd(colindex 1) is of type combobox. Others are of textbox type. I've to populate this combobox from table1. There are two columns in table1 - ID and Name. Only Name will be displayed in the combobox. User can select any one from that combobox and write something in other 5 columns. Upon pressing save button, the ID of the selectedItem from combobox and other 5 textboxes' value will be stored in table2. When again the form will be loaded it'll fetch data from table2 and display corresponding data in datagridview. I've used datasource to populate the combobox. But it's not working. Do any one of you have any idea, how can I do it?
Try Setting the DisplayMember of the combobox to "Name" and the ValueMember of the combobox to ID.
And Also the bindings (For that matter any changes) you perform at design time is in InitializeComponent method of your class, depending on the version of Visual Studio you're using, It'll reside in a same or separate file.

How to update a database after a DataGridComboBoxColumn has changed?

I have a wpf datagrid that is bound to one dataset and has two combobox columns that take their values form the main dataset and has options from two different databases, in every other cell (which are regular textbox cells) once editing is finished the database is updated through an dataset.DataTable.RowChanged event, the problem is the two other columns aren't part of the same datatable (even though they take their value from that datatable) and so doesn't call the same function that is called when their value is changed. Also, I can't find an event that is called when the value is changed (only before it changes).
What can I do to get an event whenever a combobox inside a datagrid is changed?
Silly me, it appears that the update method is invoked (popped a MessageBox to check) and the algorithm had an issue when it was sent form the combobox.

Categories

Resources