I have simple comboBox with:
cb_listaUczniow.ValueMember = "Key";
cb_listaUczniow.DisplayMember = "Value";
And I have constructor for this Form (classID is not important yet):
MyForm(int classID, string selectedName)
{
cb_listaUczniow.ValueMember = "Key";
cb_listaUczniow.DisplayMember = "Value";
comboBox.DataSource = new BindingSource(makeList(classID), null);
}
makeList return Dictionary
and How i can select in comboBox item with "Value" (displayMember) where names selectedName?
for example (pseudo-Code):
MyForm(3, "Gall Anonim") -> comboBox.Item.Selected = comboBox.Item.where("Value" == "Gall Anonim");
How i can set it?
If I understand this correctly, you can simply set ComboBox's SelectedValue property to the corresponding value :
comboBox.SelectedValue = 3;
That will make "Gall Anonim" the selected item of the ComboBox.
Related
I have a class which is creating a Combobox with a Databinding to an Object.
The Object has a Value for an enum. But when the ComboBox is loaded it doesnt Contain a Value. The Following is the part where i´m creating the ComboBox.
ComboBox combBox = new ComboBox();
combBox.DropDownStyle = ComboBoxStyle.DropDownList;
combBox.BackColor = Color.White;
combBox.DisplayMember = "Anzeige";
combBox.ValueMember = "Value";
var values = Enum.GetValues(EnumType);
foreach(int value in values)
{
ComboBoxItemClass comboBoxItemClass = new ComboBoxItemClass() { Value = value, Anzeige = Enum.GetName(EnumType, value) };
combBox.Items.Add(comboBoxItemClass);
}
combBox.DataBindings.Add(nameof(combBox.SelectedValue), NAFDetailView.CurrentObject, PropertyName, true);
combBox.SelectedText = "Anzeige";
i am using this code for displaying text on load.
I solved the Problem by getting rid of the ComboBoxItemClass, and assigning the DataSource of the ComboBox, after that i used the SelectedItem Property for the DataBinding as follows.
ComboBox combBox = new ComboBox();
combBox.DropDownStyle = ComboBoxStyle.DropDownList;
combBox.DataSource = Enum.GetValues(EnumType);
combBox.DataBindings.Add(nameof(combBox.SelectedItem), NAFDetailView.CurrentObject, PropertyName, true);
I have a ListView populated with the code below. Ask you can see, I set both the DisplayMember and the ValueMember. What I am wanting to do is find a ListViewItem by its ValueMember. So essentially what I'm looking for is ListViewItem.Value. I know I can get SelectedValue for the ListView itself, but I just don't see any properties on the ListViewItem that give me what I'm looking for. Am I just missing something, or is there no way to do this?
private void PopulateList(Globals.DataFieldMappingTypes mappingType)
{
ListBox lst = GetListBox(mappingType);
ComboBox cbo = cboh.GetComboBox(new ComboBoxHandler.CboInfo(Globals.NodeTypes.DataField, mappingType));
string sql = "select DataFieldReferenceValueId, [Value] from DataFieldReferenceValueInfo where DataFieldId = " + cbo.SelectedValue.ToString();
DataTable tbl = dal.GetTable(sql, "DataFieldReferenceValue");
lst.DisplayMember = "Value";
lst.ValueMember = "DataFieldReferenceValueId";
lst.DataSource = tbl.DefaultView;
}
I think that you are not getting the values from DataTable correctly I guess.
I hope tbl.Rows[0][0].ToString() will have the DataFieldReferenceValueId and
tbl.Rows[0][1].ToString() will have the [Value]
Please check the below MSDN link
https://forums.asp.net/t/1188002.aspx?how+to+read+DataTable+Rows+value+and+put+it+into+the+string+
Instead of loading your dataTable straight in as the dataSource why don't you create a class to define the values?
public class SqlTable
{
public string Name {get;set;}
public string Value {get;set;}
}
var listPair = new List<SqlTable>();
Then load the list with your SqlDataReader and grab your desired pair with LINQ.
while (sdr.Read())
{
listPair.Add(new SqlTable() { Name = sdr[0].ToString(), Value = sdr[1].ToString() });
}
lst.DisplayMember = "Name";
lst.ValueMember = "Value";
lst.DataSoure = listPair;
SqlTable sqlTable = listPair.Find(x => x.Name == "Whatever name you are searching for") as SqlTable;
The SqlTable item is now the one you are searching for and you can get its properties by going:
string value = sqlTable.Value;
string name = sqlTable.Name;
Edit from comment begins here:
Well your first issue is that the 'lst' item in your example is a ListBox and not a ListView. It you switch it to a listview you can still feed your List of listPair items like so:
ListView lst = new ListView();
lst.View = View.Details;
foreach (var data in listPair)
{
lst.Items.Add(new ListViewItem(listPair.Name, listPair.Value);
}
So now you have a ListView that is a collection of your listPairs where each listPair is a ListViewItem. I assume you want to isolate the ListViewItem based on your value (or listPair.Value or sdr[1] or [Value] they are all the same now) in order to color it. You can now grab the listPair item like so:
SqlTable pair = listPair.Find(x => x.Value == "Whatever the Value value is that you want to color");
ListViewItem searchedItem = lst.FindItemWithText(pair.Name);
searchedItem.BackColor = System.Drawing.Color.Red; //or whatever color you choose
You can now use searchedItem to grab its index in the ListView and all its other properties. The ListPair just allows you to associate the values.
I believe this question is kinda new-bie, but I can't solve it in correct way.
Brief description:
I have an inherited from ComboBox class that does some data bindings in constructor:
var mdl = new Model();
ValueMember = "id";
DisplayMember = "unit";
DataSource = mdl.getUnits();
All good here. The combobox is filled by required data.
Then I have another form with a function editIngridient. The function is following;
public bool editIngridient(int id)
{
currentId = id;
var row = mdl.getIngridient(id);
txtIngridient.Text = (string)row["ingridient"];
cmbUnit.ID = (int)row["unitId"];
numNotifyQty.Value = (int) row["notifyQty"];
ShowDialog();
return true;
}
Now, when the form popups, textbox and number box filled by needed values, while combobox is filled by first value.
If I will run the combobox data bind function as the first line inside editIngridient function - all works good.
Please point me to my stupidity.
Thanks a lot!
YOu didnt say what is your dataSource, but I assume thats DataTable, so you can do it:
DataRowView rowData = comboBox1.SelectedItem as DataRowView;
int id = Convert.ToInt32(rowData["id"]);
string name = rowData["unit"].ToString();
Is it possible to set the selectedIndex of a combobox based on its value, without having to iterate through the datasource?
I set the datasource like this:
caseDBTableAdapters.usersTableAdapter usersAdapter = new caseDBTableAdapters.usersTableAdapter();
caseDB.usersDataTable users;
users = usersAdapter.GetUsers();
cbOwner.DisplayMember = "fullName";
cbOwner.ValueMember = "userId";
cbOwner.DataSource = users;
It seems less elegant to have to iterate through the table fx. by doing:
int counter = 0;
foreach (caseDB.usersRow usersRow in users)
{
if (usersRow.userId == selectedUser)
{
cbOwner.SelectedIndex = counter;
}
counter++;
}
Try assing
comboBox.SelectedValue = "value";
or
comboBox.SelectedItem = item;
After question edit:
Set SelectedValue to selectedUser:
cbOwner.SelectedValue = selectedUser;
When you use the DisplayMember and ValueMember properties, set SelectedValue.
Otherwise, set SelectedItem.
How to get value from datagridview combobox after selected value is changed?
You can use:
var value = DataGridView.Rows[0].Cells[0].Value
NOTE: You would supply the correct row and cell number.
Or you can do something like this if it is bound to an object like ListItem
string value = DataGridView.Rows[RowIndex].Cells[ColumnIndex].Value.ToString();
if DataGridView.Rows[RowIndex].Cells[ColumnIndex] is DataGridViewComboBoxCell && !string.IsNullOrEmpty(value))
{
List<ListItem> items = ((DataGridViewComboBoxCellDataGridView.Rows[RowIndex].Cells[e.ColumnIndex]).Items.Cast<ListItem>().ToList();
ListItem item = items.Find(i => i.Value.Equals(value));
}
Do not pass to give column name
string SelectedText = Convert.ToString((Pass_dataGridView.Rows[0].Cells["Partner"] as DataGridViewComboBoxCell).FormattedValue.ToString());