2017-05-09 22 views
0

我在gridview中有一列出現日期和時間。但我只想合併基於相同日期的列,雖然時間不一樣。如何做到這一點?此代碼和輸出我嘗試到目前爲止,只合並其他列而不是列日期時間。如何在gridview中合併單元格datetime

public void GridView_Row_Merger(GridView gridView) 
{ 
    for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--) 
    { 
     GridViewRow currentRow = gridView.Rows[rowIndex]; 
     GridViewRow previousRow = gridView.Rows[rowIndex + 1]; 

     for (int i = 0; i < currentRow.Cells.Count; i++) 
     { 
      if (currentRow.Cells[i].Text == previousRow.Cells[i].Text) 
      { 
       if (previousRow.Cells[i].RowSpan < 2) 
        currentRow.Cells[i].RowSpan = 2; 
       else 
        currentRow.Cells[i].RowSpan = previousRow.Cells[i].RowSpan + 1; 
       previousRow.Cells[i].Visible = false; 
      } 
     } 
    } 
} 
+0

假設你有3行,A,B,C。如果A&B的日期時間列具有相同的日期但時間不同,您想要隱藏A行,那是正確的嗎?時間部分是否真的很重要?將時間部分重置爲12:00 AM或顯示日期部分,可以顯示B行嗎? – Martheen

+0

不,如果在一列中有相同的日期,它會合併爲一個。時間不重要。我想合併具有相同日期的單元格。但這段代碼不起作用,因爲我的時間不同,儘管它的日期相同。如何從日期中刪除時間,以便它可以在不考慮時間的情況下讀取和合並單元格。只考慮日期? – NFH

+0

@Martheen是的,對不起,我只是回頭看,你的理解是什麼!不,我不想考慮時間。只有日期。上面的代碼考慮時間,這就是爲什麼它不合並單元格。不需要放映時間,只需要在B行的日期。你能幫助我嗎? – NFH

回答

0

我剛剛得到了答案!這是代碼。謝謝大家幫助 !

public void GridView_Row_Merger(GridView gridView) 
{ 
    for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--) 
    { 
     GridViewRow currentRow = gridView.Rows[rowIndex]; 
     GridViewRow previousRow = gridView.Rows[rowIndex + 1]; 
     string[] arra1 = currentRow.Cells[0].Text.Split(' '); 
     string[] arra2 = previousRow.Cells[0].Text.Split(' '); 
     currentRow.Cells[0].Text = arra1[0]; 
     previousRow.Cells[0].Text = arra2[0]; 
     if (currentRow.Cells[0].Text == previousRow.Cells[0].Text) 
     { 
      if (previousRow.Cells[0].RowSpan < 2) 
      { 
       currentRow.Cells[0].RowSpan = 2; 
      } 
      else 
      { 
       currentRow.Cells[0].RowSpan = previousRow.Cells[0].RowSpan + 1; 
      } 
      previousRow.Cells[0].Visible = false; 
     } 
    } 
}