How to take only first index from combobox c# - c#

I have two indexes on my combobox.
I want to take only the first index ?
Can I get some example ?
The code:
foreach (DataRow dr2 in dt2.Rows)
{
comboBox1.Items.Add(dr2["Station"].ToString() + " - " + dr2["Ime"].ToString());
}
I want to take dr2["Station"].ToString() ?

The Win Forms ComboBox has both a SelectedIndex and SelectedText property.
Once your list is loaded with items you can pick which one is selected like this:
// selected by position in the list
comboBox1.SelectedIndex = 0;
// ... by value
comboBox1.SelectedText = "some value";

Related

C# What does setting the SelectedIndex of a combobox to -1 do?

When I was working on one of my projects I was trying to write to a datasourced combobox and then write a value into the combobox like below:
//Create list for combobox
List<string> companyList= new List<string>() { "", "Company1", "Company2" };
//Datsource list to combobox
cbCompanyName.DataSource = companyList;
//If form is set to import data and the billing address is not null
if (importAddress && StaticValues.billAddress != null)
{
//Fill all fields with data from Static Values class
cbCompanyName.Text = StaticValues.billAddress.CompanyName;
cbCountry.Text = StaticValues.billAddress.Country;
}
else
{
//Set country to US
cbCountry.SelectedIndex = 0;
}
however the line cbCompanyName.Text = StaticValues.billAddress.CompanyName; ran without writing any text to the combobox, until I set the selected index of the combobox to -1. What does setting the combobox selected index to -1 do that would change this as apposed to setting the selected index to 0?
Setting the SelectedIndex on a ComboBox to -1 deselects (SelectedItem is NULL). Setting to 0 selects the first item in Items
Combobox needs to know what is my value and display member,
giving the datasource is not enough for it.
I think you can use like this or
// comboBox.DisplayMember = "Text";
// comboBox.ValueMember = "Value";
int =0;
companyList.forEach(x=> comboBox.Items.Add(new { Text = x.toString(), Value =i++ }));
comboBox1.SelectedIndex = 0;
you can look this article
similar question and answers

Getting the value and items in checkListBox

I am writing a Windows Forms application that has a checkListBox. I have a databinded checkListBox value that is connected to my sql db. I want to write a loop to loop through a list of checked items and get its value (not index). I am wondering is there a way to do it just like the comboBox.SelectedValue?
foreach(var item in checkListBox.CheckedItems){
//get the value of that
string query = select * from employeeId where '"+checkListBox.SelectedValue+"'
}
You can try like this:
foreach(object item in checkListBox.CheckedItems)
{
DataRowView dt = item as DataRowView;
string str = dt["nameHere"];
// some code
}
You should cast the item to the relevant type (DataRowView?)
foreach(var item in checkListBox.CheckedItems){
var val = item as DataRowView;
// retrieving the relevant values
}
You can try this
foreach(var item in checkListBox.CheckedItems){
var value = (item as ListItem).Value;
}
The items in the CheckedListBox and checks every other item in the list. Using the Items property to get the CheckedListBox.ObjectCollection to get the Count of items.
Using the SetItemCheckState and SetItemChecked methods to set the check state of an item. For every other item that is to be checked, SetItemCheckState is called to set the CheckState to Indeterminate, while SetItemChecked is called on the other item to set the checked state to Checked.
//checkedListBox1.SetItemChecked(a,true);
for ( int i = 0; i < checkedListBox1.Items.Count; i++ )
{
if(checkedListBox1.GetItemCheckState(i) == CheckState.Checked)
{
string query = select * from employeeId where '" + checkedListBox1.Items[i].ToString() + "';
}
}
You can do it other way as well
List<object> _checkedItems = checkedListBox1.CheckedItems.OfType<object>().ToList();
This will give you all the checked items. If you want to pass this into sql query then you can do something like
string delimeter = "','";
string _selectedItems ="'"+ _checkedItems.Aggregate((i, j) => i + delimeter + j).ToString()+"'";
and pass it in your sql query
string query = select * from employeeId where somevalue in ("+_selectedItems +")

Checklistbox issue displaying selection in message box

I am having an issue getting the checkedlistbox to display all selections in a message box for a windows application. I am only getting the last one selected to display. For example, I select "one, three, and five", only five displays.
Here is my code:
string display = "";
foreach (object selectedItems in clb.CheckedItems)
{
if (clb.SelectedItems.Count != 0)
{
display = "Items needed\n-----------\n\n\n" + selectedItems.ToString();
}
else
{
display = "No items selected";
}
}
MessageBox.Show(display, "Title");
Any ideas to point me in the right direction to accomplish this is appreciated.
Your error is in the loop that starts before the test of the number of items selected/checked. Your loop continues changing the value of the variable display at each loop. At the end the variable contains only the last item checked/selectd.
So, I assume that you want to display the checkeditems, not the selecteditems.
In any case you need to loop over the collection (ChekedItems in this case) and accumulate in a stringbuilder the item texts that you want to display.
string display = "";
// Every item in this collection is an item
// with CheckState = Checked or Indeterminate
if (clb.CheckedItems.Count != 0)
{
StringBuilder sb = new StringBuilder();
foreach(string item in clb.CheckedItems)
sb.AppendLine(item);
display = "Items needed\n-----------\n\n\n" + sb.ToString();
}
else
{
display = "No items checked";
}
MessageBox.Show(display, "Title");
If you really want to loop on the selecteditems, the code is the same, but just use the SelectedItems collection
you need to concat the selected items like display += or better to use Stringbuilder
display += " Items needed\n-----------\n\n\n" + selectedItems.ToString();
OR you can do as below
if(clb.CheckedItems.Count >0)
display = "Items needed\n-----------\n\n\n" + string.Join(",", clb.CheckedItems.Select( i=>i.ToString()));
else
display = "No items selected";

listbox selected item give me " System.Data.DataRowView" , C# winforms

I have listbox1 - its datasource is a column (productname).
so i have in the listbox a MultiSelection option.
and im trying to make a MessageBox for all the option i selected and this the code:
foreach (object selectedItem in listBox1.SelectedItems)
{
MessageBox.Show((selectedItem.ToString() + Environment.NewLine));
}
the problem is that im getting this value insteadSystem.Data.DataRowView
How do you populate the listbox (ie what is exactly the datasource)?
From your comment I would say a DataView (and wich contains DataRowView...)
So you just need to cast the SelectedItem into DataRowView in order to get a value from this DataRowView:
foreach (object selectedItem in listBox1.SelectedItems)
{
DataRowView dr = (DataRowView)selectedItem;
String result = dr["productname"].ToString;
MessageBox.Show(result + Environment.NewLine);
}
The VB.Net developers that could fall on this post could also be interested in this.
Try to change with this
ListBoxItem lbi ;
String myStr ;
for (int i =0; i <= listbox1.selecteditems.count-1 ; i++)
{
lbi = (ListBoxItem)(listBox1.ItemContainerGenerator.ContainerFromIndex(i));
myStr += lbi + Environment.NewLine);
}
MessageBox.Show(myStr);
You can also use the ordinal value as in:
DataRowView dr = (DataRowView)selectedItem;
String result = dr[1].ToString;

Assigning each property of every single item of IEnumerable Collection to different textboxes

Elaboration of what I want to achieve :
I have an collection of an object in my itemsource.Suppose there are three items in my itemsource and i want each property of every single item to be assigned to different textboxes, how can i get this ?
textbox1.text = // assign the first value of an item to this
textbox2.text = // assign the second value of an item to this
Why would you need a lambda?
var itemSource = enumerable.toList();
textbox1.text = itemSource[0].toString();
textbox2.text = itemSource[1].toString();
textbox1.Text = enumerable.First();
textbox2.Text = enumerable.Skip(1).First();
Yet another way to skin this cat:
textbox1.Text = itemSource.ElementAtOrDefault(0);
textbox2.Text = itemSource.ElementAtOrDefault(1);
You could put your text boxes into a list and step through both the items source and the text box list.
var textBoxes = new List<TextBox> { textbox1, textbox2 };
for( int index = 0; index < itemsSource.Count; index++ )
{
textBoxes[index].Text = itemsSource[index].ToString();
}
I'm not sure what a lambda expression would do for you. Could you expand your question?

Categories

Resources