I need a way to reorder this DropDownList so that...
ListItem lioak = new ListItem("Oak Pre-finished", "pre-finished oak");
...will show first.
foreach (DataRow r in ds.Tables[0].Rows)
{
if (r[0].ToString().ToLower() == "oak")
{
ListItem lioak = new ListItem("Oak Pre-finished", "pre-finished oak");
dd1Finish.Items.Add(lioak);
}
if (r[0].ToString().ToLower() == "white")
{
ListItem lioak = new ListItem("White pre-finished", "white");
dd2Finish.Items.Add(lioak);
}
if (r[1].ToString().ToLower() == "unfinished")
{
ListItem lioak = new ListItem("Oak Unfinished", "unfinished");
dd3Finish.Items.Add(lioak);
}
}
Thanks for any help in advance.
If you just want the "Oak Pre-finished" item at the start of the list you could try Insert -
ListItem lioak = new ListItem("Oak Pre-finished", "pre-finished oak");
dd1Finish.Items.Insert(0,lioak);
Related
i have a dropdown list which i want to highlight a item from it. i have given the condition correct as par to me.But it didnt highlight the given item,instead showing normally as other items.
DataTable dtt = new DataTable();
dtt.Load(cmd.ExecuteReader());
ddlCompanyName.DataSource = dtt;
ddlCompanyName.DataTextField = "COMPANYNAME";
ddlCompanyName.DataValueField = "COMPANYID";
foreach (ListItem item in ddlCompanyName.Items)
{
if (item.Text == compidd)
{
item.Attributes.Add("style", "background-color:#3399FF;color:white;font-weight:bold;");
}
}
ddlCompanyName.DataBind();
ddlCompanyName.Items.Insert(0, new ListItem("--Select Name--"));
Compidd(string) has specified item to be highlighted in dropdownlist
You need to do DataBind before the loop:
ddlCompanyName.DataBind();
foreach (ListItem item in ddlCompanyName.Items)
{
if (item.Text == compidd)
{
item.Attributes.Add("style", "background-color:#3399FF;color:white;font-weight:bold;");
}
}
EDIT:
To set the value as default value you can try like this
ddlCompanyName.SelectedValue = "The value which you want to set as default"
The ddlCompanyName.DataBind(); must be executed before you loop the items:
ddlCompanyName.DataBind();
foreach (ListItem item in ddlCompanyName.Items)
{
if (item.Text == compidd)
{
item.Attributes.Add("style", "background-color:#3399FF;color:white;font-weight:bold;");
}
}
Otherwise there are no items in the DropDownList.
I am attempting to retrieve a ListItemCollection from a List of controls. This List contains many controls of different types - DropDownList, Label, TextBox.
I would like to retrieve all ListItem from all the DropDownList controls contained in the original List of controls.
My thought process so far has been to extract all of the DropDownList controls into a new list, and iterate though this to pull out each ListItem - however, every DropDownList control is coming up with 0 items
ControlCollection cList = pnlContent.Controls;
List<DropDownList> ddlList = new List<DropDownList>();
foreach (Control c in cList)
{
if (c.GetType() == new DropDownList().GetType())
{
ddlList.Add((DropDownList)c);
}
}
ListItemCollection itemCollection = new ListItemCollection();
foreach (DropDownList ddl in ddlList)
{
foreach(ListItem li in ddl.Items)
{
itemCollection.Add(li);
}
}
I'm sure this is the wrong (and massively inefficient) way of doing this. Any help would be appreciated.
This will do:
public IEnumerable<ListItem> GetListItems(ControlCollection controls)
{
return controls.OfType<DropDownList>().SelectMany(c => c.Items.OfType<ListItem>());
}
I currently do not have an installation where I can test this, but as a line of thought I would use Linq to do this.
Here is an example that you should be able to use;
var type = new DropDownList().GetType();
var listOfControl = from c in pnlContent.Controls
where c.GetType() == type
select ((DropDownList)c).Items;
Try this:
var ddlList = cList.OfType<DropDownList>();
ListItemCollection itemCollection = new ListItemCollection();
// option 1
var temp = ddlList.Select(ddl => ddl.Items.Cast<ListItem>()).SelectMany(li => li).ToArray();
itemCollection.AddRange(temp);
// or option 2
var temp = ddlList.Select(ddl => ddl.Items.Cast<ListItem>()).SelectMany(li => li);
foreach (var listItem in temp)
{
itemCollection.Add(listItem);
}
comboBox1.Items.Add(button1);
comboBox1.Items.Add(button2);
comboBox1.Items.Add(dateTimePicker1);
comboBox1.Items.Add(checkBox1);
comboBox2.Items.Add(button3);
comboBox2.Items.Add(button4);
comboBox2.Items.Add(dateTimePicker2);
comboBox2.Items.Add(checkBox2);
comboBox3.Items.Add(button5);
comboBox3.Items.Add(dateTimePicker3);
comboBox3.Items.Add(checkBox3);
comboBox3.Items.Add(checkBox4);
List<ComboBox> ddlList = new List<ComboBox>();
foreach (Control c in panel1.Controls)
{
if (c is ComboBox)
{
ddlList.Add((ComboBox)c);
}
}
List<Control> itemCollection = new List<Control>();
foreach (ComboBox ddl in ddlList)
{
foreach (var li in ddl.Items)
{
itemCollection.Add((Control)li);
}
}
I'm trying to do a logic here where by if an item from db exists in part of the dropdownlist ListItem it will have that item selected, else it will display the new item in a textbox and have the "Others" selected in the dropdownlist.
This is what I have so far
string gameData = readGame["gTitle"].ToString();
string gameTitle = ddlgameTile.Items.ToString();
if (printHouseData == gameTitle)
{
ddlgameTile.SelectedIndex = ddlgameTile.Items.IndexOf(ddlgameTile.Items.FindByValue(gameData));
}
else
{
txtNewGame.Text = readGame["gTitle"].ToString();
ddlgameTile.SelectedIndex = ddlgameTile.Items.IndexOf(ddlgameTile.Items.FindByValue("Others"));
}
I tried using Foreach loop and for loop, it still would not work (properly). It only gets the if-else logic by the last ListItem which is the "Others".
Assuming that gameData is the db item you want to select if it does exist, you can use ListItemCollection.FindByValue to get the item or null if it does not exist. Then you can set DropDownList.SelectedValue to select it:
string selectedValue = "Others";
if(ddlgameTile.Items.FindByValue(gameData) != null)
selectedValue = gameData;
ddlgameTile.SelectedValue = selectedValue;
However, if you have set the DataValueField and DataTextField you have to use FindByText.
How about something like this?
var compareTo = new ListItem("Title","Value");
if (ddl.Items.Contains(compareTo))
{
var selectedIndex = ddl.Items.IndexOf(compareTo);
}
else
{
var selectedIndex =
ddl.Items.IndexOf(new ListItem { Value = "Others", Text = "Others" });
}
Assuming I have the context of what you're trying to achieve right, try this:
foreach (string gameTitle in ddlgameTile.Items)
{
if (printHouseData == gameTitle)
{
ddlgameTile.SelectedIndex = ddlgameTile.Items.IndexOf(ddlgameTile.Items.FindByValue(gameData));
}
else
{
txtNewGame.Text = readGame["gTitle"].ToString();
ddlgameTile.SelectedIndex = ddlgameTile.Items.IndexOf(ddlgameTile.Items.FindByValue("Others"));
}
}
I binded my checkboxlist by:
string countrySQL = "Select id, CurrencyName From VirtualAccount_Currency";
string[] param = { };
object[] paramVal = { };
return ClassDBQuery.ExecDataReader(countrySQL, param, paramVal);
datalayer:
string selCurrSQL = "SELECT * FROM VirtualAccount WHERE MerchantMasterID = #id";
string[] param = { "#id" };
object[] paramVal = { currID };
return ClassDBQuery.ExecDataReader(selCurrSQL, param, paramVal);
here is my code:
DataTable currDT = new DataTable();
currDT = ClassView.SelectCurrency(idses);
foreach (DataRow row in currDT.Rows)
{
foreach (ListItem item in currencyBox.Items)
{
if (item.Value == (row["CurrencyID"].ToString()))
{
item.Selected = true;
break;
}
}
}
Now, when i run my code, there is no error encountered but the values of the checkbox are displayed. When I am debugging it, the system only loops in the first foreach the enters the second foreach but doesn't go through the if statement...
What's wrong with my code..?
thank you...
try below,
foreach (DataRow row in currDT.Rows)
{
foreach (ListItem item in currencyBox.Items)
{
// check here what you get
int currencyId= row.Field<int>("CurrencyID");
if (item.Value == currencyId.ToString())
{
item.Selected = true;
break;
}
}
}
if your field CurrencyID can have null values then,
int? currencyId= row.Field<int?>("CurrencyID");
if (currencyId! =null && item.Value == currencyId.ToString())
{
item.Selected = true;
break;
}
Try This:-
for (int j = 0; j < CheckBoxList1.Items.Count; j++)
{
if (CheckBoxList1.Items[j].Text.Trim() ==row["CurrencyID"].ToString())
{
CheckBoxList1.Items[j].Selected = true; break;
}
}
Hope this helps!!
What is the best way to grab the contents of a DataGridView and place those values into a list in C#?
List<MyItem> items = new List<MyItem>();
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
MyItem item = new MyItem();
foreach (DataGridViewCell dc in dr.Cells)
{
...build out MyItem....based on DataGridViewCell.OwningColumn and DataGridViewCell.Value
}
items.Add(item);
}
If you bind your list using DataSource, you can convert back by with:
List<Class> myClass = DataGridView.DataSource as List<Class>;
var Result = dataGridView1.Rows.OfType<DataGridViewRow>().Select(
r => r.Cells.OfType<DataGridViewCell>().Select(c => c.Value).ToArray()).ToList();
or to get a string dictionary of the values
var Result = dataGridView1.Rows.OfType<DataGridViewRow>().Select(
r => r.Cells.OfType<DataGridViewCell>().ToDictionary(c => dataGridView1.Columns[c.OwningColumn].HeaderText, c => (c.Value ?? "").ToString()
).ToList();
Or a linq way
var list = (from row in dataGridView1.Rows.Cast<DataGridViewRow>()
from cell in row.Cells.Cast<DataGridViewCell>()
select new
{
//project into your new class from the row and cell vars.
}).ToList();
IEnumerable.OfType<TResult> extension method can be your best friend here. Here is how I would have done it through a LINQ query:
List<MyItem> items = new List<MyItem>();
dataGridView1.Rows.OfType<DataGridViewRow>().ToList<DataGridViewRow>().ForEach(
row =>
{
foreach (DataGridViewCell cell in row.Cells)
{
//I've assumed imaginary properties ColName and ColValue in MyItem class
items.Add(new MyItem { ColName = cell.OwningColumn.Name, ColValue = cell.Value });
}
});
VB:
Dim lst As List(Of DataGridViewRow) = Me.MasterDataGridView.Rows.Cast(Of DataGridViewRow).AsEnumerable.ToList
C#:
List<DataGridViewRow> lst = this.MasterDataGridView.Rows.Cast<DataGridViewRow>.AsEnumerable.ToList;