How to Add Columns from DataTable to ComboBox in C# - c#

I have a ComboBox and a DataSet. I want to add each DataColumn to ComboBox as ComboBox Item.
I have tried this code:
DataColumn[] column_collection=new DataColumn[dataset.Tables[0].Columns.Count];
dataset.Tables[0].Columns.CopyTo(column_collection, 0);
combo_box.Items.AddRange(column_collection);
However, problem is that I just get a Empty List when I open ComboBox. That list has same number of items as there are columns, however there is no Value in it.

try something like this
var columns = dataset.Tables[0].Columns
.OfType<DataColumn>()
.Select(c => c.ColumnName);
combo_box.Items.AddRange(columns.ToArray());

Instead of:
combo_box.Items.AddRange(column_collection);
Write this:
for (int i = 0; i < column_collection.Length;i++)
{
combo_box.Items.Add(column_collection.GetValue(i).ToString());
}

Related

c# dynamic LINQ query

In my winform application, I am adding combobox column in datagridview and selecting the combobox item using the following code:
var entityModel= new AdminEntities();
DataGridViewComboBoxColumn cboIsNew = new DataGridViewComboBoxColumn();
var isNew = (from a in entityModel.TOWERs select a.ISNEW).Distinct().OrderBy(x => x);
cboIsNew.Items.AddRange(isNew.ToArray());
int i = dgvLoadTable.Columns["ISNEW"].Index;
dgvLoadTable.Columns.Insert(i, dgvCol);
dgvLoadTable.Columns[i].HeaderText = dgvLoadTable.Columns[i + 1].HeaderText;
dgvLoadTable.Columns[i + 1].Visible = false;
Is there a way to do this by a simple function in which I will pass the tablename and column name/index only? Also, If I do this:
dgvLoadTable.Columns.Insert(i, new DataGridViewComboBoxColumn());
Then how do I add item to this dynamically created combobox? I tried to add items like the following, but it doesn't work:
cboIsNew.Items.AddRange((from a in entityModel.TOWERs select a.ISNEW).Distinct().OrderBy(x => x))
Any help will be appreciated.
Try this:
cboIsNew.Items.AddRange((from a in entityModel.TOWERs select a.ISNEW)
.Distinct().Select( x => new DataGridViewComboBoxColumn() ).ToList());
You would need to see the DataGridViewComboBoxColumn with specific values using x as the source, but the way I've layed out should create a List of objects that should be able to be added.

Listbox Selected Item to array

I have a DataTable with 2 columns called "ID" and "Software" that I have used as a DataSource for a lst_Software multiselect listbox.
I'm trying to gather the ID for all the selected items in that have been selected and place that in an int[] array.
Listbox setup:
lst_Software.DataSource = software; //software is a DataTable
lst_Software.DisplayMember = "Software";
lst_Software.ValueMember = "ID";
I've tried below
List<int> list = new List<int>();
for (int i = 0; i < lst_Software.SelectedItems.Count; i++)
{
list.Add(Convert.ToInt32(lst_Software.SelectedValue.ToString()));
}
int[] software = list.ToArray();
I'm finding that I'm only getting the first selected value except it will not iterate through all... I know why though. I'm not using i to get passed through inside the for loop. I'm hoping someone can give me a direction to go to iterate through all the selected values.
Thank you
You're using lst_Software.SelectedValue.ToString() here in the loop so it only returns the one item. You have a for loop but you're not using the index variable. However, all of this is unnecessary really all you need is;
var items = lst_Software.SelectItems;
As that property is already the list of selected items. From there you can cast/convert them as you please.
SelectedValue is just the first selected value. You need to use SelectedItems.

Searching for an item in a dataset and showing it in a combo box

I have done combobox binding on form load.
I want to load ComboBox with numerous products, then based on a bar code i 'd like to select the corresponding product in the ComboBox.
I believe you were looking for this:
DataTable products = new DataTable();
products.Columns.Add("Product_Name");
products.Columns.Add("Product_BarCode");
products.Rows.Add("test1", 123456);
products.Rows.Add("test", 923456);
products.Rows.Add("test8", 823456);
products.Rows.Add("test", 723456);
products.Rows.Add("test0", 023456);
productname_tb.DataSource = products;
productname_tb.DisplayMember = "Product_Name";
productname_tb.ValueMember = "Product_BarCode";
// select the "test8" item by using it's Product_BarCode value of 823456
for (int i = 0; i < productname_tb.Items.Count; i++)
{
if (((System.Data.DataRowView)(productname_tb.Items[i])).Row.ItemArray[1].ToString() == "823456")
{
productname_tb.SelectedItem = productname_tb.Items[i];
break;
}
}
If I understand correctly you want to load your ComboBox with numerous products, then based on a bar code you'd like to select the corresponding product in the ComboBox. Try the following:
productname_tb.Items.IndexOf("<YOUR BARCODE>");
Does this work for you?

Datagridview_2 combobox value is not valid in C#

I have a datagrid with 2 columns: 1 is normal textbox type and the other column is combobox type.
My user interface has another datagrid_1 which contains a list of names. When a user clicks on the row of datagrid_1 with names. It puts the value selected by the user in the row of datagrid_2 in 1st column and then expects user to select one of the values in the other column (combobox).
I am not sure how to assign a datasource to this combobox. I have tried the following code but I am getting error "Datagridview_2 combox value is not valid."
var source = new BindingSource();
var phase_7 = (phaseeqType.return_Distinct_Phase()
.Select(b => b).AsEnumerable()).ToList();
string[] P_combo = new string[phase_7.Count()];
for (int i = 0; i < phase_7.Count(); i++)
{
P_combo[i] = phase_7.ToString();
}
source.DataSource = phase_7;
dataGridView1.CurrentRow.Cells[1].Value = source;
Can anyone pls help?
Cells don't have a DataSource property, so you would have try casting it to something that does:
Example:
((DataGridViewComboBoxCell)dataGridView1.CurrentRow.Cells[1])
.DataSource = source;

DevExpress GridControl sorted list

How to get a list of items, that is in sorted gridControl?
I need to create a new List with only values, sorted by GridControl.
probably not the most elegant solution but as found on the devexpress site because i hit the same problem a while a go:
the main gridview of the gridcontrol has a property DataRowCount, so you could do this;
List<DataRow> dataRows = new List<DataRow>();
for (int i = 0; i < gridView1.DataRowCount; i++) {
DataRow row = gridView1.GetDataRow(i);
dataRows.Add(row);
}
and then you can do whatever, or select a value from the row before you add it to an collection while using the column header:
object result = gridview1.GetDataRow(i)["ID"];

Categories