仅在datagridview中绘制可见行的图表

本文关键字:datagridview 绘制 仅在 | 更新日期: 2023-09-27 18:16:43

我试图仅在datagridview中绘制可见行。原因是因为datagridviewer已经按日期排序,我只想要这些值。另一行值并编码为. visible = false。下面是显示图表并绘制X和Y值的代码。我试过使用RowsVisible方法,但没有任何结果。

if (CK_QA_DataDataGridView.Rows.Count == 0)
{
    MessageBox.Show("Select Date Range and Show Results Before Chart");
}
else
{
       chart5.Visible = true;
       InitialChart.Visible = false;
       chart5.DataSource = CK_QA_DataDataGridView.DataSource;
       this.chart5.Series["X error"].XValueMember = CK_QA_DataDataGridView.Columns[0].DataPropertyName;
       this.chart5.Series["Y error"].YValueMembers = CK_QA_DataDataGridView.Columns[13].DataPropertyName;
       chart5.DataBind();
}

仅在datagridview中绘制可见行的图表

可能有很多方法可以做到这一点,但是我怀疑你是否可以让DataSource直接查看DGV行的可见性。

因此,最佳解决方案取决于如何确定能见度。

如果是通过评估一些数据,您可以使用基于与 Filter 相同的DataTableDataView

但是,如果用户可以随意使DGV行不可见,则必须为Chart创建单独的DataSource

这是一个完整但最小的例子…

我先创建一个表来填充DGV:

DataTable DT2 = null;
private void button16_Click(object sender, EventArgs e)
{
    DT2 = new DataTable("Artists");
    DT2.Columns.Add("Name", typeof(string));
    DT2.Columns.Add("Age", typeof(int));
    DT2.Columns.Add("Score", typeof(int));
    DT2.Rows.Add("Animals", 33, 17);
    DT2.Rows.Add("Band", 45, 9);
    DT2.Rows.Add("Cream", 43, 26);
    DT2.Rows.Add("Doors", 50, 21);
    dataGridView1.DataSource = DT2;
}

然后我编写一个NumericUpDown来模拟可见性的条件:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    int limit = (int)numericUpDown1.Value;
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        var dbo = (DataRowView)row.DataBoundItem;
        row.Visible = (int)dbo[2] >= limit;  //check for current row missing!
    }
}

最后,我基于原始DataTable中的行和每个DGV行的Visible属性创建DataSource:

private void button17_Click(object sender, EventArgs e)
{
    List<DataRowView> rows2show = new List<DataRowView>();
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        var dbo = (DataRowView)row.DataBoundItem;
        if (row.Visible) rows2show.Add(dbo);
    }
    chart5.Series[0].Points.DataBind(rows2show, "Name", "Score", "");
}

注意不能隐藏当前行。因此,为了安全起见,您可能需要使用此解决方案。