I have a chart in which i want to be able to add or remove lines based on a checkboxlist.
To this end i came up with this code:
foreach (ListItem lstitem in CBLYear.Items)
{
if (lstitem.Value == "2014")
{
Chart3.Series["2014"].XValueMember = "Month";
Chart3.Series["2014"].YValueMembers = "2014";
}
if (lstitem.Value == "2013")
{
Chart3.Series["2013"].XValueMember = "Month";
Chart3.Series["2013"].YValueMembers = "2013";
}
if (lstitem.Value == "2012")
{
Chart3.Series["2012"].XValueMember = "Month";
Chart3.Series["2012"].YValueMembers = "2012";
}
if (lstitem.Value == "2011")
{
Chart3.Series["2011"].XValueMember = "Month";
Chart3.Series["2011"].YValueMembers = "2011";
}
if (lstitem.Value == "2010")
{
Chart3.Series["2010"].XValueMember = "Month";
Chart3.Series["2010"].YValueMembers = "2010";
}
if (lstitem.Value == "2009")
{
Chart3.Series["2009"].XValueMember = "Month";
Chart3.Series["2009"].YValueMembers = "2009";
}
}
On page load nothing shows up on the graph (as wanted). However once click any one of the items all the series show up not just the one i clicked why is that?
you need to use else if from 2nd condition, you should use switch statement as in your code you are checking many conditions, in switch it will execute only the particular case
switch(Convert.ToString(lstitem.Value))
{
case"":
{
// put all your conditions in cases
}
break;
default :
break;
}
You are iterating over the entire items collection, so of course each series will show up.
You need to check the "Selected" property instead. You can do this easily using LINQ:
foreach (ListItem lstitem in CBLYear.Items.Where(l => l.Selected))
{
....
}
See the MSDN Documentation: CheckBoxList (Specifically the "Introduction" section)
Related
I just wanna ask if there is someone here that have already made a breadcrumbs in Sitecore. I'm currently doing a Sitecore 8 MVC project that needs to have a breadcrumbs.
Currently I saw this website http://blog.ryanbailey.co.nz/2015/05/breadcrumbs-for-pages-in-sitecore.html. But It doesn't work for me yet because I don't know what to reference.
I just need to know how to get every item in the path of my current page I can handle it already.
Thanks
Something like this should do it:
public ICollection<Item> GetBreadcrumbs(Item current, SiteContext site)
{
Item homeItem = site.StartItem;
List<Item> breadcrumbs = new List<Item>();
while (current != null)
{
// You may want to add additional logic to opt in/out of
// the breadcrumb based on a template/field
breadcrumbs.Add(current);
if (current == homeItem)
break;
current = current.Parent;
}
breadcrumbs.Reverse();
return breadcrumbs;
}
And then:
var breadcrumbs = GetBreadcrumbs(Context.Item, Context.Site);
You can take the current item and then take all the ancestors of it.
var ancestors = currentItem.Axes.GetAncestors().ToList();
Then you can get home item and filter the ancestors to remove sitecore and content nodes.
ancestors = ancestors.SkipWhile(i => i.ID != home.Id.ToID()).ToList();
public void GetBreadcrumbs(Item ParentItem)
{
List<BredCrumbDetails> lstBreadCrumbs = new List<BredCrumbDetails>();
string strcurrenttitle = ParentItem.Name;
Item currentitem = ParentItem;
int i = 0;
while (currentitem != null)
{
var ItemTemplateid = currentitem.TemplateID.ToString();
var FolderTemplateId = "{}";
if (ItemTemplateid != FolderTemplateId) //here we are removing folders
{
BredCrumbDetails bcDetails = new BredCrumbDetails();
if (i == 0)
{
bcDetails.BCPageLink = null;
bcDetails.Title = currentitem.Name;
bcDetails.IsVisible = true;
lstBreadCrumbs.Add(bcDetails);
}
else
{
bcDetails.BCPageLink = currentitem.Paths.FullPath;
bcDetails.Title = currentitem.Name;
bcDetails.IsVisible = true;
lstBreadCrumbs.Add(bcDetails);
}
i++;
if (currentitem.Name == ("Home"))
{
break;
}
currentitem = currentitem.Parent;
}
else
{
i++;
currentitem = currentitem.Parent;
}
}
lstBreadCrumbs.Reverse();
rptCrumbs.DataSource = lstBreadCrumbs;
rptCrumbs.DataBind();
}
I made a code that check if in my DataGrid a particular element is already exists.
If the elements is already added in the DataGrid the code show a pop-up to the user. This is the code:
if (grid.Items.Count > 0)
{
for (int i = 0; i < grid.Items.Count; i++)
{
if (((Teams.Club_Information)grid.Items[i]).name == reader["name"].ToString())
{
MessageBox.Show("La squadra è già stata inserita!");
}
else
{
MainWindow.AppWindow.Squadre_DataGrid.Items.Add(new Teams.Club_Information
{
name = reader["name"].ToString(),
code = reader["code"].ToString(),
shortName = reader["shortName"].ToString(),
squadMarketValue = reader["SquadMarketValue"].ToString()
});
}
}
}
else
{
MainWindow.AppWindow.Squadre_DataGrid.Items.Add(new Teams.Club_Information
{
name = reader["name"].ToString(),
code = reader["code"].ToString(),
shortName = reader["shortName"].ToString(),
squadMarketValue = reader["SquadMarketValue"].ToString()
});
}
In the first condition I check if there's row in the DataGrid, if the condition is true then I iterate through the element of DataGrid.
Now the problem is that I'm compare a grid item with the attribute "name" of the reader. reader is the variable the read of the element of a query result (sqlite).
There's another way to check in the column name of my DataGrid if the element is already added?
Your items are of type "Teams.Club_Information", but you are comparing them to reader["name"] which is a string. Unless you have written an Equals method which will compare Club_Information.name to a string, the comparison will always fail.
Try instead:
if (((Teams.Club_Information)grid.Items[i]).name == reader["name"])
You can do something like this
if(grid.Items.Contains(reader["name"]))
{
MessageBox.Show("team already added");
}
else
{
MainWindow.AppWindow.Squadre_DataGrid.Items.Add(new Teams.Club_Information
{
name = reader["name"].ToString(),
code = reader["code"].ToString(),
shortName = reader["shortName"].ToString(),
squadMarketValue = reader["SquadMarketValue"].ToString()
});
}
var name = reader["name"];
var isNamePresent = grid.Items
.Cast<Teams.Club_Information>()
.Any(item => item.name == name);
if (isNamePresent) ... else ...
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 have two tables Rule and RuleCondition (one -> many).
people can add conditions at any time.
Suppose initially he adds two conditions.
He can comeback and add another condition also can update the conditions that are already added.
I am able to save the updated conditions, but not able to insert the extra condition he added.
Below is my code, and it is failing at
rule.RuleConditions.Add(oRuleCon); -- Entity set was modified during enumaration
If I use the approach
oAngieCtxt.RuleConditions.InsertOnSubmit(oRuleCon);
it is not at all inserting the data.
can somebody advise how to handle?
public ActionResult saveMetricRule(Rule rule)
{
bool IsNew = rule.RuleId == 0;
using (NewAngieDataContext oAngieCtxt = new NewAngieDataContext(new CSConfigurationMgr().GetConnectionString(ConnectionStringKey.Angie)))
{
if (IsNew)
oAngieCtxt.Rules.InsertOnSubmit(rule);
else
{
RuleCondition oRuleCon = null;
foreach (RuleCondition childItem in rule.RuleConditions)
{
if (childItem.RuleConditionId == 0)
{
oRuleCon = new RuleCondition();
oRuleCon.Points = childItem.Points;
oRuleCon.ConditionValue = childItem.ConditionValue;
oRuleCon.ToOperatorId = childItem.ToOperatorId;
oRuleCon.Sort = childItem.Sort;
rule.RuleConditions.Add(oRuleCon);
// oAngieCtxt.RuleConditions.InsertOnSubmit(oRuleCon);
}
else
{
oRuleCon =
oAngieCtxt.RuleConditions
.Where(CON => CON.RuleConditionId == childItem.RuleConditionId)
.FirstOrDefault();
oRuleCon.Points = childItem.Points;
oRuleCon.ConditionValue = childItem.ConditionValue;
oRuleCon.ToOperatorId = childItem.ToOperatorId;
oRuleCon.Sort = childItem.Sort;
}
}
oAngieCtxt.Rules.Attach(rule);
oAngieCtxt.Refresh(RefreshMode.KeepCurrentValues, rule);
}
oAngieCtxt.SubmitChanges();
}
return this.Json(new
{
msg = "Successful save.",
ruleId = rule.RuleId
});
}
You can't add an item to a list that you're enumerating over. Since you're looping over the rule.RuleConditions, you can't add to it inside the foreach loop. Instead, you can add to a temporary list and then add all items from that list to the rule.RuleConditions after the foreach.
var newRuleConditions = new List<RuleCondition>();
foreach (RuleCondition childItem in rule.RuleConditions)
{
if (childItem.RuleConditionId == 0)
{
oRuleCon = new RuleCondition();
oRuleCon.Points = childItem.Points;
oRuleCon.ConditionValue = childItem.ConditionValue;
oRuleCon.ToOperatorId = childItem.ToOperatorId;
oRuleCon.Sort = childItem.Sort;
//add to temporary list
newRuleConditions.Add(oRuleCon);
oAngieCtxt.RuleConditions.InsertOnSubmit(oRuleCon);
}
else
{
...
}
}
//add all new rule conditions
rule.RuleConditions.AddRange(newRuleConditions);
I've wrote a method to search thru ListView for a given string and mark the ones that are found with Color. It works fine however with lots of information on screen and scrollable ListView it's sometimes hard to find what user is looking for.
Normally I do create special searches by modifying method and SQL query WHERE clause but it's always a pain and requires more work/code for each ListView/Data.
I would like to have some generalized search that would work for all kind of searches in ListView just like one I have now but with ability to hide (rows) what's not needed and show only necessary rows. Of course if one changes it's mind it has to bring back the old rows back.
I guess the biggest problem for me is how to store all the columns and data without over complicating knowing that it can be 3 to 20+ columns and multiple rows.
public static void wyszukajNazweListView(ListView varListView, string varWyszukaj) {
if (varWyszukaj != "") {
foreach (ListViewItem comp in varListView.Items) {
comp.UseItemStyleForSubItems = false;
foreach (ListViewItem.ListViewSubItem drv in comp.SubItems) {
string textToAdd2 = drv.Text;
if (textToAdd2.Length >= 1) {
if (textToAdd2.ToLower().Contains(varWyszukaj.ToLower())) {
drv.BackColor = Color.DarkOrange;
} else {
drv.BackColor = Color.White;
}
}
}
bool varColor = false;
foreach (ListViewItem.ListViewSubItem drv in comp.SubItems) {
if (drv.BackColor == Color.DarkOrange) {
varColor = true;
break;
}
}
if (varListView.SmallImageList != null) {
if (varColor) {
comp.ImageIndex = 2;
} else {
comp.ImageIndex = -1;
}
}
}
} else {
foreach (ListViewItem comp in varListView.Items) {
comp.UseItemStyleForSubItems = false;
comp.BackColor = Color.White;
foreach (ListViewItem.ListViewSubItem drv in comp.SubItems) {
drv.BackColor = Color.White;
comp.ImageIndex = -1;
}
}
}
}
I'd probably store it as a DataTable object. DataTable type allows setting its rows as hidden (e.g. Visible = false) and you can bind your ListView directly to it.
EDIT: noticed the WinForms tag. Even simpler: no need to mock about with ViewState/Session.