drop downlist selected value - c#

ddlType.DataSource = ObjComplaintReportFormBLL.ComplaintType();
ddlType.DataTextField = "ComplaintType_Name";
ddlType.DataValueField = "complainttype_id";
ddlType.DataBind();
ddlType.Items.Insert(0, "All");
ObjComplaintReportFormBLL.ComplaintType() returns the ComplaintType_Name, complainttype_id
All is the default value for the drop down list Now how should I set the value of this list item "All" to 0(int)
I could do
ddlType.Items[0].value = "0". But this is a string
Thanks
Sun

Replace
ddlType.Items.Insert(0, "All");
with
ddlType.Items.Add(new ListItem(0,"All"));

By using Insert, you're putting the text "All" as the first option in the dropdown with a value of "" (empty string). To get "0" to be your value for your "All" item, provide a ListItem:
ddlType.Items.Add(new ListItem("0", "All"));
or, perhaps closer to what you want to do, this will insert your "All" item at the start of the list:
ddlType.Items.Insert(0, new ListItem("0", "All"));
then:
ddlType.SelectedValue = "0";

You can pass the listItem object to the ddlType.Items.Insert method instead of passing a string value e.g.
ListItem liItem = new ListItem("All","0");
ddlType.Items.Insert(0,liItem);

Related

How to remove an item from dropdownlist control in c#

I have a dropwdown list with few items like this(in the format "name-number"):
George Michael - 456456
Justin Dukes - 5644654
Peter Heins - 5456454
Mark Twain - 4454564
Now i have to delete an item from the above list based on the value i have from other query.
My other query gives me a number in string format and i have to remove exactly the same item which is in the above dropdownlist second part(after hyphen).
For ex: My query gives me "5456454".
Now I have to remove the third item(Peter Heins - 5456454) from the dropdownlist which matches with the number my query returns.
I am trying to use
ddl.Items.Remove
But Im not getting what parameter i should pass. Both the text and value fields are same in the dropdownlist and i cannot modify that. So i cannot use
ddl.Items.FindByValue()
Any help?
Thanks in advance.
You could do something like this:
var removeItem = ddl.Items.Cast<ListItem>()
.Where(x => x.Value.Contains(number))
.FirstOrDefault()
ddl.Items.Remove(removeItem);
If you're populating your DropDownList with ListItem objects, I would set the text for example to "Peter Heins - 5456454" and the value to "5456454".
ListItem exampleItem = new ListItem("Peter Heins - 5456454", "5456454");
Then, you could do as you described and call
string value = "5456454";
ListItem valueToRemove = ddl.Items.FindByValue(valueToRemove);
dd1.Remove(valuetoRemove);
edit: If you absolutely cannot change the value (whether it be for school purposes or some other reason) I would do something like this.
string id = "5456454";
foreach (ListItem item in dd1.Items)
{
if (item.Text.Contains(id))
{
dd1.Items.Remove(item);
}
}

how to set the default value to the drop down list in C#?

DataSet dsCurrency = new DataSet();
dsCurrency = ParamCurrency.SelectCurrencys();
ddCurrencyField.DataSource = dsCurrency;
ddCurrencyField.DataTextField = "CurrencyName";
ddCurrencyField.DataValueField ="CurrencyCode";
ddCurrencyField.DataBind();
How to select a default value to the dropdownlist control using C#?
If you know the value will exist:
ddCurrencyField.FindItemByText("YourDefaultText").Selected = true;
else
ListItem selectedListItem = ddCurrencyField.Items.FindItemByText("YourDefaultText");
if (selectedListItem != null)
{
selectedListItem.Selected = true;
};
You can also find item by value :
ListItem selectedListItem = ddCurrencyField.Items.FindByValue("YourDefaultValue");
if (selectedListItem != null)
{
selectedListItem.Selected = true;
};
I assume in your datasource object (dsCurrency) is not parsing the default value for the dropdown.
So first you will have to add the default item. After binding the datasource do the following.
ddCurrencyField.Items.Insert(0, new ListItem("-- Select --",0));
With the above code you will have a default/first item selected as "--Select--". If it does not select the first item then simply set the SelectedIndex to 0.
There are 2 ways to set the default item after populating a dropdown.
you can use the "SelectedValue" property
you can use the "SelectedIndex" property
Most of the code samples are given in the previous answers. But I prefer to use the "FindByValue" method.
ddCurrencyField.SelectedIndex = ddCurrencyField.Items.IndexOf(ddCurrencyField.Items.FindByValue(myValue));
If you want to write a safe code please use the second option.
If this dropdown list is a combobox, use this:
ddCurrencyField.SelectedIndex = ddCurrencyField.Items.IndexOf("Wanted Value");

How to get the selected value from combo box in wpf?

I am trying to get the DepartmentId after selecting the item in combo box and then assign it to the nameTextBox.Text. But when I run the program it gives Null Reference Exception.
var deptList = dr.ReadAllDepartment();
departmentCombobox.DisplayMemberPath = "DepartmentName";
departmentCombobox.SelectedValuePath = "DepartmentId";
departmentCombobox.ItemsSource = deptList;
nameTextBox.Text = departmentCombobox.SelectedValue.ToString();
SelectedValue is null because there is no selected value. The user didn't select a value in the brief moment between when you set the ItemSource and when you tried to access SelectedValue.
At some point departmentCombobox.SelectedValue is null and the ToString() is being called on null.
You can use the ? operator;
you can also select the first item on the ComboBox before trying to access it:
var deptList = dr.ReadAllDepartment();
departmentCombobox.DisplayMemberPath = "DepartmentName";
departmentCombobox.SelectedValuePath = "DepartmentId";
departmentCombobox.ItemsSource = deptList;
departmentCombobox.SelectedIndex = 0;
nameTextBox.Text = departmentCombobox.SelectedValue?.ToString();

C#-comparing combobox and that of the list items

I have a combobox in which i can select three items and a list that contains items i want to check first of all whether the value selected from the combobox is in list and then the list item which is same as combobox one ; want to do some ops on it.
List<string>names = af.GetBlankSignatureNames();
comboBox1.SelectedItem.ToString();//combobox value taken
How to do so?
you can do:
if(names.Any(r=> r == comboBox1.SelectedItem.ToString())
{
// match found
}
else
{
// not found
}
Or to get the item from the list try:
string str = names.FirstOrDefault(r=> r == comboBox1.SelectedItem.ToString());
if str is null that means string not found in the names list, if its not null then you got the string as well, (which by the way would be same as comboBox1.SelectedItem.ToString())

How to get value from datagridview combobox?

How to get value from datagridview combobox after selected value is changed?
You can use:
var value = DataGridView.Rows[0].Cells[0].Value
NOTE: You would supply the correct row and cell number.
Or you can do something like this if it is bound to an object like ListItem
string value = DataGridView.Rows[RowIndex].Cells[ColumnIndex].Value.ToString();
if DataGridView.Rows[RowIndex].Cells[ColumnIndex] is DataGridViewComboBoxCell && !string.IsNullOrEmpty(value))
{
List<ListItem> items = ((DataGridViewComboBoxCellDataGridView.Rows[RowIndex].Cells[e.ColumnIndex]).Items.Cast<ListItem>().ToList();
ListItem item = items.Find(i => i.Value.Equals(value));
}
Do not pass to give column name
string SelectedText = Convert.ToString((Pass_dataGridView.Rows[0].Cells["Partner"] as DataGridViewComboBoxCell).FormattedValue.ToString());

Categories