簡體   English   中英

C#在圖表中使用DataGridView中的數據

[英]C# Using data from DataGridView in a Chart

我有在DataGridView中生成數據的代碼,我也想通過單擊同一按鈕將其推送到圖表。

處理:

將文本放入文本框myInputBox然后按下按鈕processButton拆分文本並將其輸出到DataGridView myOutputDGV該按鈕具有以下代碼:

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,
            }
        }

我想用來自X軸上的column [1](直線)和Y軸上的column [3](總計)的值填充圖表myChart

編輯:

我嘗試在按鈕processButton單擊代碼中添加以下代碼,但未填充圖表:

ChartArea chartArea1 = new ChartArea();
chartArea1.AxisX.MajorGrid.LineColor = Color.LightGray;
chartArea1.AxisY.MajorGrid.LineColor = Color.LightGray;
chartArea1.AxisX.LabelStyle.Font = new Font("Consolas", 8);
chartArea1.AxisY.LabelStyle.Font = new Font("Consolas", 8);
myChart.ChartAreas.Add(chartArea1);

myChart.Series.Add(new Series());

myChart.Series[0].XValueMember = myOutputDGV.Columns[1].DataPropertyName;
myChart.Series[0].YValueMembers = myOutputDGV.Columns[3].DataPropertyName;
myChart.DataSource = myOutputDGV.DataSource;

myChart.Series[0].ChartType = SeriesChartType.Line;

編輯2:

我也嘗試這樣做無濟於事:

System.Windows.Forms.DataVisualization.Charting.ChartArea myChartArea = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
myChartArea.AxisX.MajorGrid.LineColor = Color.Red;
myChartArea.AxisY.MajorGrid.LineColor = Color.Red;
myChartArea.AxisX.LabelStyle.Font = new System.Drawing.Font("Consolas", 10);
myChartArea.AxisY.LabelStyle.Font = new System.Drawing.Font("Consolas", 10);
myChart.ChartAreas.Add(myChartArea);

myChart.Series.Add(new System.Windows.Forms.DataVisualization.Charting.Series());

var datax = myOutputDGV.Rows.Cast<DataGridViewRow>().Select(x => x.Cells[1].Value).ToList();
var datay = myOutputDGV.Rows.Cast<DataGridViewRow>().Select(x => x.Cells[4].Value).ToList();

編輯3個完整代碼:

private void ProcessButton_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(myInputBox.Text) || myInputBox.Text == "")
{
    MessageBox.Show("Please enter data", "Info");
}
else
{
    myOutputDGV.Rows.Clear();
    myOutputDGV.Columns.Clear();
    myOutputDGV.Refresh();
    myChart.ChartAreas.Clear();
    myChart.Series.Clear();

    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 = "Cost Range";
    myOutputGrid.Name = "Cost Range";
    myOutputDGV.Columns.Add(myOutputGrid);
    myOutputGrid = new DataGridViewTextBoxColumn();

    myOutputGrid.HeaderText = "Cost First Row";
    myOutputGrid.Name = "Cost First Row";
    myOutputDGV.Columns.Add(myOutputGrid);
    myOutputGrid = new DataGridViewTextBoxColumn();

    myOutputGrid.HeaderText = "Cost All Rows";
    myOutputGrid.Name = "Cost All Rows";
    myOutputDGV.Columns.Add(myOutputGrid);
    myOutputGrid = new DataGridViewTextBoxColumn();

    myOutputGrid.HeaderText = "Rows";
    myOutputGrid.Name = "Rows";
    myOutputDGV.Columns.Add(myOutputGrid);
    myOutputGrid = new DataGridViewTextBoxColumn();

    myOutputGrid.HeaderText = "Width";
    myOutputGrid.Name = "Width";
    myOutputDGV.Columns.Add(myOutputGrid);

    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,
        Regex.Match(item, @"\.\.(.+?) ").Groups[1].Value,
        Regex.Match(item, @"rows=(.+?) ").Groups[1].Value,
        Regex.Match(item, @"width=(.+?)\)").Groups[1].Value);
    }

    var datax = myOutputDGV.Rows.Cast<DataGridViewRow>().Select(x => x.Cells[1].Value).ToList();
    var datay = myOutputDGV.Rows.Cast<DataGridViewRow>().Select(x => x.Cells[4].Value).ToList();
    System.Windows.Forms.DataVisualization.Charting.ChartArea myChartArea = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
    myChartArea.AxisX.MajorGrid.LineColor = Color.Red;
    myChartArea.AxisY.MajorGrid.LineColor = Color.Red;
    myChartArea.AxisX.LabelStyle.Font = new System.Drawing.Font("Consolas", 10);
    myChartArea.AxisY.LabelStyle.Font = new System.Drawing.Font("Consolas", 10);
    myChart.ChartAreas.Add(myChartArea);
    myChart.Series.Add(new System.Windows.Forms.DataVisualization.Charting.Series());

    myChart.Series[0].XValueMember = myOutputDGV.Columns[1].DataPropertyName;
    myChart.Series[0].YValueMembers = myOutputDGV.Columns[3].DataPropertyName;
    myChart.DataSource = myOutputDGV.DataSource;
    myChart.Series.Add(new System.Windows.Forms.DataVisualization.Charting.Series());
}
}

編輯4我有網格和半行:

            var datax = myOutputDGV.Rows.Cast<DataGridViewRow>().Select(x => x.Cells[1].Value).ToList();
            var datay = myOutputDGV.Rows.Cast<DataGridViewRow>().Select(x => x.Cells[4].Value).ToList();
            string dataxx = datax.ToString();
            string datayy = datay.ToString();
            System.Windows.Forms.DataVisualization.Charting.ChartArea myChartArea = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            myChart.ChartAreas.Add(myChartArea);
            myChart.Series.Add(new System.Windows.Forms.DataVisualization.Charting.Series());
            myChart.Series[0].Points.AddXY(dataxx, datayy);

如果要盡可能保留代碼,則可以填充DataTable而不是DataGridView並將其用作DataGridViewChartDataSource

在這種情況下,下面的代碼大致是您需要做的:聲明DataTable ,創建Columns (正確的類型,我只是猜到了!),填充Rows ,並將其用作DataSource

好處很明顯:您將擁有強制的數據類型,並且可以使用斷點和VS Data Visualizer來檢查內容是否符合預期(但在這種情況下,您也會在DataGridView看到它)。 可能是數據類型給chart帶來了麻煩。

C#(翻譯):

private DataTable dt;

private void MyForm_Load()
{
    LoadDefaults();
}

private void LoadDefaults()
{
    dt.Columns.Add("Line", typeof(Int16));
    dt.Columns.Add("Section", typeof(string));
    dt.Columns.Add("Range", typeof(string));
    dt.Columns.Add("Total", typeof(float));
}

private void processButton_Click(object sender, EventArgs e)
{

foreach (var Item in mySplit) {
    dt.Rows.Add({Item.Trim(), ......});
    }


this.DataGridView1.DataSource = dt;
...
myChart.DataSource = dt;
}

VB.NET

Dim dt As DataTable

Private Sub MyForm_Load()
    Call LoadDefaults()
End Sub

Private Sub LoadDefaults()
    dt.Columns.Add("Line", GetType(Int16))
    dt.Columns.Add("Section", GetType(String))
    dt.Columns.Add("Range", GetType(String))
    dt.Columns.Add("Total", GetType(Single))
End Sub

Private Sub processButton_Click(sender As Object, e As EventArgs) Handles BtnExcel.Click
    For Each Item In mySplit
        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})
    Next

    Me.DataGridView1.DataSource = dt
    ...
    myChart.DataSource = dt
End Sub

編輯 -調試圖表示例:

在此處輸入圖片說明

有很多使用Chart DataBinding的方法 -我建議在綁定時不要綁定到圖表,而是綁定到SeriesPoints -通過這種方式,您可以更好地控制事物,例如選擇要綁定到的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);

..其中xNameyName可以是DGV Columns名稱或索引。

但是,正如建議的那樣,建議使用DataTable 無論如何,您都需要一個IEnumerable進行綁定。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM