DataTable dataTable = objset.Tables["dd"];          
   
            //创建一个新行并添加到dataTable数据表中
            DataRow dataRow;
            dataRow = dataTable.NewRow();           
            //使用Sum含数计算编号的总和,使用Count函数计算编号的个数
            //dataRow[1] = "合计";            dataRow[0] = dataTable.Compute("Sum(ttime)", "true");            dataRow[1] = dataTable.Compute("Sum(stime)", "true");             double aa = Convert.ToDouble(dataRow[0].ToString());            double ss = Convert.ToDouble(dataRow[1].ToString());            dataRow[5] = Convert.ToDouble(ss / aa*100).ToString("f2");                 dataTable.Rows.Add(dataRow);
            
            dataGridView1.DataSource = dataTable;怎样加上颜色?  谢谢

解决方案 »

  1.   


    //设置行的颜色
    dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.PaleGoldenrod;
    //设置列的颜色
    dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.LightYellow;
      

  2.   

    使用 DataGridViewCellStyle 
      

  3.   

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        // If the column is the Artist column, check the
        // value.
        if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Artist")
        {
            if (e.Value != null)
            {
                // Check for the string "pink" in the cell.
                string stringValue = (string)e.Value;
                stringValue = stringValue.ToLower();
                if ((stringValue.IndexOf("pink") > -1))
                {
                    e.CellStyle.BackColor = Color.Pink;
                }        }
        }
        else if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Release Date")
        {
            ShortFormDateFormat(e);
        }
    }
      

  4.   


    //设置合计行的背景颜色为红色
    this.dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Red;
      

  5.   

    在DataGridView的RowPostPaint事件中添加代码private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
    {
        DataGridView dgv = sender as DataGridView;
        if (dgv.Rows.Count <=0) return;
        for (int i = 0; i<dgv.Rows.Count; i++)
        {
            dgv.Rows[i].Cells[1].Style.BackColor = System.Drawing.Color.Yellow;
        }
    }
      

  6.   

    POWER_WONG
    你发的代码是整个DataGridView列的颜色 我要最后行的颜色
      

  7.   

    for(int i=0;i<DataGridView.columns.count;i++)
    {
    DataGridView.rows[0].cells[i].backcolor=color.red;
    }
    字符可能有写的不正确的地方,就是这么个意思,自已搞下.