Bound DataGridView with multiple sources - c#

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

Related

How save changes to a database after IBindingSource.AddNew() method is used

I have a form that I use for both editing and inserting new users into a database. I use Entity Framework 6. All my controls (textboxes and one combobox) are bound to specific binding sources so when I update an entity with:
context.user.add(user);
context.Save();
my UI changes. And vice-versa. Now, updating works fine, but I have some problems with inserting of new users. Currently a data grid view has it datasource set, and I have disabled adding rows by clicking on a grid view directly (because I want that part to go through the form):
Okay, so far so good. Now when I want to add a new user, I do this:
myDataSource = context.user.Local.ToBindingList();
myDataSource.AllowNew = true;
and later on:
myDataSource.AddNew();
After calling AddNew() the blank row is added to a grid view, and when I insert values in my form, everything shows up automatically in the grid view.
The thing is, I can't really persist this data into a database. If I do:
context.Save();
nothing happens. I guess because the newly created entity by the AddNew() method is not attached to the current context. But I don't have a reference to it to attach it.
I guess that AddNew() creates a new entity, right? And adding it to the datasource. Correct me if I am wrong.
So how to save changes in this case?
I have a similar issue with getting an added record persisted to the database. I had same basic setup, using .Load, and setting the binding source data source = .Local.ToBindingList()
And found an odd fix to the issue. So under my binding navigator save event, this is the only way I could get newly added records to make it to the database. The last ResetBindings call is just so that the Identity Id shows it's actual DB value instead of zero.
private void mapBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
mapBindingSource.EndEdit();
mapBindingSource.ResetBindings(false);
mapBindingSource.EndEdit();
_db.SaveChanges();
mapBindingSource.ResetBindings(false);
}

Change selected DataGridView row given an entity

I have a DataGridView with DataSource set to a BindingSource control, that is bound to an ObservableList of entities.
I use this grid as a read only grid, for searching purposes.
When an item is selected, another BindingSource is updated with the current selected item.
No issues until here.
The issue is when creating a new item and I want to select the new item in the DataGrid, as after saving user is supposed to be able edit some data of the just created record.
How can I do that?
I can only provide the entity itself or the ID, but I can't figure out how to do that.
I'm not quite used working with Windows Forms, so I'm sure I'm missing some trivial solution.
You should bind the SelectedItem property of the grid to a property where you can set what entity you wish to be selected. Not sure but you might need to trigger a refresh on the grid because it might not automatically refresh it's View.
Ok, after some research and inspecting the "row" object, I've found this solution that perfectly meet my needs.
If anyone find any flaw with this approach, please let me know.
Once the new item is saved, I get the entity updated with the new primary key, then I call the following function:
private void SelectDataGridItem(Model.MyEntityType selectedItem)
{
foreach (DataGridViewRow row in MyDataGrid.Rows)
{
var boundItem = (Model.MyEntityType) row.DataBoundItem;
if (boundItem.Id == selectedItem.Id)
{
row.Selected = true;
break;
}
}
}

Two datagridviews in one windows form = not possible to save data to the second datagridview

I create a winform
I´m adding a datagridview by dragging a table from data sources
the result: I´ve got a winform with a datagridview and a bindingnavigator
I´m adding a second datagridview to the same winform by dragging another table from data sources
the result: same as bullet 3, but now with two datagridviews; a second bindingnavigator is not added though, don´t ask me why!
I copy the existing bindingnavigator and a adapts the copy to the second datagridview
I look into the .Designer.cs file, and it looks perfect regarding consistency
Now to the problem - even though both datagridviews have their own BindingNavigatorSaveItem_Click method, with code pointing out their respective bindingsources... it´s not possible to save changes from the second datagridview to the database!
Anyone seen this? Any suggestions what to do?
I solved it like this (I thank mr/mrs/miss dretzlaff17 who started up some processes in what´s left of my doped brain)
private void tableMeLikeBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
try
{
this.Validate();
this.tableMeLikeBindingSource.EndEdit();
// IMPORTANT: the following predefined generic Update command
// does NOT work (sometimes)
// this.tableAdapterManager.UpdateAll(this.rESOURCE_DB_1DataSet);
// instead we explicitely points out the right table adapter and updates
// only the table of interest...
this.tableMeLikeTableAdapter.Update(this.rESOURCE_DB_1DataSet.TableMeLike);
}
catch (Exception ex)
{
myExceptionHandler.HandleExceptions(ex);
}
}
I would suggest to set the DataSource property of the DataGridView in code through the use of a DataTable or object collection instead of using a bindingnavigator.

C# - DataSets and Data Binding in multiple Forms

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?

datagridviewcomboboxcolumn not returned with datagridview

i have created a datagridview that is populated with data from the database. When i am displaying the data in my application I add two datagridviewcomboboxcolumns which contain a list of selectable values. These display correctly in the application. The idea is that a user selects a value then hits a button that will take the value and update the database. The problem is that when the update button is pressed the two added datagridviewcolumns are not returned as part of the datagridview, instead when i try to reference the columns in my c# code only the original columns are available.
Please can someone advise as to how I can retrieve the datagridview with the new columns?
Many thanks,
Stu
Issue solved! I was declaring the ComboboxColumn both globally then locally when i was adding it to the datagridview! by removing the local reference and only using global i was able to retrieve the value at a later stage using
ComboboxColumn.DataGridView.Rows[x].Cells[0].Value.ToString();

Categories

Resources