C# DataGridView 单行颜色文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1499188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# DataGridView Color text of single row
提问by neildeadman
I have a DataGridView that has a class as its DataSource.
我有一个 DataGridView,它有一个类作为它的数据源。
The class contains an ID string, 2x DateTimes and a Boolean.
该类包含一个 ID 字符串、2x 日期时间和一个布尔值。
I have written some code to change the text of a row that matches an ID I pass to method to Red, but nothing I have tried works.
我已经编写了一些代码来更改与我传递给方法的 ID 匹配的行的文本到 Red,但我尝试过的任何方法都不起作用。
This is what I have so far:
这是我到目前为止:
public void ShowInstanceAsTerminated(String id)
{
foreach (DataGridViewRow dgvRow in dgvRIM.Rows)
{
if (dgvRow.Cells[0].Value.ToString() == id)
{
dgvRow.DefaultCellStyle.ForeColor = Color.Red;
}
}
}
This is one of many variations of code I have tried, but the cells in question never change!!
这是我尝试过的许多代码变体之一,但有问题的单元格永远不会改变!!
Thanks Neil
谢谢尼尔
采纳答案by Eric J.
Try this format:
试试这个格式:
dgvRIM.Rows[myRow].Cells[0].Style.ForeColor = Color.Red;
If you want to set the entire row, loop over all cells.
如果要设置整行,请遍历所有单元格。
回答by David
You need to change the individual cells.
您需要更改单个单元格。
This is VB code lifted from an app where I am able to successfully do this, but it shouldn't be to hard to convert this to c#.
这是从我能够成功执行此操作的应用程序中提取的 VB 代码,但将其转换为 c# 应该不难。
For i As Integer = 0 To ds.Tables("AddressChanges").Rows.Count - 1
If ds.Tables("AddressChanges").Rows(i)("iSeriesAddress").ToString <> ds.Tables("AddressChanges").Rows(i)("CSIAddress").ToString() Then
Me.dgAddressDiscrepancies.Rows(i).Cells("iSeriesAddress").Style.BackColor = Color.Yellow
Me.dgAddressDiscrepancies.Rows(i).Cells("CSIAddress").Style.BackColor = Color.Yellow
End If
Next
回答by Jay Riggs
Use the DataGridView's CellFormatting event to make changes to individual cells that compose a row.
使用 DataGridView 的 CellFormatting 事件对组成一行的单个单元格进行更改。
Something like this (beware, not tested):
像这样的东西(当心,未经测试):
private void dgvRIM_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
if (dgvRIM.Rows[e.RowIndex].Cells[0].Value.ToString() == id) {
e.CellStyle.ForeColor = Color.Red;
}
}
回答by Meta-Knight
I just tested your code (setting the ForeColor
with DefaultCellStyle
) and it DOES work (with .NET 3.5, but I don't think Winforms has evolved since 2.0).
我刚刚测试了您的代码(设置ForeColor
with DefaultCellStyle
)并且它确实有效(使用 .NET 3.5,但我认为 Winforms 自 2.0 以来没有发展)。
Now I don't know why it doesn't work for you... maybe you call the code before rows are added, or you reload the rows after your code is called??
现在我不知道为什么它对你不起作用......也许你在添加行之前调用代码,或者在调用代码后重新加载行?
Either way, you're probably better off with Jay Riggs's solution, as it will work even if you add new rows afterwards.
无论哪种方式,使用 Jay Riggs 的解决方案可能会更好,因为即使您之后添加新行它也能工作。
回答by King_Rob
I believe the solution lies in WHEN you set the color, not the method in which you do so. Several different events have been suggested and some will indeed work. One of the problems with using either the cellformatting, databindingcomplete, or even paint events is that they get fired multiple times. From what I've gathered, there is an issue with the datagridview control in that you cannot change the color of any of the cells until AFTER the form has been shown. Thus methods that run, or events that fire before Shown() is called will not change the color. The events that are sited as the solution to the problem usually work, but since they're called many times, may not be the most efficient answer.
我相信解决方案在于您设置颜色的时间,而不是您设置颜色的方法。已经提出了几种不同的事件,有些确实会起作用。使用 cellformatting、databindingcomplete 甚至 Paint 事件的问题之一是它们会被多次触发。从我收集到的信息来看,datagridview 控件存在一个问题,即在显示表单之前您无法更改任何单元格的颜色。因此,在调用 Shown() 之前运行的方法或触发的事件不会改变颜色。定位为问题解决方案的事件通常有效,但由于它们被多次调用,可能不是最有效的答案。
Probably the simplest solution to the issue is to put your code to fill/color your grids in the Shown() method of your form instead of the constructor. Below is a link to a post in the msdn forums that tipped me off to the solution, it's marked as the answer about 3/4 of the way down the page.
这个问题最简单的解决方案可能是在表单的 Shown() 方法而不是构造函数中放置用于填充/着色网格的代码。下面是 msdn 论坛中向我提示解决方案的帖子的链接,它被标记为页面下方约 3/4 处的答案。
回答by Jagadesh
This will work fine
这将正常工作
private void grid1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow row = grid1.Rows[e.RowIndex];// get you required index
// check the cell value under your specific column and then you can toggle your colors
row.DefaultCellStyle.BackColor = Color.Green;
}
回答by Nima
It seems there is a bug with TabControl
that when it has more than one tab, cellstyles
that are created with code are only applied to the first tab's DataGridView, so to solve the problem you can move your DataGridView
to the first tab or you can use the TabControl's SelectedIndexChanged
event and put your styling code in this event .
似乎有一个错误TabControl
,当它有多个选项卡时,cellstyles
使用代码创建的代码仅适用于第一个选项卡的 DataGridView,因此要解决该问题,您可以将您移动DataGridView
到第一个选项卡,或者您可以使用 TabControl 的SelectedIndexChanged
事件并将您的样式代码放在此事件中。
回答by Aryan Alipour
row.DefaultCellStyle.ForeColor = Color.Red;
This works for .NET 4.0 and upwards at least.
这至少适用于 .NET 4.0 及更高版本。