c# dynamic LINQ query - c#

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.

Related

filling combo box using LINQ query (distinct)

I have a combocox the name is "cmbModel" I want to fill the database with two different values in a table.
This is what I have done:
private void Form1_Load(object sender, EventArgs e)
{
using (LINQSQLDataContext db = new LINQSQLDataContext())
{
cmbModel.DisplayMember = "szModel";
cmbModel.DataSource = db.VehEcus.ToList<VehEcu>();
}
}
this will fill my cmbModel with szModel column of my table but I want to avoid repeating , how can I use "distinct" in query to achieve my goal?
and also I want to show 2 items of my table like "modelID-szModel" in my combobox
Thanks
If you just want to show a single column anyway you could select that column and use Distinct:
cmbModel.DataSource = db.InfoProg_VehEcus.Select(x => x.szModel).Distinct();
You can apply Distinct() at any point after your query. I recommend doing it before you enumerate.
To create the custom modelID-szModel field, you can enumerate the query using AsEnumerable(), then create an anonymous type and use String.Format to concatenate your values into your new field.
using (LINQSQLDataContext c = new LINQSQLDataContext ())
{
var items = c.VehEcus.Select(t => new
{
a = t.szModel,
b = t.modelID
}).Distinct()
.AsEnumerable().Select(t => new
{
displayMember = String.Format("{0}-{1}", t.a, t.b)
});
cmbModel.DisplayMember = "displayMember";
cmbModel.DataSource = items.ToList();
}

How to Add Columns from DataTable to ComboBox in 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());
}

Manually Populating and Selecting ComboBox Items

I can't believe how difficult this simple task is.
I have the following code:
cboCountry.ValueMember = "ID";
cboCountry.DisplayMember = "Title";
var countries = from c in context.Set<sc_Countries>()
orderby c.Title
select new { c.ID, c.Title };
Now, I want to populate the ComboBox cboCountry with this collection, and then I want to select the list item with the ID (value) "US".
I was able to add the items to the ComboBox using cboCountry.Items.AddRange(countries.ToList()), but then cboCountry.SelectedValue = "US" had no effect.
Next, I tried adding the collection using cboCountry.DataSource = countries but this just left the control list empty.
Surely there must be a simple way to accomplish this trivial task. Can anyone offer the missing ingredient?
Until you call ToList() on your LINQ statement, you're not actually getting data from the database:
var countries = (from c in context.Set<sc_Countries>()
orderby c.Title
select new { c.ID, c.Title }).ToList();
Now you should be able to set the DataSource, etc. like you were:
cboCountry.ValueMember = "ID";
cboCountry.DisplayMember = "Title";
cboCountry.DataSource = countries;
cboCountry.SelectedValue = "US"
Edit:
Now that I'm re-reading your question, it looks like you were already calling countries.ToList(), but using Items.AddRange. I see the same thing you do when I try it. It appears you have to set the DataSource instead of using Items.AddRange, for SelectedValue to work.

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;

Categories