C# 在图表中使用 DataGridView 中的数据

我有在 DataGridView 中生成数据的代码,我还想通过单击同一按钮将其推送到图表。


过程:


文本被放入文本框中myInputBox,然后按下按钮processButton将文本分割并将其输出到 DataGridViewmyOutputDGV按钮具有以下代码:


private void processButton_Click(object sender, EventArgs e)

        {

            List<string> mySplit = new List<string>(myInputBox.Text.Split(new string[] { "XN" }, StringSplitOptions.None));


            DataGridViewTextBoxColumn myOutputGrid = new DataGridViewTextBoxColumn();

            myOutputGrid.HeaderText = "Line";

            myOutputGrid.Name = "Line";

            myOutputDGV.Columns.Add(myOutputGrid);

            myOutputGrid = new DataGridViewTextBoxColumn();


            myOutputGrid.HeaderText = "Section";

            myOutputGrid.Name = "Section";

            myOutputDGV.Columns.Add(myOutputGrid);

            myOutputGrid = new DataGridViewTextBoxColumn();


            myOutputGrid.HeaderText = "Range";

            myOutputGrid.Name = "Range";

            myOutputDGV.Columns.Add(myOutputGrid);

            myOutputGrid = new DataGridViewTextBoxColumn();


            myOutputGrid.HeaderText = "Total";

            myOutputGrid.Name = "Total";

            myOutputDGV.Columns.Add(myOutputGrid);

            myOutputGrid = new DataGridViewTextBoxColumn();


            foreach (string item in mySplit)

            {

                myOutputDGV.Rows.Add(item.Trim(),

                item.Split(new string[] { "(cost=" }, StringSplitOptions.None).First(),

                Regex.Match(item, @"cost=(.+?) rows").Groups[1].Value,

                Regex.Match(item, @"cost=(.+?)\.\.").Groups[1].Value,

            }

        }

myChart我想使用 X 轴上的列 [1](线)和 Y 轴上的列 [3](总计)中的值填充图表。


不负相思意
浏览 60回答 2
2回答

回首忆惘然

如果您想尽可能保留代码,可以填写 aDataTable而不是DataGridView,并将其用作 和中DataSource的。DataGridViewChart以下代码大致是您在这种情况下需要执行的操作:声明DataTable、创建Columns(正确的类型,我只是猜测它们!)、填充Rows并将其用作DataSource。好处很明显:您将拥有强制数据类型,并且可以使用断点和 VS Data Visualizer 来检查内容是否符合预期(但在本例中您也会看到它)DataGridView。可能是数据类型给您带来了麻烦chart。C#(翻译):private DataTable dt;private void MyForm_Load(){&nbsp; &nbsp; LoadDefaults();}private void LoadDefaults(){&nbsp; &nbsp; dt.Columns.Add("Line", typeof(Int16));&nbsp; &nbsp; dt.Columns.Add("Section", typeof(string));&nbsp; &nbsp; dt.Columns.Add("Range", typeof(string));&nbsp; &nbsp; dt.Columns.Add("Total", typeof(float));}private void processButton_Click(object sender, EventArgs e){foreach (var Item in mySplit) {&nbsp; &nbsp; dt.Rows.Add({Item.Trim(), ......});&nbsp; &nbsp; }this.DataGridView1.DataSource = dt;...myChart.DataSource = dt;}VB.NETDim dt As DataTablePrivate Sub MyForm_Load()&nbsp; &nbsp; Call LoadDefaults()End SubPrivate Sub LoadDefaults()&nbsp; &nbsp; dt.Columns.Add("Line", GetType(Int16))&nbsp; &nbsp; dt.Columns.Add("Section", GetType(String))&nbsp; &nbsp; dt.Columns.Add("Range", GetType(String))&nbsp; &nbsp; dt.Columns.Add("Total", GetType(Single))End SubPrivate Sub processButton_Click(sender As Object, e As EventArgs) Handles BtnExcel.Click&nbsp; &nbsp; For Each Item In mySplit&nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add({Item.Trim(), Item.Split("(cost=", StringSplitOptions.None).First(), Regex.Match(Item, @"cost=(.+?) rows").Groups[1].Value, Regex.Match(Item, @"cost=(.+?)\.\.").Groups[1].Value})&nbsp; &nbsp; Next&nbsp; &nbsp; Me.DataGridView1.DataSource = dt&nbsp; &nbsp; ...&nbsp; &nbsp; myChart.DataSource = dtEnd Sub编辑-调试图表示例:

交互式爱情

使用图表数据绑定的方法有很多种。- 我建议,绑定时,不要绑定到图表,而是绑定Points到Series!- 这样你就可以更好地控制事情,比如选择Series绑定到哪个。一个例子..:var datax = dataGridView1.Rows            .Cast<DataGridViewRow>().Select(x => x.Cells[ixName].Value).ToList();var datay = dataGridView1.Rows            .Cast<DataGridViewRow>().Select(x => x.Cells[yName].Value).ToList();aSeries.Points.DataBindXY(datax, datay);..其中xName和yName可以是 DGV 的名称或索引Columns。但是,正如所建议的,DataTable建议使用 a。无论如何你都需要一个IEnumerable来绑定。
打开App,查看更多内容
随时随地看视频慕课网APP