I want to dynamically change the value of the Property.settings with user scope. If I debug the values with console.printline everything seems good. But the value doesn't change.
I have a data grid where I can change the setting values. So if you write something in the row then the value should change.
IEnumerator enumerator = Properties.Settings.Default.PropertyValues.GetEnumerator();
Console.WriteLine("Itemname:: " + enumerator.ToString());
while (enumerator.MoveNext())
{
SettingsPropertyValue item = (SettingsPropertyValue)enumerator.Current;
foreach (DataGridViewRow row in dg_values.Rows)
{
if (row.Cells[0].ToString().Equals(item.Name) && row.Cells[2].Value != null)
{
item.PropertyValue = row.Cells[2].Value;
}
}
}
I think you need to save after changing the value. You will find something like this item.Save() or near to this syntax.
Related
I need to check, particular value whether available in the loaded dropdown list data source, How to do it?
Here is the code which I tried and it works fine, but is there any simple way to find it?
if (ddlcountry.Items.Contains(ddlcountry.Items.FindByValue(drJob["Country"].ToString())) == true)
{
ddlcountry.SelectedValue = drJob["Country"].ToString(); //if available it assigns the value
}
Following is much simpler:
ListItem item = ddlcountry.Items.FindByValue(drJob["Country"].ToString());
if(item != null)
ddlcountry.SelectedValue = item.Value;
I'm rather new to MVC/C# and from what I understand, foreach is read-only.
I would like there to be a method that sets the values that are null to false if that method is called.
IQueryable<CurrentMatch> FindTheMatch = (from row in db.CurrentMatches
where row.UserId.ToString() == UserIdentity
where row.MatchID == MatchIdentity
select row);
List<CurrentMatch> SetRemainingValuesToFalse = FindTheMatch.ToList();
I know that the part below wont work, it just demonstrates how I'm trying to achieve what I want to do.
foreach (var Column in SetRemainingValuesToFalse)
{
if (Column == null)
{
Column = false;
}
}
As the row has a large number of properties it wouldn't be scaleable in the future to set each property manually.
You just need to use a standard for loop instead of a foreach. You can't modify the collection inside a foreach because that is how the iterator works. You can however modify values on the objects themselves.
See also: Changing objects value in foreach loop?
I think you have this sort of the wrong way round. If you set that value to false inside any sort of loop, the context is lost when you exit that iteration of the loop.
Instead, what you probably want to do is, when consuming the list, treat nulls as false. You can use the null coalesce operator for this (??)
foreach (var row in FindTheMatch)
{
DoSomethingInterestingWith(row.Column ?? false); // pass false if Column is null.
}
for(int i=0;i<SetRemainingValuesToFalse.length;i++)
{
if (SetRemainingValuesToFalse[i] == null)
{
SetRemainingValuesToFalse[i] = false;
}
}
you are slightly misunderstanding how the foreach is working
foreach(var c in col)
reads as
While col.asEnumerable.HasValues let c = col.asEnumerable.Current
because of this you can't change either the enumerable or its current value with out breaking the loop, however if the enumerable isn't attached to the collection you are changing then you have no problems
ToList for example will clone the collection meaning the enumerable is attached to the clone not the original collection
foreach(var c in col)
col.Remove(c);
will error
foreach(var c in col.ToList())
col.Remove(c);
works fine
like wise
foreach(var c in col)
if(c.Field == null) c.Field = false;
is also fine because you are editing the the content of the current enumerable location not the location itself
however your stated desire of just replacing nulls in a collection is much simpler
col.Select(c=>c??false); //c#6
col.Select(c=>c == null? false : c); //c#<6
as you seem to be working with something akin to a datatable then you could do this
foreach(var row in table.Rows)
foreach(var col in table.Columns)
row[col] = row[col] ?? false;
The following code shows the way i created the drop downs programmatically. It works fine. But now I need to get value of a specific dropdown of a cell.
Microsoft.Office.Interop.Excel.DropDowns xlDropDowns;
Microsoft.Office.Interop.Excel.DropDown xlDropDown;
xlDropDowns = ((Microsoft.Office.Interop.Excel.DropDowns)(sheet.DropDowns(Type.Missing)));
xlDropDown = xlDropDowns.Add((double)rag.Left, (double)rag.Top, (double)rag.Width, double)rag.Height, true);
var DropDownList = {"aaaa","bbbb","cccc","dddd"};
int x = 0;
foreach (var item in DropDownList)
{
x++;
xlDropDown.AddItem(item);
}
This is how i tried to get the xlDropDown value. currentCell is the cell where i have the drop down
ColumnVal = currentCell.Text; // This didnt give any output
OR
var dd = (Microsoft.Office.Interop.Excel.DropDown)currentCell.DropDowns(Type.Missing);
I know the 2nd one is wrong, because the cell range and drop down are 2 different things. But I tried all the options, still couldnt find any solution. Someone please help me
More clearly, I want to access a specific cell(currentCell), and the xldropdown it contains and then get value from it
First you would need a reference to the drop down you've just added:
*Assuming there's only one drop down, the below would do
xlDropDown = ((Excel.DropDown)(xlDropDowns.Item(1)));
then you need to access the .get_List() property of the Excel.DropDown while making sure that something has been selected.
Example:
if (xlDropDown.Value > 0)
{
sht.get_Range("A1").Value = xlDropDown.get_List(xlDropDown.Value);
}
else
{
throw new Exception("Nothing was selected yet");
}
Identifying the dropdowns:
You could a for each loop on the xlDropDowns collection and grab the .Name and .ListIndex of each xlDropDown?
foreach (Excel.DropDown xlDD in xlDropDowns)
{
MessageBox.Show(xlDD.Name + ", " + xlDD.ListIndex);
}
I have a checkbox list filled by a list of ListItem, each ListItem having both text and a value like "8" or "5".
But I realized that a CheckBox does not have a value, its value is checked or not.
var listType = SettingsManager.Get("CRMCaseTypes");
var listStatus = SettingsManager.Get("CRMStatusReasons");
var listTypeItems = ParseSettingList(listType);
var listStatusItems = ParseSettingList(listStatus);
cblCRMType.DataSource = listTypeItems;
cblCRMType.DataBind();
cblCRMStatus.DataSource = listStatusItems;
cblCRMStatus.DataBind();
foreach (Control c in cblCRMStatus.Controls)
{
CheckBox cb = c as CheckBox;
if(cb != null && cb.(value........)
}
Is there some way I could store a value in each checkbox and use it again in code behind after the user clicks submit?
Thanks
Yes, you can. You can add a custom attribute to the CheckBox. You can use the HTML5 data attributes so your HTML will be HTML5 valid:
Set
foreach (Control c in cblCRMStatus.Controls)
{
CheckBox cb = c as CheckBox;
if(cb != null)
{
cb.Attributes.Add("data-MyField", myFieldVal);
}
}
Retrieve
foreach (Control c in cblCRMStatus.Controls)
{
CheckBox cb = c as CheckBox;
if(cb != null && cb.Attributes["data-MyField"].ToString())
{
// do something
}
}
How long do you want to re use it? If you want to store it temporarily, you can use session. If you want to store it longer, save it to a database or a file.
You could pull the value from the list that the checkboxes were bound to, referencing the relevant list item based on the index of the checkbox.
You could add it as an attribute -
SET:
myCheckBox.Attributes.Add("myKey","myValue");
GET:
var myKey = myCheckBox.Attributes["myKey"] != null ? myCheckBox.Attributes["myKey"].ToString() : "";
Is there some way I could store a value in each checkbox and use it again in code behind after the user clicks submit?
You can set it to a Session
Session["CbxList"] = YourCheckBoxList;
Then when you want to reference it just add the following:
if (Session["CbxList"] != null)
{
YourCheckBoxList = Session["CbxList"] as CheckBoxList;
}
and use.
I've included a link to sessions in case you or anyone else is not familiar with them:
http://msdn.microsoft.com/en-us/library/ms178581(v=vs.100).aspx
You need to look at the Items collection, not the Controls collection:
foreach (ListItem item in cblCRMStatus.Items)
{
string value = item.Value;
}
Although probably no longer needed by OP I'll add my answer since this thread still ranks highly in Google. If this is an ASPxCheckBox you can make use of the JSProperties dictionary to store your value(s) like so:
cb.JSProperties["cpMyValue"] = "MyValue";
I would then usually use a callback from a ASPxGridView or CallbackPanel to get this back to server side (which is slightly out of scope of the original question).
I've got a ListBox(with selectionMode=multiple) and store the selected values in a cookie.
When the user comes back I want to provide him with all the options selected again.
What I do so far: I get all the selected values and store the indices ","-separated in a string in the cookie.
When the user comes back I split the string and loop through all ListItems to select each one again!
but with:
foreach (string str in selectedStr)
{
listbox1.SelectedIndex = Int32.Parse(str);
}
I only get one (random?) selected value.
Can anyone help me to get all the selected values selected again?
maybe even a better solution?
Just try using FindByValue property of Listview as follow...
foreach (string str in selectedStr)
{
if(listbox1.Items.FindByValue(str) != null)
{
listbox1.Items.FindByValue(str).Selected = true;
}
}
You can iterate through your splitted string array, and access the ListBox.Items[] based on the index and set the Selected property to true.
foreach (string str in selectedStr)
{
listbox1.Items[Int32.Parse(str)].Selected = true;
}
Make sure that str is indeed an integer and its in range with Items.Length