Set Combobox selected item based on its ItemsValue - c#

I have a Combobox, in which let's say that an item's Display Text is "School" and it's Item Value is 19. So i have stored this 19 into a DataGrid.
Then, i retrieve Combobox Value from DataGrid, then what i want to do is simply that based on value retrieved from DataGrid, combobox should set it's display Item or SelectedItem which have Value 19. In above scenario Combobox should display its selected item "School" if its value was 19.
So far i have wrote code upto this point. But it always giving me First Item of a Combobx.
DataGrid gd = (DataGrid)sender;
DataRowView rowSelected = gd.SelectedItem as DataRowView;
if(rowSelected!=null)
{
for (int i = 0; i < comboBox1.Items.Count;i++ )
{
if (Convert.ToString(comboBox1.SelectedValue) == Convert.ToString(rowSelected[14]))
{
index = comboBox1.Items.IndexOf(comboBox1.SelectedValue);
}
comboBox1.SelectedItem= comboBox1.Items[index];
}
textBox9.Text=rowSelected[14].ToString();
}

Now i am able to retrieve the Combobox Item, Based on its value which i am retrieving from the WPF DataGrid.
for (int i = 0;i <comboBox1.Items.Count; i++)
{
comboBox1.SelectedIndex = i;
if ((string)(comboBox1.SelectedValue) == Convert.ToString(rowSelected[14]))
{
index = i;
}
}
comboBox1.SelectedIndex = index;

Change your code to
if(rowSelected!=null)
{
int index = comboBox1.Items.IndexOf(rowSelected[14]);
comboBox1.SelectedItem = comboBox1.Items[index];
}
or
Use FindStringExact() method of combobox
int i = comboBox1.FindStringExact("Combo");
if(i >= 0)
{
}

Related

Wpf ComboBoxColumn selected items to string list

question : how i can get a List<string> of all selected items in ComboBoxColumnso i can change the old values on my DB
im new into wpf and overflow so forgive me if i made any mistake
Assuming that 0 is the index of the ComboBoxColumn.
int j = DataGD.Items.Count;
int i = 0;
do
{
DataGridRow row = this.DataGD.ItemContainerGenerator.ContainerFromIndex(i) as DataGridRow;
ComboBox ele = this.DataGD.Columns[0].GetCellContent(row) as ComboBox;
string newS = ele.Text;
list.add(newS);
i++;
} while (i < j);

listbox selectedindex only selecting last element

I have a listbox that is bound to a list of objects from the database. I have a secondary list that has less objects that I want to use it to mark as selected elements.
cell = new HtmlTableCell();
List<ClasaAutor> listaAutori = DataTableToClasaAutor(dal.CitesteTotiAutori());
List<ClasaAutor> listaAutoriPublicatie = DataTableToClasaAutor(dal.CitesteTotiAutoriUneiPublicatii(guidPublicatie));
ListBox list = new ListBox();
list.SelectionMode = ListSelectionMode.Multiple;
list.ID = "cbAutori";
list.DataSource = listaAutori;
list.DataTextField = "NumeComplet";
list.DataValueField = "GuidAutor";
list.DataBind();
foreach (ClasaAutor autor in listaAutoriPublicatie)
{
for (int i = 0; i < list.Items.Count; i++)
{
if (list.Items[i].Value == autor.GuidAutor.ToString())
list.SelectedIndex = i;
}
}
cell.Controls.Add(list);
row.Cells.Add(cell);
The problem is that only my last element gets selected... why? How can I fix it?
My if is ok, it gets true 2 times...
Try this loop:
foreach (ClasaAutor autor in listaAutoriPublicatie)
{
foreach (ListItem item in list.Items)
{
if (item.Value == autor.GuidAutor.ToString())
item.Selected = true;
}
}
the problem in semantics, SelectedIndex of list can hold only one value, this is not collection
however you could make list item selected by setting the Selected value of it to true
list.Items[i].Selected = list.Items[i].Value == autor.GuidAutor.ToString();

Show values from datagridView in combobox

I have for example this values in datagridView : 1;2;3;4;
I want display this values in combobox like this : 1
2
3
4
My code show only last value in combobox: 4
My code of showing :
string cmbValue = CmbText;
string[] cmb = cmbValue.Split(new[] { ';' },StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < cmb.Length; i++)
{
comboBox1.Text = cmb[i];
}
and here is my code of creating array of combobox and calling method to change cell in dataGrid:
string cmbText = comboBox1.Text;
string[] cmb = new string[] { cmbText};
frm1.ChangeCellCmb(2, cmb);
this.Dispose();
Someone know how to do this ? I cant simply set collection of values combobox, because values of combobox are reading from datagrid and it's reading from DB.
Many thanks.
You need to loop and add the required items as follows:
for (int i = 0; i < cmb.Length; i++)
comboBox1.Items.Add(cmb[i]);
This will add all the required items to the drop down menu. To select/display '4' the 3rd entry in cmb by default do
comboBox1.SelectedIndex = 3;
or
comboBox1.SelectedItem = "4";
I hope this helps.

Loop thru the combobox and compare with the data in the database

i had try several method to loop through the combobox, but it won't work...
for (Int16 i = 0; cbxrecipe.Items.Count - 2 >= i; i++)
{
if (cbxrecipe(i).Items.ToString() != Reader_recipe1.GetValue(0).ToString())`
{
//update the combobox;
}
}
means i need to loop through the combobox, to check whether the items inside the combobox is same with the data in the database, if same, it will not update, otherwise, the combobox will appear new item immediately...
thanks for the help =)
you have an error in your code.
try this :
for (Int16 i = 0; cbxrecipe.Items.Count - 2 >= i; i++)
{
if (cbxrecipe.Items[i].ToString() != Reader_recipe1.GetValue(0).ToString())
{
//update the combobox;
}
}

Datagridivew DataGridViewComboBoxColumn select value member

i have datagridview which has one combobox column. i populate combobox column. when i select any text from combobox column then i need to get the value when i read the data in for loop.
dgFilter is here datagridview
DataGridViewComboBoxColumn dgcoSpec = new DataGridViewComboBoxColumn();
dgcoSpec = new DataGridViewComboBoxColumn();
dgcoSpec.DataSource = loadOperators();
dgcoSpec.DisplayMember = "Operatortxt";
dgcoSpec.ValueMember = "Operatorvalue";
dgcoSpec.AutoComplete = true;
dgcoSpec.HeaderText = "Operators";
dgcoSpec.Name = "Operators";
dgcoSpec.DefaultCellStyle.NullValue = "--Select--";
dgcoSpec.Width = 130;
dgFilter.Columns.Insert(1, dgcoSpec);
here this way i read data from combobox column
for (int i = 0; i <= dgFilter.Rows.Count - 1; i++)
{
strOperator = dgFilter.Rows[i].Cells[1].Value.ToString().Trim();
}
but the problem is i am not getting the combox value member rather i am getting display member.
so how to extract value member from for loop. please guide me with code. thanks
The value member is the string that is displayed inside the DataGridViewComboboxCell.
The actual Combobox control only exists during the time frame in which the user is editing the cell.
If you mean that you would like to get the index of the value in the list of the DataGridViewComboboxCell items, you can query the index of the value:
for (int i = 0; i <= dgFilter.Rows.Count - 1; i++)
{
var cell = dgFilter.Rows[i].Cells[1] as DataGridViewComboboxCell;
int index = cell == null || cell.Value == null ? -1 : cell.Items.IndexOf(cell.Value);
Console.WriteLine(index);
}
In this example, I am using -1 as an invalid value.
EDIT Just noticed you are using a DataSource;
See DataGridViewComboBoxColumn name/value how? for possible duplicate.

Categories

Resources