Read all item from listbox in windows application - c#

I have two listbox in windows Form c#, the first one has all valid rooms at this time & the 2nd has the current rooms reserved to the Guest, & the user can change this data by transfer between this listboxex.
as example:
list1: 1d ,5d ,6d , 1r, 12r
list2: 2d, 4d
the user can delete 2d and put instead of it 6d.
what I need how can I read all data from list2 after user update it, & use this to update my table in sql server.

To copy a selected item from one list box to another you could use the following code. Ensure that the SelectionMode for the listbox is Single. This bit of code reads the selected item, adds it to the other ListBox and removes it from the current ListBox. You do not need to cast it to a string as there is no reason to read the actual data. This also allows you to have complex objects as listBoxItems.
object obj = listBox1.SelectedItem;
listBox2.Items.Add(obj);
listBox1.Items.Remove(obj);
If you link this to left/right buttons, you can ensure that the user is not able to duplicate rooms or add room numbers which are outside the scope.

Try code below:
string TimeslotItems = "";
foreach (ListItem item in listBox2.Items)
{
TimeslotItems += item.ToString() + ","; // /n to print each item on new line or you omit /n to print text on same line
}

There is no 'item added' event on ListBox. When you use a BindingList as your datasource you can register events to the BindingList itself and respond to those. If you want an item added event on the List itself look at Item added events question for ListBox
To iterate over your list items:
foreach (string s in listBox1.Items)
{
string name = s;
}
In regards to your database transfer, what way do you want to go? Using Entity Framework? Direct SQL execution?

Related

Can't manually add item to listbox that is populated iterating through a list

In a C# WinForms project I'm querying a database to get a list of values to populate a listbox with. The query populates a List and then I iterate through that to add the list items to the listbox.
lsNewValuesList = dbGetNewValueInfo.GetNewValuesDgvData(strNewValuesQuery);
foreach (string strItem in lsNewValuesList)
{
lsBxNewValues.Items.Add(strItem);
}
After that's done, I need to manually add an item to the top of the list, which I'm doing via, lsBoxNewValues.Items.Insert(0, "DELETE");. But when I run it I get the message,
Items collection cannot be modified when the DataSource property is set.
Here is a screenshot of the error (to clarify some questions):
Looking into that I'm understanding that error arises when the listbox is populated with a datasource, but I'm just populating it from a string list. Is that, technically, a datasource then?
How do I accomplish what I'm trying to do?
[UPDATE]
Okay, I've done some fiddling around, albeit without resolving my issue, and I've tried the following:
lsNewValuesList = dbGetNewValueInfo.GetNewValuesDgvData(strNewValuesQuery);
lsNewValuesList.Insert(0, "DELETE");
lsBxNewValues.DataSource = lsNewValuesList;
//foreach (string strItem in lsNewValuesList)
//{
// lsBxNewValues.Items.Add(strItem);
//}
Instead of inserting "DELETE" in the ListBox (which is what I was originally trying and was causing the error), I inserted it at index 0 of the List, the datasource of the ListBox, and then set the ListBox's datasource. "DELETE" is showing up in the ListBox, but it's getting alphabetized along with the rest of the items. I'm not doing any sorting of the list or the ListBox - I'm using ORDER BY in the database query, however. CyberZeus suggested I use Refresh() on the ListBox, but that didn't have any effect.
Yes, a list of strings is a datasource.
You have to add the data retrieved from the database to the datasource of the ListBox. You may need to refresh the ListBox.

how to identify if multiple indexes are selected from the listbox control in c# winforms?

I am developing an application and I have a requirement to place the fields
on front end just like take checkbox. If the user selected particular fields on the checkbox, then based on the selection I will generate crystal report from sql database.
So up to 10 fields checkbox is good enough. But the fields are increased up to 30 and the checkbox count is also increased on the form.
So I decided to take listbox. But in listbox how to identify if
multiple items are selected from the user?
In the listbox, I have set SelectionMode property to MultiSimple.
But if I select two or more items, listbox takes only the index of the first item.
Code :
if(listbox1.SelectedIndex==0)
{
//my code for first field.
}
if(listbox1.SelectedIndex==1)
{
//my code for second field.
}
Note : I wrote a method for getting a dynamic sql query based on the user
selected items. So in my method createSQLquery(), I want to identify the
indexes.
I want to identify which items the user has selected from the frontend and based up on that i will proceed to write my code.
Thanks
There are three ways in which you can find
1)
foreach (object item in listbox.SelectedItems)
{
// do domething
}
2)
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
// do domething
}
}
3)
var selected = ListBox1.GetSelectedIndices().ToList();
var selectedValues = (from c in selected
select ListBox1.Items[c].Value).ToList();
You can use the ListBox.SelectedIndices property to get the indexes of multiple selected items.
Gets a collection that contains the zero-based indexes of all
currently selected items in the ListBox.

Select a row in listview

I am web developer , working on a part of my project developed in WinForms. So my question could be a basic one. Try to bear with it.
I have two list views on my page and a remove button that works for both.
Problems.
I am not able to select a row in both the list view when I run my program, may be some property needed for it?
If I am able to select the row I want to detect which list view item has been selected, so how would I do that?
I have three columns and have bound the data by using the code below.
listView1.Columns.Add("ID",20);
listView1.Columns.Add("Name",40);
listView1.Columns.Add("Mobile",40);
foreach (var item in dataList)
{
newItem = new ListViewItem();
newItem.SubItems.Add(item.ID.ToString());
newItem.SubItems.Add(item.Name);
newItem.SubItems.Add(item.Mobile.ToString());
listView1.Items.Add(newItem);
}
but the ID column is left blank and the data starts to bind in these sense.
ID Name Mobile
1 abc
2 xyz
So how do I properly show the data?
Lastly I want to use my ID column to delete the data. So if I give width=0, is this the best way to hide a column?
See ListView.FullRowSelect property.
See ListView.SelectedItems property. Note, that by default ListView allows multiselection.
Set item text via constructor: newItem = new ListViewItem(item.ID.ToString());, then add rest of subitems (except of item.ID).
If you want to delete the column, just remove it from the columns collection.
Set
listView1.Items[index].Selected=true;

Tick correct checkbox if 'checked' within the database

I am using c# .net.
Thanks in advance for any help.
I have searched the web, but don't think I am using the right words, as nothing being returned is really helping.
I have a 'edit' section within my web-form which allows the user to tick (using a checklist) certain information.
For example:
• Receive newsletters
• Receive phone calls etc
The checklist is populated from a database table called Requirements.
When the user ticks a certain checkbox this information should be stored within another table userRequirement.
I can display all the requirements (from Requirements) by looping through and adding another item:
foreach (tblRequirement singleRequirement in viewAllRequirement)
{
requirementCheckBoxList.Items.Add(new ListItem(singleRequirement.requirementName,singleRequirement.rrequirementID.ToString(),true));
}
However how do I then loop through the userRequirement and automatical tick the right checkboxes?
For Example:
User selects ‘Receive Newsletters’
checkbox and presses the ‘Update’
button.
This is then stored within
the userRequirement table along with
the users ID
If the user wants to
edit their details again, they can
do. They are taken to the ‘edit’
page. Here the ‘Receive Newslettlers’
should already be selected.
Should I be using a if statement? If so can anyone help by providing an example?
Thanks
Clare
You can loop through all the items in the CheckBoxList using a foreach loop like so:
foreach (ListItem item in requirementCheckBoxLis.Items)
{
item.Selected = true; // This sets the item to be Checked
}
You can then set whether an item is checked by setting its Selected property to true. Does that help any?
In your loop, you can select the proper items as they're being entered into the CheckBoxList. Might look something like this (I don't know how your tblRequirement object works):
foreach (tblRequirement singleRequirement in viewAllRequirement)
{
ListItem newItem = new ListItem(singleRequirement.requirementName,singleRequirement.rrequirementID.ToString(),true));
//If item should be checked
if(singleRequirement.Checked)
newItem.Selected = true;
requirementCheckBoxList.Items.Add(newItem);
}

C# - Update a subitem within a listview

I am working on an application where users enter information that is then added to a listview. That works great. The only problem is, the application connects to a web site that updates the "Points" field in that listview for each account. I am not sure how I can update a single subitem within a listview.
Here is an example screenshot:
How can I select a specific subitem in a specific row to update?
Ok, I'm going to assume Windows Forms.
WinForms' ListViewItem class has a Name property, which you can use to look up a specific item in a list. So as you populate the list, assign a unique value to the Name of each:
var item = new ListViewItem("Text");
item.Name = "foo"; // some unique id string
listView1.Items.Add(item);
That way you can locate the item in the ListView later, using its Items.Find method.
var fooItem = listView1.Items.Find("foo", false);
To expand on Matt's answer, it looks like each row has a unique email address, so you could assign that as the Name property for each ListViewItem. Once you've located the row to update using the Find method, you can update that row's Points like this:
fooItem.SubItems[2] = "450";

Categories

Resources