none
datagridview 首次加载问题 RRS feed

  • 问题

  • datagridview首次加载时,对其中一些行修改背景颜色无效

    代码如下:

    dataGridViewLVDC.AutoGenerateColumns = false;
                //加载datasource数据源
                dataGridViewLVDC.DataSource = LDCBll.Get_CabinetList(tableName, projectNumber, cabinetNumberSelect1, cabinetOrder1, cabinetNumberSelect2, cabinetOrder2);
                //设置列的模式和初始不可见
                foreach (DataGridViewColumn column in dataGridViewLVDC.Columns)
                {
                    column.SortMode = DataGridViewColumnSortMode.NotSortable;
                    column.Visible = false;
                }
                //设置列宽为显示自动调整模式
                dataGridViewLVDC.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
                dataGridViewLVDC.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False;
    
                //遍历每列,有数据的显示
                for (int j = 0; j < dataGridViewLVDC.Rows.Count; j++)
                {
                    for (int i = 19; i < dataGridViewLVDC.Columns.Count; i++)
                    {
                        if (dataGridViewLVDC.Rows[j].Cells[i].Value.ToString() != "0")
                            dataGridViewLVDC.Columns[i].Visible = true;
                        if (dataGridViewLVDC.Rows[j].Cells[i].Value.ToString() == "0")
                            dataGridViewLVDC.Rows[j].Cells[i].Value = "";
                    }
                }
                //设置行高等参数
                dataGridViewLVDC.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
                dataGridViewLVDC.ColumnHeadersHeight = 28;
                dataGridViewLVDC.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dataGridViewLVDC.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                //设置其中某些行的背景颜色
                for (int i = 0; i < dataGridViewLVDC.Rows.Count; i++)
                {
    
                    if (Convert.ToInt32(dataGridViewLVDC.Rows[i].Cells["CabinetNumber"].Value.ToString().Split('N')[1]) % 2 != 0)
                    {
                        dataGridViewLVDC.Rows[i].DefaultCellStyle.BackColor = Color.LightBlue;
                    }
                    else if (Convert.ToInt32(dataGridViewLVDC.Rows[i].Cells["CabinetNumber"].Value.ToString().Split('N')[1]) % 2 == 0)
                    {
                        dataGridViewLVDC.Rows[i].DefaultCellStyle.BackColor = Color.LightGreen;
                    }
                }

    经过测试,每行代码都运行了,但是显示发现设置某些行的背景颜色这个要求并没有实现,请问这个是什么问题呢?

    后面改用其他方法,再次调用datagridview的加载事件,却可以实现分行显示背景颜色的功能


    • 已编辑 yzjjhk 2018年5月2日 8:12
    2018年5月2日 8:11

答案

  • 你好,

    >>经过测试,每行代码都运行了,但是显示发现设置某些行的背景颜色这个要求并没有实现,请问这个是什么问题呢?

    这应该是Binding 是需要时间的,你在bindsource的是时候直接做这个判断,渲染的时候,没有得到你的信息。建议把变色放在DataBindingComplete事件中。像这样:

    privatevoid dataGridViewLVDC_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
       //设置列的模式和初始不可见
               
    foreach (DataGridViewColumn column in dataGridViewLVDC.Columns)
               
    {
                    column
    .SortMode = DataGridViewColumnSortMode.NotSortable;
                    column
    .Visible = false;
               
    }
               
    //设置列宽为显示自动调整模式
                dataGridViewLVDC
    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
                dataGridViewLVDC
    .ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False;

               
    //遍历每列,有数据的显示
               
    for (int j = 0; j < dataGridViewLVDC.Rows.Count; j++)
               
    {
                   
    for (int i = 19; i < dataGridViewLVDC.Columns.Count; i++)
                   
    {
                       
    if (dataGridViewLVDC.Rows[j].Cells[i].Value.ToString() != "0")
                            dataGridViewLVDC
    .Columns[i].Visible = true;
                       
    if (dataGridViewLVDC.Rows[j].Cells[i].Value.ToString() == "0")
                            dataGridViewLVDC
    .Rows[j].Cells[i].Value = "";
                   
    }
               
    }
               
    //设置行高等参数
                dataGridViewLVDC
    .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
                dataGridViewLVDC
    .ColumnHeadersHeight = 28;
                dataGridViewLVDC
    .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dataGridViewLVDC
    .RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
               
    //设置其中某些行的背景颜色
               
    for (int i = 0; i < dataGridViewLVDC.Rows.Count; i++)
               
    {

                   
    if (Convert.ToInt32(dataGridViewLVDC.Rows[i].Cells["CabinetNumber"].Value.ToString().Split('N')[1]) % 2 != 0)
                   
    {
                        dataGridViewLVDC
    .Rows[i].DefaultCellStyle.BackColor = Color.LightBlue;
                   
    }
                   
    else if (Convert.ToInt32(dataGridViewLVDC.Rows[i].Cells["CabinetNumber"].Value.ToString().Split('N')[1]) % 2 == 0)
                   
    {
                        dataGridViewLVDC
    .Rows[i].DefaultCellStyle.BackColor = Color.LightGreen;
                   
    }
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 yzjjhk 2018年5月4日 1:31
    2018年5月3日 7:17
    版主

全部回复

  • 你好,

    >>经过测试,每行代码都运行了,但是显示发现设置某些行的背景颜色这个要求并没有实现,请问这个是什么问题呢?

    这应该是Binding 是需要时间的,你在bindsource的是时候直接做这个判断,渲染的时候,没有得到你的信息。建议把变色放在DataBindingComplete事件中。像这样:

    privatevoid dataGridViewLVDC_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
       //设置列的模式和初始不可见
               
    foreach (DataGridViewColumn column in dataGridViewLVDC.Columns)
               
    {
                    column
    .SortMode = DataGridViewColumnSortMode.NotSortable;
                    column
    .Visible = false;
               
    }
               
    //设置列宽为显示自动调整模式
                dataGridViewLVDC
    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
                dataGridViewLVDC
    .ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False;

               
    //遍历每列,有数据的显示
               
    for (int j = 0; j < dataGridViewLVDC.Rows.Count; j++)
               
    {
                   
    for (int i = 19; i < dataGridViewLVDC.Columns.Count; i++)
                   
    {
                       
    if (dataGridViewLVDC.Rows[j].Cells[i].Value.ToString() != "0")
                            dataGridViewLVDC
    .Columns[i].Visible = true;
                       
    if (dataGridViewLVDC.Rows[j].Cells[i].Value.ToString() == "0")
                            dataGridViewLVDC
    .Rows[j].Cells[i].Value = "";
                   
    }
               
    }
               
    //设置行高等参数
                dataGridViewLVDC
    .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
                dataGridViewLVDC
    .ColumnHeadersHeight = 28;
                dataGridViewLVDC
    .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dataGridViewLVDC
    .RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
               
    //设置其中某些行的背景颜色
               
    for (int i = 0; i < dataGridViewLVDC.Rows.Count; i++)
               
    {

                   
    if (Convert.ToInt32(dataGridViewLVDC.Rows[i].Cells["CabinetNumber"].Value.ToString().Split('N')[1]) % 2 != 0)
                   
    {
                        dataGridViewLVDC
    .Rows[i].DefaultCellStyle.BackColor = Color.LightBlue;
                   
    }
                   
    else if (Convert.ToInt32(dataGridViewLVDC.Rows[i].Cells["CabinetNumber"].Value.ToString().Split('N')[1]) % 2 == 0)
                   
    {
                        dataGridViewLVDC
    .Rows[i].DefaultCellStyle.BackColor = Color.LightGreen;
                   
    }
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 yzjjhk 2018年5月4日 1:31
    2018年5月3日 7:17
    版主
  • 果然是这个问题,谢谢版主
    2018年5月4日 1:31