Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I use this code:
int monthIndex = dateTimePicker1.Value.Month;
but when I look in breakpoint I am only able to get 0 from my dateTimePicker.

What is wrong?

This is my code. When I select November 2011 in dateTimePicker, I would like the date range for november to show up on the x-axis of my chart.

C#
        private string myAreaName(int i)
        {
            if (i == 1) return "January";
            else if (i == 2) return "February";
            else if (i == 3) return "March";
            else if (i == 4) return "April";
            else if (i == 5) return "May";
            else if (i == 6) return "June";
            else if (i == 7) return "July";
            else if (i == 8) return "August";
            else if (i == 9) return "September";
            else if (i == 10) return "October";
            else if (i == 11) return "November";
            else return "December";
        }

private void makeChart()
        {
            string year = dateTimePicker1.Value.Year.ToString().Trim();
            monthIndex = dateTimePicker1.Value.Month;
            ChartArea chartArea2 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            chartArea2.Name = myAreaName(monthIndex);

            List<string> ls = new List<string>();

            foreach (string drv in checkedListBox1.CheckedItems)
            {
                ls.Add(drv.Trim());
            }

            Ship = ls.ToArray();

            chart1.Series.Clear();
            chart1.Titles.Clear();
            chart1.ChartAreas.Clear();

            chart1.ChartAreas.Add(myAreaName(monthIndex));
            chart1.Titles.Add(myAreaName(monthIndex));
            chart1.Titles["Title1"].Name = myAreaName(monthIndex);
            chart1.Titles[myAreaName(monthIndex)].Text = myAreaName(monthIndex) + " " + dateTimePicker1.Value.Year;
            chart1.Titles[myAreaName(monthIndex)].DockedToChartArea = myAreaName(monthIndex);
            chart1.Titles[myAreaName(monthIndex)].IsDockedInsideChartArea = false;
            chart1.Titles[myAreaName(monthIndex)].Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Top;
            chart1.Titles[myAreaName(monthIndex)].DockingOffset = 0;
            chart1.Titles[myAreaName(monthIndex)].BackColor = Color.SeaShell;

            foreach (string row in Ship)
            {
                System.Windows.Forms.DataVisualization.Charting.Series AnnualSeries = new System.Windows.Forms.DataVisualization.Charting.Series();
                Ship_id = row.Substring(0, row.IndexOf(" "));
                Ship_name = row.Substring(row.IndexOf(" ") + 1, row.Length - 1 - row.IndexOf(" "));
                SeriesName = Ship_name;// +" " + monthIndex.ToString();
                AnnualSeries.Name = SeriesName;
                AnnualSeries.ChartArea = myAreaName(monthIndex);
                chart1.Series.Add(AnnualSeries);
                AnnualSeries.IsValueShownAsLabel = true;
                chart1.Series[SeriesName].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
                chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Days;
                chart1.Series[SeriesName].MarkerSize = 10;
                chart1.Series[SeriesName].MarkerStyle = myMarkerStyle(chart1.Series.IndexOf(SeriesName));
                chart1.Series[SeriesName].EmptyPointStyle.IsValueShownAsLabel = false;
                chart1.Series[SeriesName].EmptyPointStyle.MarkerSize = 1;
                chart1.Series[SeriesName].Color = mySeriesColor(Ship_id);
            }

            SqlConnection sqlbA = new SqlConnection(connectionString);
            sqlbA.Open();
            SqlDataAdapter dabA = new SqlDataAdapter();
            DataSet dsbA = new DataSet();

            foreach (string row in Ship)
            {
                Ship_id = row.Substring(0, row.IndexOf(" "));
                Ship_name = row.Substring(row.IndexOf(" ") + 1, row.Length - 1 - row.IndexOf(" "));
                SeriesName = Ship_name;// + " " + monthIndex.ToString();
                int DaysInMonth = DateTime.DaysInMonth(Convert.ToInt16(year), monthIndex);

                dabA.SelectCommand = new SqlCommand(
                 "SELECT ship_id, [dayOfMonth_a] AS s_day, [avg_cons] AS AVG_cons FROM dbo.[dayAvgCons] " +
                 "WHERE ( ship_id = " + Ship_id +
                 " AND [avg_cons] != 0 " +
                 " AND [month_a] = " + monthIndex.ToString() +
                 " AND [year_a] = " + year +
                 ") ORDER BY s_day"
                  , sqlbA);
                dsbA.Clear();
                dabA.Fill(dsbA, "AVG");
                //AcurrSeries = SeriesName;
                foreach (DataRow rowc in dsbA.Tables["AVG"].Rows)
                {
                    if (Convert.ToInt16(rowc["AVG_cons"]) > 60)
                    {
                        chart1.Series[SeriesName].Points.AddXY(Convert.ToInt32(rowc["s_day"]), Convert.ToInt32(rowc["AVG_cons"]));
                    }
                }
            }//End foreach
        }
Posted
Updated 14-Nov-13 2:07am
v2
Comments
Fredrik Bornander 13-Nov-13 9:25am    
What, according to your debugger, is the value of dateTimePicker1.Value?

1 solution

Your code is valid in WinForms; if you have not set the Value of the DateTimePicker at design-time, or run-time, then its value at run-time should be the ordinal number of the current month.

Where are you setting your break-point in your code ? Have you changed the values of the DateTimePicker's MaxDate, and MinDate Properties at design-time ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900