Dropdownlist not highlighting the given value - c#

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.

Related

change background color of dropdown items at runtime on the basis of flag column of a datatable asp.net

i have a dropdown cboVendor in which supplier name is coming now
i want background colour to be red whose fullyAgg column(dt contains 11 columns in which fullagg is the 11th column) is coming as Zero.currently i am doing as shown in the below code but it is taking all of them a zero(which should not happen)
.aspx
<asp:DropDownList ID="cboVendor" runat="server" AppendDataBoundItems="True"
AutoPostBack="true"> <asp:ListItem Value="0">- Select Vendor -</asp:ListItem>
</asp:DropDownList>
c# code
DataTable dt = default(DataTable);
cboVendor.DataSource = dt;
cboVendor.DataTextField = "SupplierName";
cboVendor.DataValueField = "SupplierID";
cboVendor.DataBind();
cboVendor.SelectedIndex = 0;
foreach (ListItem item in cboVendor.Items) {
if (dt.Rows(10)("fullyAgg") == 0) {
item.Attributes.Add("style", "background-color:red;");
}
}
found the solution
DataView dv = dt.DefaultView;
dv.RowFilter = "fullyAgg=0";
foreach (DataRowView dr in dv) {
foreach (ListItem item in cboVendor.Items) {
if (dr("SupplierID").ToString() == item.Value.ToString()) {
item.Attributes.Add("style", "background-color:red;");
}
}
}
You may try this :
foreach (ListItem item in cboVendor.Items) {
//select row corresponding to current dropdown item
var selectedRow = from myRow in dt.AsEnumerable()
where myRow.Field<int>("SupplierID") == item.Value
select myRow;
//check fullyAgg column for selected row
if (selectedRow("fullyAgg") == 0) {
item.Attributes.Add("style", "background-color:red;");
}
}
Couple of issues:
if (dt.Rows(10)("fullyAgg") == 0) {
First: you make no iteration there, you are comparing the same value everytime.
Second: Rows(10) doesn't give you the 11nth column (row <> column !!!)
Third: make the foreach (a proper one) in cboVender_ItemDataBound function (which handles cboVender.ItemDataBound) so you can access the item's properties from there
Edit: how to do it? -> have you tried anything I said?
private void cboVender_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e) {
if ((e.Item.ItemType == ListItemType.Item)) {
rowIndex = e.Item.ItemIndex;
// here you have your row number, you can Access the value of that column and do whatever you want
}
}

How to skip listitem with index 0 foreach checkboxlist item

I have a CheckBoxList which I am going through all its items using:
foreach (ListItem item in this.checklist.Items)
I was wondering how I can skip the first item in this CheckBoxList(item with index 0).
Linq should help you do the trick. Make sure to cast the items collection to typed collection, and then you can use Skip to skip first item:
foreach (ListItem item in this.checklist.Items.Cast<ListItem>().Skip(1))
I believe one way to do this is:
bool isFirst = true;
foreach (ListItem item in this.checklist.Items)
{
if (isFirst) {
isFirst = false;
} else {
// do checking
}
}
Try this.
foreach (ListItem item in this.checklist.Items)
{
if (item != this.checklist.Items[0]) {
// Do something
}
}

How to traverse the list view column

How to traverse the list view column so that we can check whether item in the list view column already exist or not and if it exist then change the item .
for example i have a list view with (quantity and item) and i want to check if the newly added item already exist in the list view then only change the quantity to quantity ++ rather then adding new item.
string[] saLvwItem = new string[4];
saLvwItem[0] = a.ToString();
saLvwItem[1] = r["ItemNumber"].ToString();
saLvwItem[2] = r["ItemName"].ToString();
saLvwItem[3] = r["Price"].ToString();
ListViewItem lvi = new ListViewItem(saLvwItem);
listView1.Items.Add(lvi);
all the values are coming from database.
If you are trying to match on ItemName (columnIndex=2) and increase the ItemNumber (columnIndex=1), then this may work:
private void InsertOrUpdateItem(ListView listView, string[] saLvwItem)
{
if (saLvwItem == null || saLvwItem.Length < 4)
{
return;
}
bool bFound = false;
foreach (ListViewItem lvi in listView.Items)
{
if (lvi.SubItems[2].Text == saLvwItem[2])
{
// item already in list
// increase the ItemNumber
lvi.SubItems[1].Text = (Convert.ToInt32(lvi.SubItems[1].Text) + Convert.ToInt32(saLvwItem[1])).ToString();
bFound = true;
break;
}
}
if (!bFound)
{
// item not found
// create new item
ListViewItem newItem = new ListViewItem(saLvwItem);
listView.Items.Add(newItem);
}
}
Use following code,
if(!lvi.ContainsKey(saLvwItem[1]))
{
ListViewItem lvi = new ListViewItem(saLvwItem[2]); //Or whatever value you want to show as name
listView1.Items.Add(lvi);
}
Listview has a key-value setting for every item, so you can't set four properties. Your key should be unique and your name should be exactly what you want to display, you can make a name by concatinating 2 or more values.

Access a cell in DataGrid.SelectedItems

I need to read a string from each of the the first cells of SelectedItems of the DataGrid
foreach (var item in myDataGrid.SelectedItems)
{
if (item[0].ToString().Contains("Buy"))
{
containsBuy = true;
}
if (item[0].ToString().Contains("Sell"))
{
containsSell = true;
}
}
How can I cast myDataGrid.SelectedItems? It is IList and gives object. Is there any simple and similar way to do it as it is done for one selected row of a DataGrid:
var row = myDataGrid.SelectedItem as DataRowView;
Here I can easy access any cell - row[i].
Just change your foreach loop to:
foreach (DataRowView in myDataGrid.SelectedItems)
{
//...
}

C# CheckBox List Selected Items.Text to Labels.Text

I have a CheckBoxList and 5 labels.
I would like the text value of these Labels to be set to the 5 selections made from the CheckBoxList after the user clicks on a button. How would I get this accomplished?
Thanks in advance.
bind an event to a button,
iterate trough the Items property of the CheckBoxList
set the text value according to the selected property of the listitem
like:
protected void button_Click(object sender, EventArgs e)
{
foreach (ListItem item in theCheckBoxList.Items)
{
item.Text = item.Selected ? "Checked" : "UnChecked";
}
}
to add a value you could do:
foreach (ListItem item in theCheckBoxList.Items)
{
item.Text = item.Selected ? item.Value : "";
}
or display al values in a mini-report:
string test = "you've selected :";
foreach (ListItem item in theCheckBoxList.Items)
{
test += item.Selected ? item.Value + ", " : "";
}
labelResult.Text = test;
find selected items from CheckboxList by Lambda Linq:
var x = chkList.Items.Cast<ListItem>().Where(i => i.Selected);
if (x!=null && x.Count()>0)
{
List<ListItem> lstSelectedItems = x.ToList();
//... Other ...
}
Why don't you have one label and on the button click do something like:
foreach (var li in CheckList1.Items)
{
if(li.Checked)
Label1.Text = li.Value + "<br />";
}
That may not be the exact syntax but something along those lines.
Use this in LINQ:
foreach (var cbx3 in CheckBoxList2.Controls.OfType<CheckBox>().Where(cbx3 => cbx3.ID == s))
{
cbx3.Checked = true;
}

Categories