简体   繁体   English

如何根据字符串值更改datagridview行颜色

[英]How to change datagridview row color based on string value

I've tried searching the forums to find the answer, but they all seem to check for an integer value, not a string. 我试过搜索论坛找到答案,但他们似乎都检查整数值,而不是字符串。 I've got a C# form pulling data values from an SQL table and displaying them on a datagridview. 我有一个C#表单从SQL表中提取数据值并在datagridview上显示它们。 The column I wish check the string value for is labeled "Type", and is the third column in my data table. 我希望检查字符串值的列标记为“Type”,并且是我数据表中的第三列。 I wish to highlight all rows red that contain the word "pharmacy" in the third column. 我希望在第三列中突出显示包含“药房”一词的所有红色行。

private void invTableDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        foreach (DataGridViewRow row in invTableDataGridView.Rows)
        {
            if (row.Cells[2].Value.ToString() == "Pharmacy")
            {
                invTableDataGridView.DefaultCellStyle.BackColor = Color.Red;
            }
        }

    }

When I run the program with this code, nothing changes, even when I conduct actions that should trigger the databindingcomplete event. 当我使用此代码运行程序时,即使我执行应触发数据绑定完成事件的操作,也不会发生任何变化。 Frustratingly, if I change row.Cells[2] to row.Cells[3] where I have integer values, and type 令人沮丧的是,如果我将row.Cells [2]更改为row.Cells [3],其中我有整数值,并键入

    if (row.Cells[3].Value.ToString() == "0")

the code works, but for the wrong column. 代码有效,但对于错误的列。 It only seems to work with integers, but I want it to work with a string. 它似乎只适用于整数,但我希望它能用于字符串。 How do I do this? 我该怎么做呢?

Edit: Yes, sorry, I should have been consistent. 编辑:是的,抱歉,我应该保持一致。 The value I wish to compare to is "Pharmacy", capitalized. 我想要比较的价值是“药房”,大写。

Edit2: Ok I found out why it was giving me so much trouble. 编辑2:好的,我发现为什么它给了我这么多麻烦。 The values in column[2] were being added in by a combobox, and for some reason it always adds two spaces after the word even though it does not show the spaces in the string collection editor. 第[2]栏中的值是由组合框添加的,由于某种原因,它总是在单词后添加两个空格,即使它没有在字符串集合编辑器中显示空格。

Because your code works with column of integers, then problem must be in the string comparing. 因为您的代码使用整数列,所以问题必须在字符串比较中。
As I mentioned in the comments check what data you have and value comparing to. 正如我在评论中提到的,检查您拥有的数据和值的比较。
Or you can execute ToLower() on the cell value and compare it to "pharmacy" 或者您可以对单元格值执行ToLower()并将其与“药房”进行比较
And you can use CellFormatting event handler. 您可以使用CellFormatting事件处理程序。 This event should be good for formatting control based on the values 此事件应该适用于基于值的格式控制

private void invTableDataGridView_CellFormatting(Object sender,
                                                 DataGridViewCellFormattingEventArgs e)
{
    Int32 yourindexColumn = 2;
    if (e.RowIndex < 0 || e.ColumnIndex <> yourindexColumn)
        Return;
    //Compare value and change color
    DataGridViewCell cell = invTableDataGridView.Rows[e.RowIndex].Cells[yourindexColumn]
    String value = cell.Value == null ? string.Empty : cell.Value.ToString()
    if (value.ToLower().Equals("pharmacy") == true)
    {
        invTableDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM