i have 4 dropdown list in my form, but when i want to set the selected value the first 2 ddl, always follow value the next 2 ddl.
aspx.cs
if(!Page.IsPostBack)
{
for (int hours = 0; hours <= 23; hours++)
{
ListItem li = new ListItem();
li.Text = hours.ToString();
li.Value = hours.ToString();
ddlDepartHours.Items.Add(li);
ddlArrivalHours.Items.Add(li);
}
for (int mins = 0; mins <= 55; mins+=5 )
{
ListItem li = new ListItem();
li.Text = mins.ToString();
li.Value = mins.ToString();
ddlDepartMins.Items.Add(li);
ddlArrivalMins.Items.Add(li);
}
ddlDepartHours.SelectedValue = "1";
ddlDepartMins.SelectedValue = "5";
ddlArrivalHours.SelectedValue = "10";
ddlArrivalMins.SelectedValue = "50";
}
this is the link of the result : http://imageshack.us/photo/my-images/69/ibte.png/
Note that you are inserting the same objects as list items for both "hours" as well as both "minutes" DDLs. Later on when you are setting the selected value of the one of them, effectively you are setting corresponding ListItem object as selected, which of course affects one DDL in which this object is also added.
Solution here is to insert different objects in different, though similar, dropdowns:
ddlDepartHours.Items.Add(new ListItem(hours.ToString(), hours.ToString()));
ddlArrivalHours.Items.Add(new ListItem(hours.ToString(), hours.ToString()));
The same for the minutes:
ddlDepartMins.Items.Add(new ListItem(mins.ToString(), mins.ToString()));
ddlArrivalMins.Items.Add(new ListItem(mins.ToString(), mins.ToString()));
Try to change the name of the object in the second ListItem li = new ListItem();
Add items like this
ddlDepartHours.Items.Add(new ListItem(hours.ToString(), hours.ToString()));
ddlArrivalHours.Items.Add(new ListItem(hours.ToString(), hours.ToString()));
ddlDepartMins.Items.Add(new ListItem(mins.ToString(), mins.ToString()));
ddlArrivalMins.Items.Add(new ListItem(mins.ToString(), mins.ToString()))
Related
So this is a hard one, i am doing a mangment software for a warehouse and i save the location of items on the shelves as plain number in my database, ie i have column row,shelf and ledge, and each one is a number (the total is of each is determined first) to say i have 11 rows, 5 shelves and each shelf has 5 ledges.
now my problem is i locate an item in row 1 shelf 1 and ledge 1, all good now what i want to do is for the program to detect that there is already an item in that location and when i try to save another item it just disables out or just removes that option in the combobox.
here is how i load the comboboxes
dgvAux.DataSource = datos.consultar("select top 1 rows from warehouse");
int rows= Convert.ToInt32(dgvAux.Rows[0].Cells[0].Value.ToString());
for (int i = 1; i < rows+ 1; i++)
{
ComboboxItem item = new ComboboxItem();
item.Text = "Row " + i.ToString();
item.Value = i;
cboRow.Items.Add(item);
cboRow.SelectedIndex = 0;
}
dgvAux.DataSource = datos.consultar("select top 1 shelves from warehouse");
int shelves= Convert.ToInt32(dgvAux.Rows[0].Cells[0].Value.ToString());
for (int i = 1; i < shelves+ 1; i++)
{
ComboboxItem item = new ComboboxItem();
item.Text = "Shelf" + i.ToString();
item.Value = i;
cboShelves.Items.Add(item);
cboShelves.SelectedIndex = 0;
}
dgvAux.DataSource = datos.consultar("select top 1 ledges from almacenes");
int ledges= Convert.ToInt32(dgvAux.Rows[0].Cells[0].Value.ToString());
for (int i = 1; i < ledges+ 1; i++)
{
ComboboxItem item = new ComboboxItem();
item.Text = "Ledge" + i.ToString();
item.Value = i;
cboLedge.Items.Add(item);
cboLedge.SelectedIndex = 0;
}
i know its not the best way to do it, but is the way i was able to make it work
You could have an event handler on each time a combobox is changed that will check the availability of all options in further comboboxesLike so if you choose a shelf number, a check is done for all the available space in that shelf and will remove the irrelevant or already full spaces (if statement or switch case to add and remove items from comboboxes)
As guys are pointing out in the comments, it is not a good design to have disabled items in combo list.
However there is property IsEnabled which you need to set to False and that's it. You need to have algorithm to decide which one to set to disable.
i am new in programming
i have 6 drop down in one page 1 want them to make them read only (non editable)
if value is selected
and rest of the drop down should not be read only if value is selected
is there any way i can check all the Drop down in one condition/check(at one go)
thanks in advance
1# Find all dropdownlists
Dim allDDs = From dd As DropDownList In Me.Controls() Where TypeOf (dd) Is
DropDownList Select dd
2# disbale the where value selected and enable others
For Each d In allDDs
d.Enabled = d.SelectedValue = ""
Next
say ddl1, ddl2, ddl3, ddl4, ddl5, ddl6 are ids of 6 dropdowns in your page.
for (int count = 1; count <= 6; count++)
{
DropDownList ddl = new DropDownList();
ddl.ID = "ddl" + count;
if (ddl.SelectedIndex > -1)
{
ddl.Enabled = false;
}
else
{
ddl.Enabled = true;
}
}
I have a listbox that is bound to a list of objects from the database. I have a secondary list that has less objects that I want to use it to mark as selected elements.
cell = new HtmlTableCell();
List<ClasaAutor> listaAutori = DataTableToClasaAutor(dal.CitesteTotiAutori());
List<ClasaAutor> listaAutoriPublicatie = DataTableToClasaAutor(dal.CitesteTotiAutoriUneiPublicatii(guidPublicatie));
ListBox list = new ListBox();
list.SelectionMode = ListSelectionMode.Multiple;
list.ID = "cbAutori";
list.DataSource = listaAutori;
list.DataTextField = "NumeComplet";
list.DataValueField = "GuidAutor";
list.DataBind();
foreach (ClasaAutor autor in listaAutoriPublicatie)
{
for (int i = 0; i < list.Items.Count; i++)
{
if (list.Items[i].Value == autor.GuidAutor.ToString())
list.SelectedIndex = i;
}
}
cell.Controls.Add(list);
row.Cells.Add(cell);
The problem is that only my last element gets selected... why? How can I fix it?
My if is ok, it gets true 2 times...
Try this loop:
foreach (ClasaAutor autor in listaAutoriPublicatie)
{
foreach (ListItem item in list.Items)
{
if (item.Value == autor.GuidAutor.ToString())
item.Selected = true;
}
}
the problem in semantics, SelectedIndex of list can hold only one value, this is not collection
however you could make list item selected by setting the Selected value of it to true
list.Items[i].Selected = list.Items[i].Value == autor.GuidAutor.ToString();
We are currently programming a student information system. What we have so far is a checkboxlist that contains all the modules for the course that the selected student is enrolled on. These values are both stored in the database. What we are trying to do is have the List item values within the checkboxlist selected/ticked based upon the modules they are currently studying, based on what is stored in the database.
Here is our c# code:
string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
myConnection.Open();
string com5 = "SELECT ModuleID FROM StudentModules WHERE StudentID = #StudentID";
SqlCommand myCommand5 = new SqlCommand(com5, myConnection);
StudentIDLabel.Text = SearchText.Text;
int StudentIDint = Convert.ToInt32(StudentIDLabel.Text);
myCommand5.Parameters.AddWithValue("#StudentID", StudentIDint);
string compareModules = myCommand5.ExecuteScalar().ToString();
var listOfStrings = new List<string>();
SqlDataReader reader = myCommand5.ExecuteReader();
while (reader.Read())
{
String currentValue = reader.ToString();
listOfStrings.Add(currentValue);
}
string[] arrayOfStrings = listOfStrings.ToArray();
foreach (ListItem li in this.CbOptional.Items)
{
for (int x = 0; x < arrayOfStrings.Length; x++)
{
if (li.Value.ToString() == arrayOfStrings[x])
{
li.Selected = true;
}
else
{
li.Selected = false;
}
}
}
From what we can see there seems to be an issue with IF statement, when comparing values from the checkbox list with values from the array.
We tested the else clause near the bottom of the above code, with "li.Selected = true" to make sure the code was running through the foreach statement correctly. All items within the checkbox list displayed as ticked. So this leads us to believe there is definitely something wrong with the IF statement inside the FOR loop.
Any ideas on this would be greatly appreciated.
Thanks
You dont need to run two for loops. You can just iterate through array and check if there is list item which is having same value as array element on specific index.
Try this.
for (int x = 0; x < arrayOfStrings.Length; x++)
{
ListItem listItemtoSelect = this.CbOptional.Items.FindByValue(arrayOfStrings[x]);
if (listItemtoSelect != null)
{
listItemtoSelect.Selected = true;
}
}
Since the List<string> will compare with the text only not value you should use this
repace li.Value with li.Text
for (int x = 0; x < arrayOfStrings.Length; x++)
{
if (li.Text.ToString() == arrayOfStrings[x])
{
li.Selected = true;
}
else
{
li.Selected = false;
}
}
In the inner loop, you are comparing the value of ListItem variable with each element in the array. Therefore, if the first element matches, the value is overwritten by the value of comparison with second element, and so on. Instead, you could try to check if the value of that particular ListItem matches any value in the list of strings retrieved from DB. You can do this like so:
if(listOfStrings.Contains(li.Value.ToString()))
{
li.Selected = true;
}
Note that you do not need to convert the listOfStrings variable to an array, the Contains method works on Lists.
I have for example this values in datagridView : 1;2;3;4;
I want display this values in combobox like this : 1
2
3
4
My code show only last value in combobox: 4
My code of showing :
string cmbValue = CmbText;
string[] cmb = cmbValue.Split(new[] { ';' },StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < cmb.Length; i++)
{
comboBox1.Text = cmb[i];
}
and here is my code of creating array of combobox and calling method to change cell in dataGrid:
string cmbText = comboBox1.Text;
string[] cmb = new string[] { cmbText};
frm1.ChangeCellCmb(2, cmb);
this.Dispose();
Someone know how to do this ? I cant simply set collection of values combobox, because values of combobox are reading from datagrid and it's reading from DB.
Many thanks.
You need to loop and add the required items as follows:
for (int i = 0; i < cmb.Length; i++)
comboBox1.Items.Add(cmb[i]);
This will add all the required items to the drop down menu. To select/display '4' the 3rd entry in cmb by default do
comboBox1.SelectedIndex = 3;
or
comboBox1.SelectedItem = "4";
I hope this helps.