|
ちょっとしたアイデアでしかありませんが、DataGridViewにBindするデータにFilterをかけるのではいかがでしょうか。
こんな感じです。RowFilterはStringなので、生成方法を考えれば希望される動作はしそうですが。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private DataSet data = new DataSet();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable table = data.Tables.Add("data");
table.Columns.Add("number", System.Type.GetType("System.UInt32"));
DataRow row;
row = table.NewRow(); row["number"] = 1; table.Rows.Add(row);
row = table.NewRow(); row["number"] = 2; table.Rows.Add(row);
row = table.NewRow(); row["number"] = 5; table.Rows.Add(row);
row = table.NewRow(); row["number"] = 6; table.Rows.Add(row);
row = table.NewRow(); row["number"] = 9; table.Rows.Add(row);
dataGridView1.DataSource = table;
}
private void button1_Click(object sender, EventArgs e)
{
DataTable gridTable = (DataTable)dataGridView1.DataSource;
gridTable.DefaultView.RowFilter = "number NOT IN (2, 5)";
}
private void button2_Click(object sender, EventArgs e)
{
DataTable gridTable = (DataTable)dataGridView1.DataSource;
if (gridTable.DefaultView.RowFilter != "")
{
gridTable.DefaultView.RowFilter = "(" + gridTable.DefaultView.RowFilter + ") and (number NOT IN (1, 9))";
}
else
{
gridTable.DefaultView.RowFilter = "(number NOT IN (1, 9))";
}
}
private void button3_Click(object sender, EventArgs e)
{
DataTable gridTable = (DataTable)dataGridView1.DataSource;
gridTable.DefaultView.RowFilter = "";
}
}
}
|