Databinding issue in C# - c#

I have an issue with DataBindings in C#.
I have a BindingSource with it's DataSource set to a DataRowView
I then use the binding source to set all the databinding for my controls.
Here is my code:
bsDataRecord.DataSource = myDataRow; //bsDataRecord is a BindingSource and myDataRow is a DataRowView
//Add Databinding to my controls
dateNextDate.DataBindings.Add("Value", bsDataRecord, "Next_Date", false, DataSourceUpdateMode.OnPropertyChanged); //DateTimePicker
textInformation.DataBindings.Add("Text", bsDataRecord, "Information", false); //TextBox
//more controls, etc
My databindings all work fine. So as I select the control and enter a value, myDataRow is updated.
My problems occurs when I try to set a control's value in code e.g.
textInformation.Text="Test";
When I do this myDataRow isn't updated. The only way I can get myDataRow to update is to give the control that I've updated focus.
Hope that makes sense!? Anybody got any ideas?
Thanks in advance.
I'm using c#.Net 4.0.

After problematically setting a property of the data source, you need to get the controls to reread their values from the datasource. Often a simple call to
dataBindingSource.ResetBindings(false); // false for value change not schema change
But you could also per control use this:
foreach (Binding b in myControl.DataBindings) b.ReadValue();
Binding.ReadValue() causes the control's bound property to be set to the data source value
Hope this helps!

If you update a (non-two-way)binding's target "by hand", that breaks the binding and it will not work anymore. This is normal behaviour.
So make your binding two-way to achieve what you want to do.

Related

How do I connect a binding to a (Winform) ComboBox control to get/set the selection in the control from the binding source?

I have plumbed the deeps of my google-fu to find an answer.
First, I am not simply asking how to bind a combobox to a datasource. It's a bit more than that unless I'm having a serious understanding gap.
On my Winform, I have a DataGridview on the left and on the right I have a panel with values from the selected row on the left. One of those controls on the right is the ComboBox I'm having trouble with. I have my bindingsource and dataview set up and the other controls on the right are working splendidly, except the ComboBox control.
The user, interacting with this ComboBox, should see values such as "Item ABC" and "Item EFG" and the value related to them might be 1234 and 5678. If this was a fully unbound control I'd put an object array of items in. Once I get it working, I'd load that from a different source.
But when I try to DataBindings.Add("??", dataview, "dataviewfield", ...) I can't get the proper value for "??". Runtime debug shows that "SelectedItem.Value" would be the right option, but I get "not found" type exception when I use that. I've tried "SelectedValue" as well, but that didn't work (debug show's it's null & throws no nulls allowed exception).
How can I get that value placed directly into the DataView via the Binding?
Setting the .DataSource simply loses the items and doesn't help at all.
How does one do this? Short of making the ComboBox unbound totally, setting the selectedindex directly and capturing the value when the selected index changes changes - Just seems so clunky to have to do that.
-old programmer
Further Notes: I edited to clarify the placement of the ComboBox.
I have made progress (naturally, only after asking a question does a new avenue pop into my head). I got to thinking I might need a custom binding adapter so started googling that. I found some samples doing what I want.
The foremost problem was I was not using assigning a datasource on the ComboBox, I was simply adding items. When I created a two column dataset and a few rows (could have been anything I suppose) and set that and the two field names as the ComboBox's displaymember and valuemember did the SelectedValue start showing a value (instead of null all the time).
I think that was the problem. The remaining issue is getting the left hand side to re-display/refresh after the change.
When loading the datasource for the DataGridView and the bindingsource, try making them share the same list:
BindingSource1.Datasource = dataset
DataGridView.Datasource = BindingSource1
This should mean that any changes to the data in the bindingsource will also update the DataGridView.
To edit the selected object then just handle the SelectionChanged event and set the BindingSource1.Position to the index of the selected object in the Datasource.

in xtragrid double click on a cell and that value should display in lookupedit

I created one XtraGrid and added some values with the help of LookUpEdit and some TextBoxes.
I want to modify the values what I have added into grid here.
I am using getfocusedrowcellvalue to get values from grid into TextBoxes.
How can I get that getfocusedrowcellvalue to LookUpEdit?
example:-
txtdrmk.Text = Convert.ToString(gridView3.GetFocusedRowCellValue("remark"));//to get value from selected cell to text box.
cmbper // this is my lookupedit.
If you fire a FocusedRowChanged event, you can certainly do what you seek by using the GetFocusedRow() method against the grid view:
object o = grdCommentsView.GetFocusedRow();
From here, if your data source is a domain object, you can just cast it to that object type:
Customer c = o as Customer;
Or, if the datasource is a datatable:
DataRow dr = o as DataRow;
All that said, there is a much better way, in my opinion. Use a binding source component, bind your data (object collection or DataTable) to the binding source DataSource property, then make the binding source the data source for both the grid and any non-grid controls you have. You can access these via the (DataBindings) property.
The best part about this approach is that the data binding is codeless*, and as you change rows on the grid the values in the controls will automatically update. If you change the property value either place (control or grid), the other will reflect the update.
If you use the DataLayoutControl, it will even do the data binding for you.

C# Winforms - Form Control Databinding - How to find source element

Please bear with me if this question is stupid or has been asked before. I understand how to databind a form control to a datasource. I would like now to reverse and retrieve the data source element for a specific control.
Example for a textbox:
this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.table1BindingSource, "ContactID", true));
How can I retrieve the result "ContactID" for textBox1?
If you have only one Binding added to the DataBindings of Textbox then you can get the datamember value of the binding as following.
var binding = textBox1.DataBindings[0];
var member = binding.BindingMemberInfo.BindingMember;
MessageBox.Show(member);
This should get you the value which are looking for.

Binding WebBrowser.DocumentText to a BindingSource won't update

So I have a BindingSource with it's DataSource property set to a DataSet. I then have several controls that are bound to different columns of tables in the DataSet.
bsStatus.DataSource = statusDS;
dgvStatus.DataBindings.Add("DataSource", bsStatus, "Status");
lblBRId.DataBindings.Add("Text", bsStatus, "Status.BorrowerAccount");
lblCBId.DataBindings.Add("Text", bsStatus, "Status.CoBorrowerAccount");
// MORE CONTROL BINDINGS
webBrowser1.DataBindings.Add("DocumentText", bsStatus, "Status.ScriptLog");
This all works just fine on loading. My controls are populated with the correct values. The problem comes when updating the 'statusDS' DataSet. Most controls are just fine. The TextBox and DataGridView controls update just fine when the source DataSet is changed.
The problem is that the WebBrowser.DocumentText property isn't updated.
I realized I had set
WebBrowser.AllowNavigation = false
Setting it back to true solved the issue.

C# Datagridview: get selected item in combobox columns

I'm working on a GUI that allows the user to manipulate xml files. I display the xml file in a datagridview organized neatly by columns through xml elements. I allow the user to add columns as an extention on my project. The column gets added to the dataset table, then updated to the datagridveiew that I use to display the xml file in. I've included the ability for the user to add a combobox column to select choices instead of entering them in constantly like.. true or false. However, that is where the problem lies. Saving a normal column was easy. The combobox column is being a pain.
I have a "save combobox column" to have it updated to the xml and a "save" button to save in a destination of the user's choice.
I've done some research and it seems like the combobox class has such a feature to gain access to the selecteditem in the combobox put in by the user.
Where we have:
ComboBox box = new ComboBox();
box.SelectedItem;
I tried applying this to the combobox column class but it does not have such a function. Thus, I cannot figure out how to directly obtain the value of the user's selected item. I tried experimenting with comboboxcell's as well, but that didn't lead me anywhere either. Both those classes I played around with do not have a... "selected item" function and even google does not have a solution for me. =( I've also tried using the cell.value, but it is "null" for some reason. Even when the user selects an item in the box, it doesn't get saved into the cell's value.
TLDR:
My question in short is, how, if possible, do you gain access to the comboboxcolumn cell's selected item? Additionally, how would you then ensure that the item value is saved in the cell?
Thanks in advance. I'm using .NET 3.5 SP1, through Visual Studio 2008 C#.
Sincerely,
tf.rz
The Control in a DataGridView is not a ComboBox, it is a DataGridViewComboBox and has different properties and methods. From MSDN
Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.
However, you mentioned that the Cell.Value is null for you. Well there may be another step you are missing according to the following article (How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List).
You must set the DataGridViewComboBoxColumn.ValueMember or DataGridViewComboBoxCell.ValueMember property to the name of a property on your business object. When the user makes a selection, the indicated property of the business object sets the cell Value property.
If we have bound a datagridcomboboxcell with a different DisplayMember and ValueMember, like so:
dgcombocell.DisplayMember = "Name";
dgcombocell.ValueMember = "Id";
dgcombocell.DataSource = dataset1.Tables[0];
Then for getting SelectedText, and SelectedValue, we can write this code:
string SelectedText = Convert.ToString((DataGridView1.Rows[0].Cells["dgcombocell"] as DataGridViewComboBoxCell).FormattedValue.ToString());
int SelectedVal = Convert.ToInt32(DataGridView1.Rows[0].Cells["dgcombocell"].Value);
I hope it solves your problem.
Use this to get or set selected value:
object selectedValue = currentRow.Cells["comboboxColumnName"].Value
Don't forget to set DisplayMember and ValueMember for your DataGridViewComboBoxColumn
This is how it is done
DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)dgv.Rows[0].Cells[1];
MessageBox.Show(""+comboCell.Items.IndexOf(comboCell.Value));
A .Net combox is actually a composite control made up of a textbox and a dropdownlist. Use box.Text to get the currently displayed information.
EDIT: The row or the cell should have a .FindControl() method. You'll need to do something like:
Combobox box = (Combobox)(row.FindControl("[combobox ID]"));
string val = box.Text;
Basically, you're finding the control within its container (row or cell), then casting the control found as a combobox, then accessing its .Text property.
I use this:
private int GetDataGridViewComboBoxCellSelectedIndex(DataGridViewCell d)
{
return ((DataGridViewComboBoxCell)d).Items.IndexOf(d.Value);
}

Categories

Resources