Save Value and text in Combobox - c#

hi i have a combobox in c# winform.
i have to save two things against each elemet. a Text which will be displayed to user and a id against that text.
in Asp.Net we can save these values in Text and Value fields of listbox..
but how to handle this situation in winform app

You can use a BindingSource to store data. And then bind it to your ComboBox. Remember to set ComboBox.ValueMember (points to ID field) and ComboBox.DisplayMember (points to Text field) properties.

comboBox1.SelectedValue;
comboBox1.SelectedText;
These two will work for you.
Happy coding.

Related

How to change the value of a Text Box depending on the currently selected row within a Data Grid?

Hi I'm hoping someone can help,
I have a Data Grid that has X amount of rows and I have a Text Box at the bottom of my form called 'Notes', I want the user to be able to select a row then enter a note within the Text Box and store this so when the user selects a different row the Text Box will be blank available for notes to be added for the new row. Then if the user selects a row that they have added notes to the Text Box will contain those notes.
I hope that makes sense, any ideas on how to approach this would be really appreciated.
Thanks,
Ryan
Thanks to GuidoG I have solved this, just in case anyone else has the same issue what I did was add a data binding to the grid then set the parameters. "Text" is just the property name, MyDataTable is the data source for my grid and "Notes" is the column name.
MyGrid.DataBindings.Add(new Binding("Text", MyDataTable, "Notes"));

How to get selected row values to combo box from data gridview in c#

There is problem with combo box values when I double-click on row of data grid view the values from the row passes to text boxes correctly but with combobox there is problem the value is not same in combobox as in data gridview row let me show you in picture.
Used code
For Textbox:
ar.txtcity.Text = this.dataGridView1.CurrentRow.Cells[5].Value.ToString();
For Combobox:
combobox ar.txtcombo.Text = this.dataGridView1.CurrentRow.Cells[6].Value.ToString();
Try this one.
ar.txtcombo.SelectedIndex = ar.txtcombo.FindString(
this.dataGridView1.CurrentRow.Cells[6].Value.ToString());
I think it will help :)
int class= int.Parse(row.Cells[6].Value.ToString());
NameOfYourComboBox.SelectedValue = class;
if the value is not in the database, you can use this;
NameOfYourComboBox.Text = row.Cells[6].Value.ToString();
I have tried this many times and it has worked fine for me with WinForms

Can I use a combobox in a programmatically bound datagrid C#

I am using entity framework and binding to a datagrid to display the results of my query. Instead of showing the foreign key from the result, I would like to show the more meaningful value from the lookup table and limit the users input by using a combobox too.
Is it possible to use a combobox in a programmatically bound datagrid on a winform? If so - how?
Thanks in advance,
James
There is a column type for a combobox in a DataGridView:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.aspx
For changing the display value you can use this event:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting.aspx

Making a WPF DataGrid look like a ListView

I have a project where there is an existing DataGrid. I have a DataSet properly bound to it and can populate columns with data for the field in the DataSet they represent.
Now I am in a situation where over time some of these descriptor fields are empty, so if I explicitly show columns and then populate the user ends up seeing a sparsely populated ugly DataGrid.
Is there some way that I can remove dividers/columns and merge descript fields. For example, if User A only has his/her email field filled out, User B only has the name field filled out and User C only had phone number field filled out I would like to display something that looks like a simple list:
userA#intertubez.com
Useri S. Bee
(555) 555-1234
Is this possible? Is this done in the XAML or the codebehind?
It is possible that you can solve it like this: bind your DataSet to the ListBox and work with a DataTemplate. :)
MSDN DataTemplate Overview
Or you could convert your rows to string and then you get a List and then bind that to the ListBox. I think the first solution is better.

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