1. 程式人生 > >aspx GridView的RowDataBound中改變單元格大小,顏色,彙總等

aspx GridView的RowDataBound中改變單元格大小,顏色,彙總等

 

 protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[0].Attributes.Add("style", "width:40px");
        e.Row.Cells[1].Attributes.Add("style", "width:60px");
        e.Row.Cells[2].Attributes.Add("style", "width:60px");
        e.Row.Cells[3].Attributes.Add("style", "width:60px");
        e.Row.Cells[4].Attributes.Add("style", "width:60px");
        e.Row.Cells[5].Attributes.Add("style", "width:75px");
        e.Row.Cells[6].Attributes.Add("style", "width:75px");
        e.Row.Cells[7].Attributes.Add("style", "width:75px");
        e.Row.Cells[8].Attributes.Add("style", "width:75px");
        e.Row.Cells[9].Attributes.Add("style", "width:75px");
        e.Row.Cells[10].Attributes.Add("style", "width:75px");
        e.Row.Cells[11].Attributes.Add("style", "width:110px");
        e.Row.Cells[12].Attributes.Add("style", "width:110px");
        e.Row.Cells[13].Attributes.Add("style", "width:110px");
        e.Row.Cells[14].Attributes.Add("style", "width:140px");
        e.Row.Cells[15].Attributes.Add("style", "width:40px");
        e.Row.Cells[16].Attributes.Add("style", "width:40px");
      
  
      
         if (e.Row.RowType == DataControlRowType.DataRow)
         {

             e.Row.Cells[5].ForeColor = System.Drawing.Color.DarkRed;
             e.Row.Cells[6].ForeColor = System.Drawing.Color.DarkBlue;
             e.Row.Cells[7].ForeColor = System.Drawing.Color.DarkGreen;
             e.Row.Cells[8].ForeColor = System.Drawing.Color.DarkRed;
             e.Row.Cells[9].ForeColor = System.Drawing.Color.DarkBlue;
 
             DataRowView dr = e.Row.DataItem as DataRowView;
             if (dr.Row[bilv].ToString().Trim() == "")
             {

             }
             else
             {
                 numbil += Convert.ToDouble(dr.Row[bilv]);

                 if (Convert.ToDouble(dr.Row[bilv]).ToString().Split('.')[0] == "0" || Convert.ToDouble(dr.Row[bilv]).ToString().Split('.')[0] == "-0")
                 {
                     e.Row.Cells[13].Text = "0";
                 }
                 else
                 {
                     //Math.Round(Convert.ToDouble(Convert.ToDouble(dr.Row[bilv]).ToString().Split('.')[0]), 2, MidpointRounding.AwayFromZero);

                     e.Row.Cells[13].Text = Math.Round(Convert.ToDouble(Convert.ToDouble(dr.Row[bilv]).ToString().Split('.')[0]), 0, MidpointRounding.AwayFromZero).ToString();
                 }

             }
             if (dr.Row[bidui].ToString().IndexOf("-") >= 0)
             {
                 e.Row.Cells[11].ForeColor = System.Drawing.Color.Red;
             }
             if (dr.Row[fuzhi].ToString().IndexOf("-") >= 0)
             {
                 e.Row.Cells[12].ForeColor = System.Drawing.Color.Red;
             }
             if (dr.Row[bilv].ToString().Trim() == "")
             {

             }
             else
             {
                 if (e.Row.Cells[13].Text.IndexOf("-") >= 0)
                 {
                     e.Row.Cells[13].ForeColor = System.Drawing.Color.Red;
                 }
             }
 
           

            
           num += Convert.ToDouble(dr.Row[bidui]);
           numfz += Convert.ToDouble(dr.Row[fuzhi]);
             i++;
         }  
         else if (e.Row.RowType == DataControlRowType.Footer)  
         {  
             //Label LabelAll =  e.Row.FindControl("labelAll") as Label;  
             //if (LabelAll != null)    
             //{      
             //    LabelAll.Text += num.ToString();//"計算的總數,或者也可以單獨計算";//  
             //}
           e.Row.Cells[0].Text = "共 "+i+"條";
           e.Row.Cells[10].Text = "總計:";
            e.Row.Cells[11].Text =  num.ToString();
           e.Row.Cells[12].Text = numfz .ToString();
           e.Row.Cells[13].Text = numbil.ToString();
  

         }
    }