I have a TextBox to which I have given a DataBinding as follows:
txtCompanyAddress.DataBindings.Add("Text", CompanyDetailsDataSet,
"CompanyDetails.CompanyAddress");
Also I have added a BindingManagerBase object on form as below:
protected BindingManagerBase BindingManager
{
get
{
return this.BindingContext[CompanyDetailsDataSet, "CompanyDetails"];
}
}
I have a cancel button on my form which cancels the changes. When I update the value in txtCompanyAddress and hit cancel, I call BindingManager.CancelCurrentEdit(); but the text box value does not change to old one.
Is this any data bindings issue?
Your bindings will not refresh automatically with this setup you need to change the mode when data binding is updated
txtCompanyAddress.DataBindings.Add("Text", CompanyDetailsDataSet,"CompanyDetails.CompanyAddress", true,DataSourceUpdateMode.OnPropertyChanged);
Also you haven't posted what your CompanyDetailsDataSet is so i assume it implements IEditableObject interface. Otherwise you will have to do it on your own and ensure that old value is cached and retrieved on CancelEdit()
textBox1.DataBinding.Add("Text",ds.Tables[0],"ColumnName")
where ds is the object of DataSet and [0] is the number of table in dataset you can write the name of table in double codes in the place of 0 if your procedure returns more then one table...
Related
C#, WinForms, DataGridView, DataTable. My form allows the user to enter data into a new row (as opposed to, say, popping up a single record view of the record). When the user clicks my Save button, the click event looks like this:
DataTable dtAddRows = this.SomeFictitiouslyNamedDataSet.SomeFictitiouslyNamedDataTable.GetChanges(DataRowState.Added);
My assumption is that calling GetChanges with that enum is going to get me a set of the rows that were added to the grid at run time. However, it errors out because 1 of the columns "Last_Updated_DT" (which must be displayed and readonly) is a date column that will be populated when we write to the datatable. However, the DataSet has a rule that this column cannot be null.
The problem is, I get an error indicating that the Last_Updated_DT column is actually null (surprise surprise). Its a new row, and the column is readonly in the grid, so of course it is null.
How do I get around this error or stuff a value in that (or those) rows for that column before I actually try to get the added rows?
For the sake of posterity:
private void myGrid_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells["MyColumnNameGridViewTextBoxColumn1"].Value = String.Empty;
}
Adding that event to my code fixed my issue. I set the value to String.Empty so it wouldnt put a date/time value in the column until after the Save was clicked.
Then, in the Save_Click event, I get the added rows with this code:
DataTable dtAddedRows = this.dsMyDataSet.MyDataTable.GetChanges(DataRowState.Added);
Then, in the TableAdapter.Insert parameter list, I added DateTime.Now for the date parameter.
Eventually call MyTableAdapter.Update method using the DataSet overload.
All fixed.
Hat tip to #KiNeTiC for providing the necessary guidance.
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
C#, VS-2010, winforms
Generally I want to create user control with a bindable MyProperty. But I met some problem: Data row become modified when current row changed.
You can download C# project and try it yourself:
test_bind.zip
To make it simple I did follow:
create test form with a
dataGridView (set it ReadOnly),
TextBox,
some typed DataSet with some Table
bindingSource
bind bindingSource to the table
bind dgv to bindingSource
bind TextBox.Text to bindingSource to some text column ("name")
on form load fill table with some data. (If you add rows manually do not forget AcceptChanges())
add event handler for table xxx_ColumnChanging(...) and set breakpoint there
all standard steps, nothing special
Run
as expected - rows in dgv, current name in textBox, click on different rows in grid...
and only when I modify text in textbox I stop in breakpoint.
Lets modify program, and bind textBox to different property instead of Text - lets say Tag.
Set DataSourceUpdateProperty "OnValidation" or "OnPropertyChanged".
Run
Now current row become modified. !!!
Same happens when instead of textBox I use UserControl with a dummy property
String MyProperty { set; get; }
How to bind custom property in user control with the same behaviour as TextBox.Text?
Note:
Binding property itself is not a problem. Problem is: when I bind to standard TextBox.Text property - everything Ok Wen I bind to ANY OTHER property (Tag) or to custom Property in User Control - then DATASOURCE ALWAYS BECOME MODIFIED FOR CURRENT ROW
Short answer is:
When bind to simple property, declared like
[Bindable(true, BindingDirection.TwoWay)] // I need TwoWay
public String MyProp {set;get;}
then datasource will be updated on current position change. You may count it as Bug or Feature...
To change behavior - I need magic word. Declare event in the control as
public event System.ComponentModel.PropertyChangedEventHandler MyPropChanged;
This name is special: "YourPropertyName" + "Changed" = YourPropertyNameChanged
Other word will not be magic.
As soon as event declared - datasource would not be updated every time as before...
and now I can control the process...
public void OnMyPropChanged(System.ComponentModel.PropertyChangedEventArgs e)
{
Binding bb = this.DataBindings[0] as Binding;
bb.WriteValue();
if (MyPropChanged != null) MyPropChanged(this, e);
}
private void TxtChanged(object sender, EventArgs e)
{
if (load) { return; }
_my_prop = Txt.Text; // ...
OnValueTwoChanged(new PropertyChangedEventArgs("MyProp"));
}
Here is the example project with more details, where I create user control with String property ValueTwo
which split in two different TextBoxes using some custom logic, and combine value and update when user change the text.
test_bind_fixed.zip
And this is article which help me
http://support.microsoft.com/kb/327413
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.
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?