I want to bind more then 1 columns to drop down list, so that I can get the column values when user clicks a button,
ddlListMine.DataSource = GetSomeChickens();
ddListMine.DataTextField = "ChickenName";
ddListMine.DataValueField= "NumberOfEggsChickenLay";
ddListMine.Items.Insert(0, new ListItem("Please Please Please Select....", "0"));
ddListMine.DataBind();
I have another column "ChickenType", which I want to access in Selected Index change column.
GetSomeChickens(); returns 6 columns, including ChickenName, NumberOfEggsChickenLay, ChickenType and so on...
Edit
Off course, I can call database again in selected index change method, but there must be a way around i think
The DropDownList doesn't hold the entire object during the binding, only the Text and Value as defined by DataTextField and DataValueField.
In order to get a selected object back, you can have a method to get the ChickenType by passing the ChickenName using Linq like this.
List<Chicken> Chickens = GetSomeChickens();
Var Chicken= Chickens.FirstOrDefault(c => c.ChickenName== ddlListMine.SelectedItem.Text);
if(Chicken!= null)
{
string ChickenType = Chicken.ChickenType ;
}
Related
I have a combo box which I want the default value to be -- Select Gender --
I have tried the following coding, and I got this error
Items collection must be empty before using ItemsSource.
Please help
cboGender.ItemsSource = null;
cboGender.DisplayMemberPath = "Display";
cboGender.SelectedValuePath = "Value";
cboGender.SetBinding(ComboBox.ItemsSourceProperty, oBinding);
cboGender.Items.Insert(0, "--Select Gender--");
cboGender.SelectedIndex = 0;
Add "--Select Gender--" to your Collection before binding it to the ItemsSource (if you want it as first, remember to use Insert(0, ) and then after binding it, set the SelectedIndex to it.
May be you should try to call
cboGender.Items.Clear();
before the insertion of the default item.
OR
Just create your data source with the default value item before inserting any value in the combo box and write something like this:
cboGender.Items.Clear();
cboGender.ItemsSource = dataSource;
where dataSource would be the list with the all items.
You can use
<ComboBox x:Name="cboGender"
Text="--Select Gender--" />
if you don't need to choose this default value again after first selection
I have a ComboBox control.
I bind to this control to the DataSet table.
Here is the code:
comboBox.Items.Add(("Select"));
comboBox.DataSource = DataSet.ColorTable;
comboBox.DisplayMember = DataSet.ColorTable.ColorNameColumn.ColumnName;
comboBox.ValueMember = DataSet.ColorTable.ColorIDColumn.ColumnName;
This result I get:
I want to display on the top of the list SELECT: word. So I need to add addition Item to the comboBox control.
Here how I implement it:
cmbCategory.Items.Add(("Select"));
But the result is still the same as above. I get only colors without SELECT: word on the top of the list.
Any idea how I can add this string-SELECT: to the ComboBox control and set to this string ValueMember?
Use Insert method instead.
cmbCategory.Items.Insert(0, "Select");
Note : Put this code after the databind.
You can add the collections of color to an array or a dataset (if you are getting them from database) first and then add items "select", then add each elements of the array or a column of the dataset.
Do this in Form_Load function and wherever there are changes made in color collections array or database.
//This will set Display member and value member
comboBox.DisplayMember = "ColorName";
comboBox.ValueMember = "ColorCode";
//This will add a new row to table in binded dataset
DataRow dr = dal.MyProperty_dsColors.Tables["ColorInfo"].NewRow();
dr["ColorName"] = "Select Color"; //SomeName
dr["ColorCode"] = 001; //Some ID
dal.MyProperty_dsColors.Tables["ColorInfo].Rows.Add(dr);
//binding dataSource
comboBox.DataSource = dal.MyProperty_dsColors.Tables["ColorInfo"];
What would also help you is that you set the ComboBox without having to 'Select' it when the popup arrives...
Select your ComboBox, under the properties tab, select Appearance->Drop Down Style and select DropDownList.
If we want to add manually values in a combobox (for example integers) this can be done using a for loop:
// sample code
int lower=1;
int higher=500;
for (int i=lower; i<=higher; i++)
combo_values.Items.Add(i.ToString());
Note that you have to use the int.Parse(combo_values.Text) command to read a value.
I gave datatable as datasource to Lisbox.
That tables fields are ID, Subject, Texts and ID is an Unique field(PK).
But Subject shown as DisplayMember.
Here is my datasource giving code:
lbTexts.DataSource = mDataSet.Tables["Story"];
lbTexts.DisplayMember = "Subject";
i want to know which rows the user has selected and what is the Unique value.
i find it with 10 rows of code. But i hope there is a simple way to find it.
If you use the ValueMember property and set it to Id, you can use the SelectedValue parameter without needing the entire dataset. You're also guaranteed to not get resorting errors where the selected index in the list doesn't match the index in the data.
lbTexts.ValueMember = "Id";
// Later
int selectedId = Int32.Parse(lbTexts.SelectedValue);
Use SelectedItem to get the currently selected item, and use SelectedValue to get the value.
Example of selectedItem:
String value = lbTexts.SelectedItem.Value;
Example of selectedValue:
String txt = lbTexts.SelectedValue.ToString();
Try this,
string value = lbTexts.SelectedItem.Value;
Friends, I'm using datagridviewcomboboxcolumn as column index 1 in my datagridview. I've fetched data from access database table and populated the datagridviewcomboboxcolumn by following manner:
for (int i = 0; reader.Read(); i++)
{
cmbBusCode.Items.Add(reader["BUSINESS_CODE"] + "-" + reader["BUSINESS_DESCRIPTION"]);
cmbBusCode.ValueMember = "BUSINESS_CODE";
}
Then I'm writing this:
cmbBusCode.DisplayIndex = 1;
cmbBusCode.Width = 200;
cmbBusCode.Name = "Code";
cmbBusCode.HeaderText = "Code";
And adding this column as
dgView.Columns.Add(cmbBusCode);
Combobox is being populated and I can select any one from the list. Now when I'm saving data I want to get the ValueMember of the selected item. To get this value I'm using following code but It's giving the string representation of the field "BUSINESS_CODE" not the value..Please help me so that get ValueMemeber of the selected item..
foreach (DataGridViewRow row in dgView.Rows)
{
string cd = row.Cells["Code"].Value.ToString();
}
The "item" you are adding to the column doesn't have a "BUSINESS_CODE" column (essentially, the item as you add it is simply a string), so that doesn't work.
What you need to do is assign items that contain multiple columns. One must be the BUSINESS_CODE column (as it is this column you want to be the value for the underlying field in the DataGridView), the other should be a display column that contains the concatenated value you're adding at the moment (DisplayMember and ValueMember).
Easiest would be to create a typed data set, add a table that contains the two columns I described and fill the table with the data from the data reader.
Then, add the table as the data source for the column.
You can try adding a DataGridViewComboBoxCell instead of a string to the cmbBusCode.Items collection. You can then specify Value in the cell. I'm not sure if this will work, but it's worth a try.
On a winform there is a combobox that derives its information from a datatable. The datatable draws from a database list.
this.cboList.DataSource = pullData();
this.cboList.DisplayMember = "fieldA";
Once the DataSource is set I am not able to insert a default row (ie *) as the first item in the combobox.
I tried this:
this.cboList.Items.Insert(0,"*");
Is there a way to insert in the combobox after the datasource is set or should this be done in the datatable?
UPDATE1:
The solution looks something like this:
var list = mydt.AsEnumerable().Select(row => row.Field<string>(fieldName)).ToList();
list.Insert(0, "*");
Where mydt is a populated datatable and fieldName is a variable holding the database field name.
Don't modify your data at the source just to make your UI work. Instead, perhaps extract your column into a list that you can modify before attaching it to the combo box.
var list = table.AsEnumerable().Select(row => row.Field<string>("fieldA")).ToList();
list.Insert(0, "*");
this.cboList.DataSource = list;
If a "Select None", or "*" is a valid select option it needs to come from the binding source object. I have done this in the past by adding a default record to a collection before binding it to a combo box.