I am trying to store a list of items i.e. a value selected from dropdown and text from textbox temporarily and then adding them 1 by one to database using foreach loop on listbox.
code behind button add event:
ListBox ListBoxFeatures = new ListBox();
ListBoxFeatures.Items.Add(new ListItem(txtBoxDescription.Text, ddlFeatures.SelectedValue));
and then i have used a foreach loop over it to grab all stored values and store in database but it always pick 1 row, means that i store only 1 row.
foreach(ListItem li in ListBoxFeatures.Items)
{
String txt= li.Text;
Int Value= li.SelectedValue.ToInt32();
//database logic i.e. InsertMethod
}
Your problem is you create a new listbox in each click event. You need to move the initialization outside of the button click. Assuming that ListBoxFeatures already exists before the button click, you can probably remove the whole line of ListBox ListBoxFeatures = new ListBox(); from your click method.
Shouldn't it be:
foreach(ListItem li in ListBoxFeatures.Items)
{ ... }
UPDATE
Also, the button event always creates a new listbox and then inserts 1 value to it... Why would you have more than one value?
Related
I have 3 Dropdown Lists (could be more in future).
DropdownList1 is a parent and is populated from a database.
DropdownList2 is a child and is populated from the selected item from DropdownList1.
DropdownList3 is a child and is populated from the selected item from DropdownList2.
I would like to add the selected item to a ListBox.
The problem i have is DropdownList2 and DropdownList3 may not always have a child item, so to add the items to a ListBox, i could store their IDs but display their text.
My database table has 3 columns to save the value (ID) from the 3 dropdown lists.
My question is if i display the text of the item in the ListBox, the next step is to save the data into the table into the relevant columns (so column 1 would save data from DD1, Column 2 would save from DD2 etc) is there anyway to identify which ID the item belongs to, so i can save the correct ID to the correct column?
So a user selects item 1 from DD1 item 2 from DD2, there is no item available from DD3, so they click add which adds the item to the ListBox. When i have to save this item how could i distinguish the item added was upto DD2 therefore it needs to be added to Column2? Or is there a better approach?
Hope this makes sense
List boxes don't like to store multiple values. They can, however, store objects as their datasource.
First create a custom object, then assign the values from each of your dropdown lists to the object:
Declare a new list of your custom object first
List<myObject> list = new List<myObject>();
then assign this to your button:
private void Button1_Click(object sender, EventArgs e)
{
myObject object = new myObject(Dropdown1, Dropdown2, Dropdown3);
list.Add(object);
//assign the list to the Listbox control
ListBox.Datasource = list;
}
When you want to access the values for your query, use a foreach loop to cycle over the objects in the list, with a pointer value to access each of them.
foreach (myObject obj in list)
{
var val1 = list[obj].Dropdown1;
var val2 = list[obj].Dropdown2;
var val3 = list[obj].Dropdown3;
//enter your sql code here
}
Let me know if you want to expand further.
I've a Listbox but when I say :
txtSelectedTables.Text += lbxTafels.GetItemText(lbxTafels.SelectedValue);
It only shows one thing in my textbox (even though I've selected multiple rows). So if I want to select multiple rows it doesn't put the values in txtSelectedTables.Text (it only shows one item).
So how can I select multiple rows and show it in a textbox.
Like RadioSpace mentioned you probably want to look at the selectedItems property
Here is an example
foreach (var item in lbxTafels.SelectedItems)
{
txtSelectedTables.Text += item.ToString();
}
Here is my full test function. If you are seeing a DataViewRow type name in the text box then maybe you have more going on than a ListBox and TextBox.
ListBox lbxTafels = new ListBox();
System.Windows.Forms.TextBox txtSelectedTables = new TextBox();
//Add 2 items
lbxTafels.Items.Add("Hello");
lbxTafels.Items.Add("World");
//Select both items
lbxTafels.SetSelected(0,true);
lbxTafels.SetSelected(1, true);
//Set the textbox
foreach (var item in lbxTafels.SelectedItems)
{
txtSelectedTables.Text += item.ToString();
}
Console.WriteLine(txtSelectedTables.Text);
The above function prints out
HelloWorld
I try to get data from the textboxex that is related to the ckeckboxex in the checkboxlist ..
in order to calculate the value of the selected box
this is my code :
foreach (ListItem item in listOthers.Items)
{
if (item.Selected)
{
sum += Convert.ToInt32(TextBox1.Text);//textbox of selected checkbox
}
}
lblSum.Text = sum.ToString();
What do I need to do to get the value of the textbox related to the checkbox?
Based on your comments, you have a CheckBoxList with a fixed size of 10 items and along with that 10 Textboxes (named TextBox1, TextBox2 and so on). The users marks some CheckBoxes and also enters a text into the corresponding Textboxes. You want to convert the Text of the Textboxes to an integer and calculate the sum of the marked entries.
You need to dynamically access the Textboxes by calling FindControl:
var index = 0;
foreach (ListItem item in listOthers.Items)
{
index++; // Increase here as the Textbox numbers start at 1
if (item.Selected)
{
var txt = FindControl("TextBox" + index.ToString()) as TextBox;
if (txt != null)
sum += Convert.ToInt32(txt.Text); //text of selected Checkbox
}
}
lblSum.Text = sum.ToString();
Please note that it is important to call FindControl on the container of the Textboxes. The call in the sample works if the Textboxes are located directly on the page. If they are located on a Panel named Panel1, you'd have to call:
var txt = Panel1.Findcontrol("TextBox" + index.ToString()) as Textbox;
An alternative to using a CheckBoxList that is prepared for variable sizes by design and having a fixed list of 10 TextBoxes next to it, would be to create a Repeater with a CheckBox and a TextBox in its ItemTemplate. Downsize is that you'd have to create some DTOs for DataBinding and also had to use some more dynamic control resolution (both for the CheckBox and the TextBox ).
I have a windows forms DataGridView, where I have data and a checkbox for each row.
I will select check box for a particular row and all the selected rows will be populated in another page.
if (grdEmp.Rows.Count > 0)
{
var selectedEmpIDs= from DataGridViewRow coll in grdEmp.Rows
where Convert.ToBoolean(coll.Cells["Select"].Value) == true
select coll;
if (selectedEmpIDs.Count() > 0)
{
foreach (DataGridViewRow row in selectedEmpIDs)
{
selectedEmp+= row.Cells["EmpId"].Value + ",";
}
}
}
This works good only for one page.
When I navigate to another page, and click the selected rows, the previous one goes off.
How do I resolve it.
Thanks
cmrhema
Note :Sorry for the confusion, When I meant it works good for a page, I meant paging.
I think I need to add more inputs,
There are 10 pages in the gridview.
I select the first record from each page of the gridview, one after another by clicking next page( Page next button).
But only the record that was selected the last is getting displayed and others and ignored off.
What could be the prblm
You can use a List or Dictionary or any other collection type globally, using Program.cs or using a static class. And store the selected rows into the list before you leave the page.
Rather than using a comma delimited string string for your list of ids you can instead use a List.
Your code will then become something like this:
if (grdEmp.Rows.Count > 0)
{
var selectedEmpIDs= from DataGridViewRow coll in grdEmp.Rows
where Convert.ToBoolean(coll.Cells["Select"].Value) == true s
select coll;
if (selectedEmpIDs.Count() > 0)
{
foreach (DataGridViewRow row in selectedEmpIDs)
{
if (!listOfIds.Contains((int)row.Cells["EmpId"].Value))
{
listOfIds.Add(((int)row.Cells["EmpId"].Value));
}
}
}
}
You will need methods to remove items from this list so adding event handlers for the checkbox selected event will probably work better.
The List object itself can simple live as a class level object of the form that containst your DataGridView.
This gets a little bit more complicated if you are managing your paging across forms, but the same principles of maintaining a list of selected ids applies.
I want to copy listbox items from one form to another....actually in the 1st form I have 2 listboxes and in the 2nd form I also have 2 listboxes. I want to move the items of the 1st form's listboxes to 2nd form..... please help me....
If you want to select the entire items from listbox1 to listbox2, then the easiest, most readable and fastest have to be:
listbox2.Items.AddRange(listbox1.Items);
public partial class Form1 : Form
{
List<String> mylistSource;
public Form1()
{
InitializeComponent();
mylistSource = new List<string>();
// populate source with test data
for (int i = 0; i < 25; i++)
{
mylistSource.Add(i.ToString());
}
//assign source to both lists
listBox1.DataSource = mylistSource;
listBox2.DataSource = mylistSource;
}
}
Just add 2 listboxes to a form and paste in code to run.
or if you just want to copy selected items you can simply do this:
foreach (var item in listBox1.SelectedItems)
{
listBox3.Items.Add(item);
}
In case of web form, use session to forward the data source of the list.
what you could do, is give the objects an extra property (eg. selected)
you bidn the collection to both the listboxes, but in the one you only show the ones with selected = false and in the other selected = true
and if you "move" the item, you just need to switch selected to true and refresh the ItemsSources