C#刷新dataGridView后如何应用行过滤器

时间:2016-07-05 17:03:23

标签: c# datagridview refresh rowfilter

我可以保存当前的dataGridView滚动和焦点位置,但是当我应用过滤器时它不起作用。如何在刷新datagridview之后应用行过滤器或保留选定的行?

public void button_refresh_Click(object sender, EventArgs e)
    {
        int row = (this.dataGridView1.CurrentCell.RowIndex);
        int col = (this.dataGridView1.CurrentCell.ColumnIndex);
        int scroll = dataGridView1.FirstDisplayedCell.RowIndex;

        try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string query = "select * from PPAPdatabase";
            command.CommandText = query;

            OleDbDataAdapter da = new OleDbDataAdapter(command);
            dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
        }
        catch (Exception ex)
        {
            MessageBox.Show("An error has occurred: " + ex.Message,
                "Important Note",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error,
                MessageBoxDefaultButton.Button1);
        }
        finally
        {
            connection.Close();
        }
        dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);             
        this.dataGridView1.CurrentCell = this.dataGridView1[col, row];
        dataGridView1.FirstDisplayedScrollingRowIndex = scroll;
    }

过滤器:

private void text_Pnumber_TextChanged(object sender, EventArgs e)
    {
        DataView dv = new DataView(dt);
        dv.RowFilter = string.Format("[Part Number] LIKE '%{0}%'", text_Pnumber.Text);
        dataGridView1.DataSource = dv;
    }

任何帮助将不胜感激!

0 个答案:

没有答案