【问题标题】:Color a null cell not all the cells [duplicate]为空单元格着色而不是所有单元格[重复]
【发布时间】:2013-02-01 13:25:51
【问题描述】:

可能重复:
Show message in empty cells in GridView

通过该代码,我将所有单元格着色 我只需要一个空的.. 有人能注意到我的错在哪里吗(对不起,我是 C# 新手)

    private void simpleButton1_Click(object sender, System.EventArgs e)
    {

        string[] msg = new string[60];
        string[] error = new string[400];
        for (int i = 0; i < gridView3.RowCount ; i++)
        {
            System.Data.DataRow Rows = gridView3.GetDataRow(i);
            string cellvalue = Rows[0].ToString();
            if (cellvalue == "")
            {
                msg[0] = "Missing 'First Name'";
                error[i] = msg[0] + " - ";  
                gridView3.CustomDrawCell += new RowCellCustomDrawEventHandler(gridView3_CustomDrawCell);

            }
        }
    }
    private void gridView3_CustomDrawCell_1(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
    {
        e.Style.BackColor = Color.LightSteelBlue;
    }

顺便说一下,这个表格是从 Excel 导入的

【问题讨论】:

    标签: c# devexpress


    【解决方案1】:

    您只需要注册一次网格事件,而不是在循环中。
    对于单元格着色,请使用 RowCellStyle 事件。

    Customizing Appearances of Individual Rows and Cells help topic.

    【讨论】:

      【解决方案2】:

      你可以试试 String.IsNullOrEmpty(cellvalue) 如果为真,那么做你的事。

      如前所述,您可能希望先循环遍历单元格

      【讨论】:

        【解决方案3】:

        嗯,我认为您还需要循环其单元格,即

        foreach (GridViewRow row in gridView3.Rows)
        {
          foreach (GridViewCell cell in row.Cells)
          {
            if (cell.Text == "")
            {
             //Do stuff here color etc
            }
          }
        }
        

        一旦您可以访问 CELL 对象,您就可以在那里设置颜色,即(System.Drawing 命名空间)

        cell.BackColor = Color.Green
        

        【讨论】:

        • 我在第一行 foreach 语句中收到此错误,因为 'int' 不包含 'GetEnumerator' 的定义,或者无法访问
        猜你喜欢
        • 2013-07-31
        • 1970-01-01
        • 1970-01-01
        • 2022-01-03
        • 2013-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多