试了下边事件无效

private void dgv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)

试了下边事件也无效 (但有的地方可以,有空详细分析下)

  /// <summary>
        /// 小计行变颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv_DataSourceChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                dgv.Rows[i].Cells["单号"].Value.ToString();
                if (dgv.Rows[i].Cells["单号"].Value.ToString().Contains("小计")) //只对小计行进行加粗
                {
                    dgv.Rows[i].DefaultCellStyle.Font =
                           new Font("微软雅黑", 12F, FontStyle.Bold);
                    // dgv.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                }
            }
            dgv.Refresh();
        }

 

datagridview控件在显示窗体之前无法更改颜色。 load 事件也不好使.

下边事件可以.

private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    // Assuming 20 is the column index you're looking for
    if (this.dataGridView2.Columns[e.ColumnIndex].Index == 20)
    {
        if (e.Value != null)
        {
            string strVal = (string)e.Value;
            if (strVal != "")
            {
                e.CellStyle.BackColor = Color.Red;
            }
        }
    }
}

 

posted on 2022-10-11 16:10  小石头的一天  阅读(136)  评论(0编辑  收藏  举报