Bind ComboBox to DataSet in code behind (no XAML) - c#

How to bind a Combo Box to DataSet in code behind, without using XAML at all:
I tried the following, but all my combobox items are "System.Data.DataRowView" instead of the actual value. What is wrong?
string str = #"SELECT * FROM FooTable";
da.SelectCommand = new SqlCeCommand(str, connection);
da.Fill(devDs, "FooTable");
dt = ds.Tables["FooTable"];
comboBox1.ItemsSource = devDt.DefaultView;

You will have to set DisplayMemberPath property
combobox.DisplayMemberPath = "ColumnName"

You can use comboBox1.DisplayMemberPath to set which column in your table should be used for UI presentation.
Test sample:
var dataTable = new DataTable();
dataTable.Columns.Add("Id", typeof(int));
dataTable.Columns.Add("Name", typeof(string));
dataTable.Rows.Add(1, "Test1");
dataTable.Rows.Add(2, "Test2");
comboBox1.ItemsSource = dataTable.DefaultView;
comboBox1.DisplayMemberPath = "Name";

Related

C# Using Listbox to display only a select few characters of description

The code i currently have below populates a list box with the Id, description and company
DataTable dTable = new DataTable();
dataAdapter.Fill(dTable);
DataColumn dcDisplayMember = new DataColumn("DisplayMember");
dcDisplayMember.Expression = string.Format("{0}+'\t\t'+{1}+'\t\t'+{2}", "dId", "Description", "Company");
dTable.Columns.Add(dcDisplayMember);
BindingSource bSource = new BindingSource();
bSource.DataSource = dTable;
listBox.DataSource = bSource;
listBox.DisplayMember = "DisplayMember";
However what i am trying to achieve is getting the list box to display a shorter version of the description column e.g. description of something...
I know i have to do something to
string.Format("{0}+'\t\t'+{1}+'\t\t'+{2}", "dId", "Description", "Company")
But i am not sure as to what

Create and Bind DataGridViewColumn with query results. Add it to DataGridView

I am desperately trying to add a DataGridViewComboBoxColumn to my DataGridView programatically. Here is the Code I've got so far.
DataTable dt = new DataTable();
dt = _userBL.getUsersTable().DefaultView.ToTable(false, "Person_ID", "FirstName", "LastName", "Title", "Username");
dataGridView1.DataSource = dt;
DataGridViewComboBoxColumn ComboBoxCell = new DataGridViewComboBoxColumn();
ComboBoxCell.Name = "State";
ComboBoxCell.ValueMember = "State";//ComboBoxCell.DisplayMember = "State";
ComboBoxCell.DisplayMember = "State";
ComboBoxCell.DataSource = _userBL.getUsersTable().DefaultView.ToTable(false, "State");
this.dataGridView1.Columns.Add(ComboBoxCell);
But i get no data in the comboboxcolumn, and since I added DataSource, I cannot add items (Active,Inactive) in it. Also, when I click it a small dropdown should appear, even for empty comboboxes, but it does not.
Thanks in advance!
Try this one..
DataGridViewComboBoxColumn combo = (DataGridViewComboBoxColumn)dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1].OwningColumn;
dt.Clear();
da = new SqlDataAdapter("select name from tblData", con);
da.Fill(dt);
combo.DataSource = dt;
combo.DisplayMember = "name";

C# - unexpected result in populating ComboBox with MySql database

I my class DBConnect, I have this function to populate a ComboBox to database based on input query:
public void POPULATE_COMBOBOX(string query, ComboBox myComboBox)
{
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
DataTable myDataTable = new DataTable();
adapter.Fill(myDataTable);
myComboBox.DataSource = myDataTable;
this.CloseConnection();
}
}
And here's how I use it
DBConnect.POPULATE_COMBOBOX("SELECT NAME FROM users", comboBox_Name);
I have 3 rows in column NAME, and I expect those 3 names will be displayed in my comboBox_Name. However instead, I got 3 lines of System.Data.DataRowView in my combobox. Any idea how to convert those DataRowView to string?
You have to tell combobox what column to display.
//Preparation
var dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add("Name1");
dt.Rows.Add("Name2");
//Setup data binding
myComboBox.DataSource = dt.DefaultView;
myComboBox.DisplayMember = "Name";
myComboBox.ValueMember = "Name";
Then SelectedItem gets the actual ComboBox item (in this case a DataRowView), whereas SelectedValue gets the value of the property you specified as the ValueMember of the ComboBox
Or another way how to populate your ComboBox is:
MySqlDataReader sqlReader = cmd.ExecuteReader();
while (sqlReader.Read())
{
myComboBox.Items.Add(sqlReader["Name"].ToString());
}
sqlReader.Close();

How to fill DataGridViewComboBoxColumn from a database table column without using datareader

Here is my code:
SqlDataAdapter GridDataAdapter = new SqlDataAdapter(query, con);
DataSet GridDataSet = new DataSet();
GridDataAdapter.Fill(GridDataSet, tbln);
dataGridView1.DataSource = GridDataSet;
DataGridViewComboBoxColumn dgvCB = new DataGridViewComboBoxColumn();
dataGridView1.DataMember = tbln;
Here, I want to fetch data for DataGridViewComboBox from a database table column.
How can I fill the DataGridViewComboBoxColumn without using a DataReader?
You already have the dataset filled, you could just iterate over the row collection if it contains the values that you want the DGVCombo to contain. So, the most straightforward way would be something along the lines of:
foreach(DataRow r in GridDataSet.Tables[0].Rows)
{
dgvCB.Items.Add(r["MyColumn"]);
}
Where .Tables[0] has the column("MyColumn") that you are looking for...
This should be what you need...
DataGridViewComboBoxColumn dgvCB = new DataGridViewComboBoxColumn();
dgvCB.Name = "lastname";
dgvCB.DataSource = tbln;
dgvCB.HeaderText = "Last";
//uncomment this to actually select the value in the combo box
//dgvCB.DataPropertyName = "lastname";
dgvCB.ValueMember = "lastname";
dataGridView1.Columns.Add(dgvCB);

How to use combo box in c#

I have no idea where to start. i tried DataTable but it didn't work.(This is an easy question :) )
I tried everything
{
var test = new DataTable();
test.Columns.Add("test");
test.TableName = "test";
test.Columns.Add("test");
comboBox1.DataSource = test.XXXX ;
}
Assuming you mean winforms, something like:
DataTable test = new DataTable();
test.TableName = "test";
test.Columns.Add("foo", typeof(string));
test.Columns.Add("bar", typeof(int));
test.Rows.Add("abc", 123);
test.Rows.Add("def", 456);
ComboBox cbo = new ComboBox();
cbo.DataSource = test;
cbo.DisplayMember = "foo";
cbo.ValueMember = "bar";
Form form = new Form();
form.Controls.Add(cbo);
Application.Run(form);
(in particular, SelectedValue should give you the 123 and 456 - useful for ids, etc)
ComboBox.Items property, unless you want data from a database or something.
DataTable dt=new DataTable();
dt.Columns.Add("Col1",typeof(int));
dt.Columns.Add("Col2",typeof(String));
dt.Rows.Add(1,"A");
dt.Rows.Add(2,"B");
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Col2";
comboBox1.ValueMember = "Col1";
You'll need to set the 'DataItemField' and 'DataValueField' to the appropriate column names in your datatable.

Categories

Resources