2014-10-10 27 views
0

我想在1列上使用行跨度(如果下一行的值相同) 並根據上述情況交替排列顏色。RowSpan和備用行顏色同時使用c#

我設法讓行跨度按我想要的方式工作,但不能交替行。批號:3694217行應該是藍色的。

下面是電流輸出:

enter image description here

天冬氨酸

<asp:GridView ID="visualisation" runat="server" 
       AutoGenerateColumns="false" OnDataBound="OnDataBound" OnItemDataBound="Item_Bound" CellPadding="15" CellSpacing="15" HeaderStyle-BackColor="DarkOliveGreen" GridLines="Both"> 
       <Columns> 
        <asp:BoundField DataField="BatchNo" HeaderText="BatchNo" HeaderStyle-Width="15%" /> 
        <asp:BoundField DataField="Type" HeaderText="Type" HeaderStyle-Width="15%" />          
        <asp:ImageField DataImageUrlField="DataLoaded" HeaderText="DataLoaded" HeaderStyle-Width="15%" /> 
        <asp:ImageField DataImageUrlField="Errors" HeaderText="Errors" HeaderStyle-Width="15%" /> 
        <asp:ImageField DataImageUrlField="ProcessingRun" HeaderText="ProcessingRun" HeaderStyle-Width="15%" /> 
       </Columns> 
      </asp:GridView> 

C#

protected void Page_Load(object sender, EventArgs e) 
     { 
      dataT(); 
      visualisation.DataBind(); 

     } 

     public void dataT() 
     { 
      DataTable dtVisu = new DataTable(); 

      dtVisu.Columns.Add(new DataColumn("BatchNo", typeof(System.String))); 
      dtVisu.Columns.Add(new DataColumn("Type", typeof(System.String))); 
      dtVisu.Columns.Add(new DataColumn("DataLoaded", typeof(System.String))); 
      dtVisu.Columns.Add(new DataColumn("Errors", typeof(System.String))); 
      dtVisu.Columns.Add(new DataColumn("ProcessingRun", typeof(System.String))); 
      //dtVisu.Columns.Add(new DataColumn("alignRow", typeof(System.String))); 


      DataRow dr = dtVisu.NewRow(); 
      dr["BatchNo"] = "3704500"; 
      dr["Type"] = "Calibration"; 
      dr["DataLoaded"] = "images/g4-12.png"; 
      dr["Errors"] = "images/g4-12.png"; 
      dr["ProcessingRun"] = "images/g4-12.png"; 
      //dr["alignRow"] = "1"; 
      dtVisu.Rows.Add(dr); 


      dr = dtVisu.NewRow(); 
      dr["BatchNo"] = "3704542"; 
      dr["Type"] = "Range Settings"; 
      dr["DataLoaded"] = "images/r4-12.png"; 
      dr["Errors"] = "images/r4-12.png"; 
      dr["ProcessingRun"] = "images/g4-12.png"; 
      dtVisu.Rows.Add(dr); 

      dr = dtVisu.NewRow(); 
      dr["BatchNo"] = "3704542"; 
      dr["Type"] = "Range Settings"; 
      dr["DataLoaded"] = "images/r4-12.png"; 
      dr["Errors"] = "images/r4-12.png"; 
      dr["ProcessingRun"] = "images/g4-12.png"; 
      dtVisu.Rows.Add(dr); 

      dr = dtVisu.NewRow(); 
      dr["BatchNo"] = "3687345"; 
      dr["Type"] = "Calibration"; 
      dr["DataLoaded"] = "images/g4-12.png"; 
      dr["Errors"] = "images/g4-12.png"; 
      dr["ProcessingRun"] = "images/g4-12.png"; 
      dtVisu.Rows.Add(dr); 

      dr = dtVisu.NewRow(); 
      dr["BatchNo"] = "3694217"; 
      dr["Type"] = "Calibration"; 
      dr["DataLoaded"] = "images/g4-12.png"; 
      dr["Errors"] = "images/g4-12.png"; 
      dr["ProcessingRun"] = "images/g4-12.png"; 
      dtVisu.Rows.Add(dr); 



      visualisation.DataSource = dtVisu; 
     } 

protected void OnDataBound(object sender, EventArgs e) 
     { 
      int RowSpan = 2; 
      for (int i = visualisation.Rows.Count - 2; i >= 0; i--) 
      { 
       GridViewRow currRow = visualisation.Rows[i]; 
       GridViewRow prevRow = visualisation.Rows[i + 1]; 

       if (currRow.Cells[0].Text == prevRow.Cells[0].Text) 
       { 
        currRow.Cells[0].RowSpan = RowSpan; 
        prevRow.Cells[0].Visible = false; 
        RowSpan += 1; 

        currRow.BackColor = Color.FromName("#7AA5D6"); 
        prevRow.BackColor = Color.FromName("#7AA5D6"); 
       } 
       else 
       { 
        RowSpan = 2; 
       } 
      } 
     } 
+0

我沒有看到任何代碼,除了兩個顏色的任何行。另外,你爲什麼要以相反的順序遍歷行? – BCdotWEB 2014-10-10 12:54:35

+0

我無法得到它的工作,這就是爲什麼我從未包含顏色的代碼。倒序的原因是我不會得到空值,我可以很容易地比較它們 – emre 2014-10-10 13:01:39

回答

3

你需要保持你的行的獨立的計數器來分配交替的顏色。請注意,在當前代碼顏色中,只有行跨越時才交替使用,因此單行不會被突出顯示。一些事情應該這樣做:

protected void OnDataBound(object sender, EventArgs e) 
{ 
    int RowSpan = 2; 
    // actual row counter, spanned rows count as one 
    int rowCount = 0; 
    for (int i = visualisation.Rows.Count - 2; i >= 0; i--) 
    { 
     GridViewRow currRow = visualisation.Rows[i]; 
     GridViewRow prevRow = visualisation.Rows[i + 1]; 

     if (currRow.Cells[0].Text == prevRow.Cells[0].Text) 
     { 
      currRow.Cells[0].RowSpan = RowSpan; 
      prevRow.Cells[0].Visible = false; 
      RowSpan += 1; 
     } 
     else 
     { 
      RowSpan = 2; 
      //it was a new row 
      rowCount++; 
     } 

     if (rowCount % 2 == 0) 
     { 
      currRow.BackColor = Color.FromName("#7AA5D6");    
     } 
    } 
} 
+0

我得到它的工作。非常感謝。 – emre 2014-10-10 13:02:03

+0

我怎樣才能將顏色分配給單排? – emre 2014-10-10 13:12:03

+0

@ emre22:我不認爲你會遍歷所有的行。 – BCdotWEB 2014-10-10 13:15:16