Error on Combo box as User Control element - c#

I'm creating a user control with a Combo box, Text box and a Button. When I place this User Control in a form following error occurred. "Cannot bind to the new value member. Parameter name: newDisplayMember" I searched the SO and couldn't find any satisfying answer (May be I'm wrong). This is the code snippet to load data to combo:
// binding data source.
_bsFieldList.DataSource = _dtFieldList;// _dtFieldList is a DataTable
// Assigning binding source as the data source of the combo box.
cboFields.DataSource = _bsFieldList;
cboFields.DisplayMember = "FieldCaptn";
cboFields.ValueMember = "FieldType";
What Am I doing wrong?
I found that using public properties for DisplayMember and ValueMember solved this issue. But in my case this is not possible.
EDIT:
These are the columns of _dtFieldList

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.

C# Trying to find an unbound binding source [duplicate]

I am creating a ComboBox array dynamically and the DataSource for all the ComboBox is a single integer list that contains some integers. But when I change a value say X in any one combo box then all other combo values get reset to value X.
So here is the situation:
All combo box controls are bound to a single list
When I change selected item of a combo box, selected item of all other combo box controls also change.
How can I stop these behavior?
Since you are binding all combo boxes to the same data source - a single list - they are using a single BindingManagerBase.
So when you choose an item from one of combo boxes, the current Position of the shared binding manager base changes and all combo boxes goes to that position of their shared data source.
To solve the problem you can bind them to different data source:
You can bind them to yourList.ToList() or any other list for example different BindingList<T>.
combo1.DataSource = yourList.ToList();
combo2.DataSource = yourList.ToList();
You can use different BindingSource for them and set your list as DataSource of BindingSource
combo1.DataSource = new BindingSource { DataSource= yourList};
combo2.DataSource = new BindingSource { DataSource= yourList};
Also as another option:
You can use different BindingContext for your combo boxes. This way even when you bind them to a single list, they are not sync anymore.
combo1.BindingContext = new BindingContext();
combo1.DataSource = yourList;
combo2.BindingContext = new BindingContext();
combo2.DataSource = yourList;
In fact all controls of the form use a shared BindingContext. When you bind 2 controls to a same data source, then they also use the same BindingManagerBase this way, when you for example move to next record, all controls move to next record an show value from bound property of next record. This is the same behavior that you are seeing from your combo boxes. Being sync for controls which are using the same BindingManagerBase is a desired behavior. Anyway sometimes we don't need such behavior. The post shares the reason and the solution.

SelectedItem Property is not working

I am using a linq to sql for populating a combo box. to populate combobox i am using the following code on page load event:
ColdStoreDataContext csdc = new ColdStoreDataContext();
comboBox1.DisplayMember="Name";
comboBox1.ValueMember="AccountHeadId";
comboBox1.DataSourse=csdc.SupplierPurchase;
the above code is working properly but when I use the given code:
comboBox.SelectedValue="KAMAL SINGH S/O AJEET SINGH";
then it does not properly works means comboBox displayed null value.
Please help me to resolve this problem.
Your combo box has AccountHeadId as its ValueMember. When you set the combo box's SelectedValue, the box will look in its data source for an item whose AccountHeadId matches the value you just set to SelectedValue.
Try
comboBox1.SelectedValue=2;

Windows forms listbox.selecteditem displaying "System.Data.DataRowView" instead of actual value

I am creating a Windows form application in Visual Studio 2010. I have an Access database which has a table 'ResearcherInfo'.
In my windows form, I have a listbox which gets populated with the contents of the column name 'Researcher Name' of this table.
However when I try to get the selected items of this text box, it only returns me a null value or sometimes System.Data.DataRowView depending on how I access it
I have tried
string name = projectmembers_lst.SelectedItem.ToString();
While debugging when I hover the mouse over the 'projectmembers_lst' word, it correctly shows me projectmembers_lst = {SelectedItem = "Jerome"}
but on hovering over the string 'name', it shows "name = "System.Data.DataRowView""
i even tried copying to an array
foreach (Object selecteditem in projectmembers_lst.SelectedItems)
{
object[] objCollection = new object[projectmembers_lst.SelectedItems.Count];
projectmembers_lst.SelectedItems.CopyTo(objCollection, 0);
MessageBox.Show(objCollection[0] as string);
}
Nothing gets me the selected items. I am not sure what is wrong here.
Populating the List Box:
1) I have the added the access table 'ResearcherInfo'. as a data source for the web form application.
2) In the properties of the Listbox, under the Datasource tab, I have linked this table and given the display member and value member property of the Listbox as the column name 'Researcher Name' of the table.
So basically my Listbox displays the Researcher Names from the Researcher info table. attached is a snap shot.
without seeing how you're populating it, it's tough to guess.
try ((objCollection[0] as DataViewRow)[0] as string

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