Tick correct checkbox if 'checked' within the database - c#

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);
}

Related

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.

Application Settings | System.Collections.Specialized.StringCollection Only Saves One Value

In the application settings I have a setting called Locations and the type is a System.Collections.Specialized.StringCollection.
I also have a textbox and a button that are supposed to add values to the StringCollection.
After entering the value into the textbox, the value is supposed to be added into a listbox and into the StringCollection. The code for adding a new value to the listbox and StringCollection is as follow:
string newPrintLocation = tbAddPrintLocation.Text;
if (lbPrintLocations.Items.Contains(newPrintLocation) == false)
{
lbPrintLocations.Items.Add(newPrintLocation);
Properties.Settings.Default.Locations.Add(newPrintLocation);
cbPrintLocation.Items.Add(newPrintLocation);
tbAddPrintLocation.Clear();
}
The goal is when the user restarts the form the values are added back into the listbox. Currently this is the code that I use to try to achieve that:
foreach (var item in Properties.Settings.Default.Locations)
{
lbPrintLocations.Items.Add(item);
}
The problem is that only the first value gets added to the listbox but none of the rest. At this point I have no idea what I am doing wrong. So I was wondering if someone could help me out or point me to the right direction.
All help is greatly appreciated.
Cheers,
Quartermain.

C# Is there a way to search through all listview items with a textbox?

I have developed an application which populates a Listview through a SQL CE connection. I already have the insert, update and delete functions in it. The only thing that's left is the search function. I thought it was the easiest part, but I am searching for 2 days now and haven't found a good tutorial on this, so I thought someone on StackOverflow could help me.
I have managed to search through the first item (main item, "PrincipalID") and I also want to search through the subitems. What I want is like
Principal ID Email Address Subject Filename
193490 test#test.com Exec OUT (report) q9193.xls
153130 test#test.com Standard mail Report q7389.xls
What I now have is that I can search for principal numbers, but what I also want is to search for words, for example if I type "test" in the textbox, that I will get the record with the Email Address = "test#test.com" on top, or what would be better is that it will only show that record and hide the rest and then if the textbox is empty, that it shows all the records again.
So basically I want to search the WHOLE listview for the string that is typed in the search textbox.
Hopefully someone can help me,
I will appreciate it from the bottom of my heart.
Thanks in advance!
Try using this C# code or something similar to extract the listview items that match your string parameter.
public void SearchInListView(ListView myListView, string searchThis)
{
if (searchThis != "")
{
foreach (ListViewItem lvItem in myListView.Items)
{
foreach (ListViewItem.ListViewSubItem lvSubItem in lvItem.SubItems)
{
if (lvSubItem.Length >= 1)
{
if (lvSubItem.ToLower().Contains(searchThis.ToLower()))
//Mark this listview item in some way (e.g., change back color) or collect and return it
else
//Mark this listview in a different way
}
}
}
}
}
You can use the TextChanged Event of a TextBox and if you search through the List you can bind only the Record you are searching for to the Grid.
So you can search for a specifik record in the List.
you have to fire two queries....
if enter Textbox1.Text.IsNumber then search by principle id
else search by email
use like in query
maybe its work

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;

Read all item from listbox in windows application

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?

Categories

Resources