0

私の Datagridview はデータテーブルにバインドされています。ユーザーが列ヘッダーの 1 つをクリックすると、DefaultView.Sort メソッドを使用してデータテーブルを並べ替えるコードを作成し、並べ替えられたビューをグリッド データソースとして設定します。以下は並べ替えのコードです。

 private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        Font f = new System.Drawing.Font("Arial", 8, FontStyle.Bold);
        string ColName = dataGridView1.Columns[e.ColumnIndex].Name;
        string SortDirection = string.Empty;


        foreach (DataRow drforDirection in dtSortDirection.Rows)
        {
            if (drforDirection["ColumnName"].ToString() == ColName)
            {
                SortDirection = drforDirection["Direction"].ToString();
                drforDirection["Direction"] = (SortDirection == "ASC") ? "DESC" : "ASC";
            }

        }

        tmptotalRow = null;
        dtTotals = ((DataTable)dataGridView1.DataSource).Clone();

        dtTotals.Rows.Add(((DataTable)dataGridView1.DataSource).Rows[0].ItemArray);
        DataTable tmpDataTable = new DataTable();


        ((DataTable)dataGridView1.DataSource).Rows.RemoveAt(0);
        SortDirection = (SortDirection == "ASC") ? "DESC" : "ASC";
        ((DataTable)dataGridView1.DataSource).DefaultView.Sort = ColName + " " + SortDirection;
        tmpDataTable = ((DataTable)dataGridView1.DataSource).DefaultView.ToTable();
        tmpDataTable.ImportRow(dtTotals.Rows[0]);



        DataRow[] dr = tmpDataTable.Select("ItemLookupCode = 'Grand Totals'");
        DataRow newRow = tmpDataTable.NewRow();
        // We "clone" the row
        newRow.ItemArray = dr[0].ItemArray;
        // We remove the old and insert the new

        tmpDataTable.Rows.Remove(dr[0]);
        tmpDataTable.Rows.InsertAt(newRow, 0);
        dataGridView1.DataSource = tmpDataTable;

        dataGridView1.Rows[0].Frozen = true;
        dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.BurlyWood;
        dataGridView1.Rows[0].DefaultCellStyle.ForeColor = Color.Black;
        dataGridView1.Rows[0].DefaultCellStyle.Font = f;
        dataGridView1.Rows[0].ReadOnly = true;
        //btnDeleteEmpty_Click(sender, e);

    }

フォームに空の行を非表示にするボタンがあります。空の行は、特定の列の数量が入力されていない行です。問題は、ユーザーがグリッドをソートすると、既存のすべての非表示の行が再び表示されることです。

行の隠しプロパティを保存して、新しいデータソースに適用するにはどうすればよいですか。

4

2 に答える 2

0

私が思いついた解決策は、データ テーブルに列を追加して行の非表示ステータスを維持することです。次に、dataview.rowfilter で非表示の行を除外しました。

于 2013-04-26T09:31:10.453 に答える