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..