以编程方式在datagridview中根据其值选择单元格

时间:2015-02-02 11:14:08

标签: c# winforms list datagridview

正如标题所述。 我在winforms应用程序中使用字符串列表(单个未命名的列)填充dataGridview,但是我需要一种方法来选择单个卖出它的值以更改文本颜色。

我该怎么做?

这就是我填充它的方式:

List<String> companyNames = new List<String>();

//.. other logic that fills up companyNames

companyDataGridView.DataSource = this.companyNames.Select(x => new { Value = x }).ToList();

之后我需要以某种方式根据列表中的值选择单元格。

3 个答案:

答案 0 :(得分:0)

您可以在html中使用eval,这意味着您可以在每一行中调用服务器端函数并确定其颜色。

幸运的是我最近已经使用过这个例子:

                <asp:TemplateField HeaderText="Level Achieved">
                    <ItemTemplate>
                        <asp:Label ID="lblActual" runat="server" Text='<%# Bind("Actual") %>' BackColor='<%# GetLevelColour(Eval("Target"),Eval("Actual"))%>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlAchievedLevel" runat="server">
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>

在这里你可以看到单元格的背面颜色是由函数GetLevelColour返回的内容决定的,我还传入了函数中使用的两个参数。

这可能是也可能不是你需要的功能,但是我对这个问题的解释是当它的值等于一个预定值时,如何改变一个单元格的颜色(在这种情况下,长度为2的单词为长度输出到单元格而不是字符串)。

我对Winforms了解不多,但是我试了一下。虽然我无法获得列表来绑定字符串,但我可以将值拉出并与已知值进行比较。这是你要找的东西吗?

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[0].Value.ToString() == "2")
                {
                //To colour the row
                row.DefaultCellStyle.BackColor = Color.Red;

                //To select the row
                row.Selected = true;
                }
            }
        }

答案 1 :(得分:0)

这可能不是很漂亮,但我最终将它们放在一起..它似乎有效。

private DataGridViewCell GetDataGridViewCellByValue(String value, DataGridView dataGridView)
        {
            List<DataGridViewCell> myCells = new List<DataGridViewCell>();
            foreach (DataGridViewRow myRow in dataGridView.Rows)
            {
                foreach (DataGridViewCell myCell in myRow.Cells)
                {
                    myCells.Add(myCell);
                }
            }

            DataGridViewCell returnCell = myCells.FirstOrDefault(x => x.Value.ToString() == value);
            if (returnCell != null)
            {
                return returnCell;
            }
            return null;
        }

然后只需使用下面的内容即可获得所需的单元格:

DataGridViewCell testCell = this.GetDataGridViewCellByValue("Company1", this.companyDataGridView);
            if (testCell != null)
            {
                testCell.Style.ForeColor = Color.Blue;
            }

答案 2 :(得分:0)

这可以是一个可扩展且快速的解决方案:

Dictionary<string, Color> valuesForColors = new Dictionary<string, Color>();

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.FormattedValue != null && valuesForColors.ContainsKey(e.FormattedValue.ToString()))
        e.CellStyle.ForeColor = e.CellStyle.SelectionForeColor = valuesForColors[e.FormattedValue.ToString()];
}