0

I'm trying to chart only the visible rows in the datagridview. The reason is because the datagridviewer is already sorted by date and I only want those values. The other row values and coded as .Visible = false. Below is the code to show the chart and plotting the X and Y values. I've tried to work with the RowsVisible method but have come up with nothing.

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();
}
LitteringAnd
  • 69
  • 1
  • 10
  • I've had the same issue with excel vba trying to plot only visible rows. The only solution was to make a copy of the table with only the rows needed. – jdweng Oct 08 '15 at 14:43
  • what is CK_QA_DataDataGridView.DataSource? a DataTable? – PrfctByDsgn Oct 09 '15 at 08:08
  • Yes. It's a datatable. The datatable is dt in the code, but apparently it already knows that based on the datagridview binding. – LitteringAnd Oct 09 '15 at 13:40

1 Answers1

0

There are probably many ways to do that, however I doubt that you can make a DataSource look directly into the visibility of a DGV row.

So the best solution depends on how the visibility is determined.

If it is by evaluating some data you may succeed by using a DataView based on the same DataTable with a Filter.

But if the user can make the DGV rows invisible at will, you will have to create a separate DataSource for the Chart.

Here is a full but minimal example..

I create a table to fill the DGV first:

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

Then I code a NumericUpDown to simulate a condition for the visibility:

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

Finally I create a DataSource based on the Rows in the original DataTable and the Visible property of each DGV row:

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", "");
}

Note that you can't hide the current row. So to be safe you may need to use this workaround..

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111