2013-04-26 67 views
0

我的Datagridview被綁定到一個數據表,當用戶點擊一個列標題,我寫代碼使用DefaultView.Sort方法排序我的數據表,然後我設置排序視圖作爲我的網格數據源,下面是代碼排序:如何記住DataGridViewRow隱藏屬性

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

    } 

我有我的窗體上的按鈕,隱藏空行,空行就是不能進入某些列的數量一排,問題是,當用戶排序網格,所有現有的隱藏行再次出現。

如何保存行的隱藏屬性,使其適用於新的數據源。

回答

1

在您的代碼中添加一個變量並將值存儲在那裏。

0

我想出的解決方案是在我的數據表中添加一個額外的列來保持行隱藏狀態,然後在我的dataview.rowfilter中,我排除了隱藏的行。