如何获取dateTimePicker值 [英] how to get dateTimePicker value
本文介绍了如何获取dateTimePicker值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用此代码:
Hi,
I use this code:
int monthIndex = dateTimePicker1.Value.Month;
但是当我看断点我只能从我的dateTimePicker得到0。
有什么问题?
这是我的代码。当我在dateTimePicker中选择2011年11月时,我希望11月的日期范围显示在我的图表的x轴上。
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.
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
}
推荐答案
您的代码在WinForms中有效;如果你没有在设计时或运行时设置DateTimePicker的值,那么它在运行时的值应该是当前月份的序数。
您在代码中设置断点在哪里?您是否在设计时更改了DateTimePicker的MaxDate和MinDate属性的值?
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 ?
这篇关于如何获取dateTimePicker值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文