Show values from datagridView in combobox - c#

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.

Related

Set Combobox selected item based on its ItemsValue

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)
{
}

How to bind ComboBox value to the TextBox value

I want to bind the ComboBox value upto the TextBox value. Here is an example:
Let's say I have one TextBox and one ComboBox
Now in TextBox there is value of 5. So as per the TextBox value is 5 my ComboBox would be bind upto 5 and in the list of ComboBox it would be saw the number upto 5 i.e. 1,2,3,4,5.
Same if the TextBox contain the value 3 then as per the change of TextBox value on the spot ComboBox should be bind upto the TextBox value.
I am working on it also but there is some list error.
Here is my code:
List<string> hafta = new List<string>();
hafta.Add(txt_hafta.Text);
for (int i = 0; i <= hafta.Count; i++)
{
cmb_hafta.BindingContext = this.BindingContext;
cmb_hafta.DataSource = hafta[i];
cmb_hafta.DisplayMember = i.ToString();
}
I am not sure that this code is perfect.
On TextBox text changed:
int count = 0;
Int32.TryParse(txt_hafta.Text, out count);
List<int> dataSource = new List<int>();
for (int i = 1; i <= count; i++)
{
dataSource.Add(i);
}
hafta.DataSource = dataSource;
hafta.DropDownStyle = ComboBoxStyle.DropDownList
If you talk about binding, then you can bind ComboBox.DataSource property to TextBox.Text property with custom eventhandler for Binding.Format event, where you can "convert" string to collection of numbers.
Put code below in the constructor of your form.
var binding =
new Binding("DataSource", txt_hafta, "Text", true, DataSourceUpdateMode.Never);
binding.Format += (sender, args) =>
{
int.TryParse(args.Value.ToString(), out int maxNumber);
args.Value = Enumerable.Range(1, maxNumber).ToList();
};
cmb_hafta.DataBindings.Add(binding);
You can do something like this:
List<string> hafta = new List<string>();
int total = Int32.Parse(txt_hafta.Text);
for (int i = 0; i <= total ; i++)
{
hafta.Add(i.ToString());
}
cmb_hafta.DataSource = hafta;

MAKE drop down ready only if value is selected

i am new in programming
i have 6 drop down in one page 1 want them to make them read only (non editable)
if value is selected
and rest of the drop down should not be read only if value is selected
is there any way i can check all the Drop down in one condition/check(at one go)
thanks in advance
1# Find all dropdownlists
Dim allDDs = From dd As DropDownList In Me.Controls() Where TypeOf (dd) Is
DropDownList Select dd
2# disbale the where value selected and enable others
For Each d In allDDs
d.Enabled = d.SelectedValue = ""
Next
say ddl1, ddl2, ddl3, ddl4, ddl5, ddl6 are ids of 6 dropdowns in your page.
for (int count = 1; count <= 6; count++)
{
DropDownList ddl = new DropDownList();
ddl.ID = "ddl" + count;
if (ddl.SelectedIndex > -1)
{
ddl.Enabled = false;
}
else
{
ddl.Enabled = true;
}
}

Setting the SelectedIndex on ComboBox

I am in a beginning C# class and I am having trouble figuring out why after the following code runs, the selected index is still -1 (i.e. the combobox at load is empty). It should be defaulting to selectedIndex = 1:
public string[,] GetArray()
{
//create array with conversion values
string[,] conversionInfo = { {"Miles","Kilometers", "1.6093"},
{"Kilometers","Miles", ".6214"},
{"Feet","Meters", ".3048"},
{"Meters","Feet","3.2808"},
{"Inches","Centimeters", "2.54"},
{"Centimeters","Inches",".3937"}};
return conversionInfo;
}
private void Form_Load(object sender, EventArgs e)
{
//get array to use
string[,] conversionChoices = GetArray();
//load conversion combo box with values
StringBuilder fillString = new StringBuilder();
for (int i = 0; i < conversionChoices.GetLength(0); i++)
{
for (int j = 0; j < conversionChoices.GetLength(1) - 1; j++)
{
fillString.Append(conversionChoices[i, j]);
if (j == 0)
{
fillString.Append(" to ");
}
}
cboConversion.Items.Add(fillString);
fillString.Clear();
}
//set default selected value for combobox
cboConversion.SelectedIndex = 0;
}
public void cboConversions_SelectedIndexChanged(object sender, EventArgs e)
{
LabelSet(cboConversion.SelectedIndex);
}
public void LabelSet(int selection)
{
//get array to use
string[,] labelChoices = GetArray();
//set labels to coorespond with selection
string from = labelChoices[selection, 0];
string to = labelChoices[selection, 1];
lblFrom.Text = from + ":";
lblTo.Text = to + ":";
}
This is a class assignment so I am not allowed to set anything using the designer, other than to link methods to event. Everything works correctly except for the default for the combobox.
Your code is completely correct, but you have one single fault in your mind. Consider, that you are loading the items into the ComboBox after you have initialized the control. At this point the control have no items, therefore the property "Text" is not setted automatically.
You have to differ between the SelectedIndex, which is the index of the item in the list of items, and the Text, which is the text, shown in the ComboBox. So think about when and how you should set the Text-property of the ComboBox.
Always set the Text-property to the first value of your item-list, after you changed it.
Greetings,
the answer of Mario can be interpreted also as:
put your code after the loading (example: on the shown event), in this way the control is initialized and has items.

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