当前位置: 代码迷 >> C# >> DataGridView行的样式有关问题
  详细解决方案

DataGridView行的样式有关问题

热度:148   发布时间:2016-05-05 05:21:54.0
DataGridView行的样式问题
// 获取应该变色的行
            int thenSelectedRowIndex = 0;
            try
            {
                foreach (DataGridViewRow row in productsDataGridView.Rows)
                {
                    string productcount = row.Cells[5].Value.ToString();
                    string sncount = row.Cells[6].Value.ToString();
                    if (productcount == sncount)
                    {
                        thenSelectedRowIndex = row.Index;
                        productsDataGridView.Rows[thenSelectedRowIndex].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                        productsDataGridView.Rows[thenSelectedRowIndex].DefaultCellStyle.SelectionBackColor = Color.Red;
                        productsDataGridView.Rows[thenSelectedRowIndex].DefaultCellStyle.SelectionForeColor = Color.White;
                        productsDataGridView.Rows[thenSelectedRowIndex].DefaultCellStyle.BackColor = Color.Yellow;
                        productsDataGridView.Rows[thenSelectedRowIndex].DefaultCellStyle.ForeColor = Color.Black;
                        productsDataGridView.Rows[thenSelectedRowIndex].DefaultCellStyle.Font = new Font("黑体", 27, FontStyle.Bold, GraphicsUnit.Pixel); 
                    }
                }
            }
            catch(Exception ex)
            {
                string erromsg = ex.Message.ToString();
            }

在是网上找了很长时间,都是说这样可以改变样式的。 可是为什么我尝试了很久都没反应,改不了。是不是控件有什么属性设置不允许修改这些属性?
------解决思路----------------------
既然你已经使用foreach循环了,代码就直接
row.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
不好吗
或者都改成for循环,然后使用rowindex,不要混用
------解决思路----------------------
对单元格 或行进行动态的展现形式,建议在cellformatting事件或者cellpainting事件中处理
  相关解决方案