带有SQLite数据的C#图表

带有SQLite数据的C#图表,c#,forms,sqlite,charts,C#,Forms,Sqlite,Charts,有很多问题与使用SQL数据的C#图表有关,但我没有发现任何与SQLite有关的问题 以前,我设法使它与SQL数据一起工作,但现在我被SQLite卡住了 下面是一些链接到组合框的变量,它们可以工作。我还创建了两个文本框,以查看它们输出的值以及它们的工作情况 SQLiteConnection con = new SQLiteConnection(DataAccess.ConString("Default")); IDbConnection cn = new SQLiteConne

有很多问题与使用SQL数据的C#图表有关,但我没有发现任何与SQLite有关的问题

以前,我设法使它与SQL数据一起工作,但现在我被SQLite卡住了

下面是一些链接到组合框的变量,它们可以工作。我还创建了两个文本框,以查看它们输出的值以及它们的工作情况

SQLiteConnection con = new SQLiteConnection(DataAccess.ConString("Default"));
IDbConnection cn = new SQLiteConnection(DataAccess.ConString("Default"));
string eID = "";
string pID = "";
string baseQuery = "SELECT [Date], [Full_Name], [Exercise_Name], [Total_Sets], [Total_Reps], [Average_Reps], [Average_Weight] FROM [Daily Progress] dp, [Person] p, [Exercise Details] ed " +
         "WHERE  (dp.[Exercise_ID] = ed.[Exercise_ID] AND dp.[Person_ID] = p.[Person_ID]) ";
string eFilter = " AND ed.[Exercise_ID] = ";
string pFilter = " AND p.[Person ID] = ";
string dateOrder = " ORDER BY [Date]";
这是我添加图表的代码

private void loadChart()
        {
            try
            {
                chart1.Titles.Clear();
                chart1.Series.Clear();

                SQLiteCommand cmd = new SQLiteCommand(baseQuery + eFilter + eID + pFilter + pID + dateOrder, con);
                con.Open();
                SQLiteDataReader dr = cmd.ExecuteReader();
                DataTable dt = new DataTable();

                dt.Load(dr);
                chart1.DataBind();
                con.Close();

                chart1.DataSource = dt;              


                cb_Total_Days.DataSource = dt;
                cb_Total_Days.DisplayMember = "Workout_Days";


                chart1.Titles.Add(cb_Workout_Name.SelectedText.ToString());
                chart1.Titles.Add(cb_Person_Name.SelectedText.ToString());
                                
                chart1.Series.Add("Total Sets");
                chart1.Series["Total Sets"].XValueMember = "Date";
                chart1.Series["Total Sets"].YValueMembers = "Total_Sets";
                chart1.Series["Total Sets"].ChartType = SeriesChartType.Column;
                chart1.Series["Total Sets"]["PixelPointWidth"] = "20";
                chart1.Series["Total Sets"].IsValueShownAsLabel = true;
                chart1.Series["Total Sets"].LabelBorderWidth = 3;
                chart1.Series["Total Sets"].SmartLabelStyle.Enabled = true;

                chart1.Series.Add("Average Reps");
                chart1.Series["Average Reps"].XValueMember = "Date";
                chart1.Series["Average Reps"].YValueMembers = "Average_Reps";
                chart1.Series["Average Reps"].ChartType = SeriesChartType.Spline;
                chart1.Series["Average Reps"].BorderWidth = 5;
                chart1.Series["Average Reps"].IsValueShownAsLabel = true;
                chart1.Series["Average Reps"].LabelBorderWidth = 3;
                chart1.Series["Average Reps"].MarkerStyle = MarkerStyle.Cross;
                chart1.Series["Average Reps"].MarkerSize = 15;
                chart1.Series["Average Reps"].SmartLabelStyle.Enabled = true;

                chart1.Series.Add("Average Weight");
                chart1.Series["Average Weight"].XValueMember = "Date";
                chart1.Series["Average Weight"].YValueMembers = "Average_Weight";
                chart1.Series["Average Weight"].ChartType = SeriesChartType.Spline;
                chart1.Series["Average Weight"].BorderWidth = 5;
                chart1.Series["Average Weight"].IsValueShownAsLabel = true;
                chart1.Series["Average Weight"].LabelBorderWidth = 3;
                chart1.Series["Average Weight"].MarkerStyle = MarkerStyle.Star10;
                chart1.Series["Average Weight"].MarkerSize = 15;
                chart1.Series["Average Weight"].SmartLabelStyle.Enabled = true;

                chart1.Series.Add("Total Reps");
                chart1.Series["Total Reps"].XValueMember = "Date";
                chart1.Series["Total Reps"].YValueMembers = "Total_Reps";
                chart1.Series["Total Reps"].ChartType = SeriesChartType.Spline;
                chart1.Series["Total Reps"].BorderWidth = 5;
                chart1.Series["Total Reps"].IsValueShownAsLabel = true;
                chart1.Series["Total Reps"].LabelBorderWidth = 3;
                chart1.Series["Total Reps"].MarkerStyle = MarkerStyle.Diamond;
                chart1.Series["Total Reps"].SmartLabelStyle.Enabled = true;

                chart1.ChartAreas[0].AxisX.LabelStyle.Format = "d-MMM";
                chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
                chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;

                chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
                chart1.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dot;

                
            }
            catch (Exception) { con.Close(); }
            finally { con.Close(); }
        }
当我运行图表时,没有显示任何错误,但图表只是保持为空,不会显示任何内容

我添加了几个复选框以实时启用该系列,并开始出现错误。这些是事件处理程序

private void chk_TotalSets_CheckedStateChanged(object sender, EventArgs e)
        {
            Series ts = chart1.Series["Total Sets"];
            ts.Enabled = chk_Total_Sets.Checked;
        }

        private void chk_TotalReps_CheckStateChanged(object sender, EventArgs e)
        {
            Series tr = chart1.Series["Total Reps"];
            tr.Enabled = chk_Total_Reps.Checked;
        }

        private void chk_AvgReps_CheckStateChanged(object sender, EventArgs e)
        {
            Series ar = chart1.Series["Average Reps"];
            ar.Enabled = chk_Avg_Reps.Checked;
        }

        private void chk_AvgWeight_CheckStateChanged(object sender, EventArgs e)
        {
            Series aw = chart1.Series["Average Weight"];
            aw.Enabled = chk_Avg_Weight.Checked;
        }
这里是错误

System.ArgumentException
  HResult=0x80070057
  Message=A chart element with the name 'Average Reps' could not be found in the 'SeriesCollection'.
  Source=System.Windows.Forms.DataVisualization
  StackTrace:
   at System.Windows.Forms.DataVisualization.Charting.ChartNamedElementCollection`1.get_Item(String name)
   at Workout_Tracker_SQLite.Visualization.chk_AvgReps_CheckStateChanged(Object sender, EventArgs e) in C:\GitHub\Workout-Tracker-SQLite\Forms\Chart_Form.cs:line 188
   at System.Windows.Forms.CheckBox.OnCheckStateChanged(EventArgs e)
   at System.Windows.Forms.CheckBox.set_CheckState(CheckState value)
   at System.Windows.Forms.CheckBox.set_Checked(Boolean value)
   at Workout_Tracker_SQLite.Visualization.btn_LoadChart_Click(Object sender, EventArgs e) in C:\GitHub\Workout-Tracker-SQLite\Forms\Chart_Form.cs:line 203
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at Workout_Tracker_SQLite.Program.Main() in C:\GitHub\Workout-Tracker-SQLite\Classes\Program.cs:line 21

  This exception was originally thrown at this call stack:
    System.Windows.Forms.DataVisualization.Charting.ChartNamedElementCollection<T>.this[string].get(string)
    Workout_Tracker_SQLite.Visualization.chk_AvgReps_CheckStateChanged(object, System.EventArgs) in Chart_Form.cs
    System.Windows.Forms.CheckBox.OnCheckStateChanged(System.EventArgs)
    System.Windows.Forms.CheckBox.CheckState.set(System.Windows.Forms.CheckState)
    System.Windows.Forms.CheckBox.Checked.set(bool)
    Workout_Tracker_SQLite.Visualization.btn_LoadChart_Click(object, System.EventArgs) in Chart_Form.cs
    System.Windows.Forms.Control.OnClick(System.EventArgs)
    System.Windows.Forms.Button.OnClick(System.EventArgs)
    System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs)
    System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message, System.Windows.Forms.MouseButtons, int)
    ...
    [Call Stack Truncated]
System.ArgumentException
HResult=0x80070057
Message=在“SeriesCollection”中找不到名为“Average Reps”的图表元素。
Source=System.Windows.Forms.DataVisualization
堆栈跟踪:
位于System.Windows.Forms.DataVisualization.Charting.ChartNamedElementCollection`1.get_项(字符串名称)
在C:\GitHub\Workout Tracker SQLite\Forms\Chart\u Form.cs中的Workout\u Tracker\u SQLite.Visualization.chk\u AvgReps\u CheckStateChanged(对象发送方,事件参数e)中
在System.Windows.Forms.CheckBox.OnCheckStateChanged(EventArgs e)中
在System.Windows.Forms.CheckBox.set_CheckState(CheckState值)中
在System.Windows.Forms.CheckBox.set\u选中(布尔值)
在C:\GitHub\Workout Tracker SQLite\Forms\Chart\u Form.cs:第203行的“训练\跟踪器\ SQLite.Visualization.btn\负荷图\点击(对象发送器,事件参数e)”
在System.Windows.Forms.Control.OnClick(EventArgs e)中
在System.Windows.Forms.Button.OnClick(EventArgs e)中
在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs-mevent)上
在System.Windows.Forms.Control.WmMouseUp(Message&m、MouseButtons按钮、Int32单击)
位于System.Windows.Forms.Control.WndProc(Message&m)
位于System.Windows.Forms.ButtonBase.WndProc(Message&m)
在System.Windows.Forms.Button.WndProc(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)中
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam)
在System.Windows.Forms.UnsafentiveMethods.DispatchMessageW(MSG&MSG)中
位于System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafentiveMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID、Int32 reason、Int32 pvLoopData)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)
在System.Windows.Forms.Application.Run(Form mainForm)中
在C:\GitHub\Workout Tracker SQLite\Classes\Program.cs中的Workout\u Tracker\SQLite.Program.Main()处:第21行
此异常最初是在此调用堆栈中引发的:
System.Windows.Forms.DataVisualization.Charting.ChartNamedElementCollection.this[string].get(string)
在Chart\u Form.cs中的Workout\u Tracker\u SQLite.Visualization.chk\u AvgReps\u CheckStateChanged(object,System.EventArgs)
System.Windows.Forms.CheckBox.OnCheckStateChanged(System.EventArgs)
System.Windows.Forms.CheckBox.CheckState.set(System.Windows.Forms.CheckState)
System.Windows.Forms.CheckBox.Checked.set(bool)
训练\跟踪器\ SQLite.Visualization.btn\加载图表\单击图表\表单.cs中的(对象、系统、事件参数)
System.Windows.Forms.Control.OnClick(System.EventArgs)
System.Windows.Forms.Button.OnClick(System.EventArgs)
System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs)
System.Windows.Forms.Control.WmMouseUp(参考System.Windows.Forms.Message,System.Windows.Forms.MouseButtons,int)
...
[调用堆栈被截断]

我发现Select查询存在语法错误。感谢@TaW的指导。

首先创建系列,然后绑定。另外:我始终建议不要绑定到图表,而是绑定到系列。点..-该表是否正确填写?
catch(Exception){con.Close();}
是一个非常糟糕的错误“处理”,因为它掩盖了所有错误,并且从不向您(或您的用户)透露以解决它。@UweKeim我应该显示一个消息框吗?一切都比吃一个异常好。我会把整条线路都拆了。如果您无法处理异常,请不要捕获它。该表应填写LOL。是吗??你最好的朋友,那个,应该帮助你我不再在这里写答案了,但是有很多答案。。