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

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.

Related

SyncFusion WPF ComboBox in Grid- How to set display text on OnCommitCellInfo event

I have a SyncFusion ComboBox dynamically added in SynckFusion:GridControl with following code:
SchoolGrid.Model[rowIndex, columnIndex].CellType = "ComboBox";
SchoolGrid.Model[rowIndex, columnIndex].ItemsSource = itemSource;
SchoolGrid.Model[rowIndex, columnIndex].DisplayMember = "FullDistrictName";
SchoolGrid.Model[rowIndex, columnIndex].ValueMember = "FullDistrictName";
SchoolGrid.Model[rowIndex, columnIndex].CellValue = cellValue;
SchoolGrid.Model[rowIndex, columnIndex].DropDownStyle = GridDropDownStyle.Exclusive;
What I want to achieve is:
1)Items in combobox I want to show in "Gujarat/Surat" format. when
user select any item, the value that I want to be shown is only
"Surat", not "Gujarat/Surat". 2) When user open dropdown list, the
selected item should have focus.
In QueryCellInfo event, I've specified value for this column as "District"- property of my model.
In CommitCellInfo event, I am fetching and assigning the values to model properties. So point 1) is working as required. But I am not able to make point 2) working. I've tried using OnCurrentCellShowingDropDown, GotFocus events, but no luck.
How can I make it working?
We have prepared the sample with your code snippet and checked the reported issue of “Focusing the selected item of ComboBox ”, but we were unable to reproduce the issue. Please find the sample link below:
Sample: GridControl
If the issue still reproduces at your end, please modify the above sample to reproduce the issue and update us with the replication procedure. So that we will be able to analyze the issue better and update you with better solution.

Get items from DataTemplate for ArtOfTest controls

I have this kind of object in code:
ArtOfTest.WebAii.Controls.Xaml.Wpf.ListBox
Items of it are in ItemTemplate. I have collection of it:
var listBoxItems = repairComapanyHintsList.Find.AllByType<ListBoxItem>();
and I want to get control from that DataTemplate. How can I do it? That solution doesn't work for DataTemplate (for other situations it's ok):
var textBlock = listBoxItem.Find.ByName("Name");
How can I get it? I tried this solution too, but that controls (from ArtOfTest) doesn't DependencyObject:
How can I find WPF controls by name or type?
I want to select one element depend of that TextBox text value.
I solved it, but it is not very good solution. It works for me and maybe it will help somebody.
I do it like that:
var listBoxItem = repairCompanyList.Find.AllByType<ListBoxItem>().FirstOrDefault(r => r.Text == "Name");
Assert.IsNotNull(listBoxItem, "Lack of expected list box item");
listBoxItem.User.Click();
I checked

How to display selective items in a binding list in grid control

I have implemented below code:
gridControl.DataSource = CusColumnList
CusColumnList is of type MyBindingList which inherits BindingList, in my case T is class MyColumn. The binding works well.
But now my problem comes, I don't want the data source to bind to every column in CusColumnList, I only want it binds to column whose name contains "ABC" or whose display name contains "XYZ". I tried to set
gridControl.DataSource = CusColumnList.Where(column => column.Name.Contains("ABC") || column.DisplayName.Contains("XYZ"));
But seems it does not work.
I also tried to create another bindinglist collection MyTempCusColumnList of type MyBindingList, and in the Get method of this MyTempCusColumnList, I just return every item in CusColumnList where the name or display name qualifies. But in this way, every time when CusColumnList is updated, I need to manually update MyTempCusColumnList.
I wonder whether there is a better way to archive this goal with just CusColumnList.
Thanks!
Edit : format code
You could use a filter string on a BindingSource object.
Check out the MSDN documentation on it, it's quite good: http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter(v=vs.100).aspx

Databinding issue in 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.

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