如何根据同一行的第一个单元格中的值更改行背面颜色 [英] How Can I Change A Rows Backcolour Based On The Value In The First Cell Of The Same Row
问题描述
如果我有一个Datagridview,我怎么能意识到
1 abc abc
2 abc abc
1 abc abc
3 abc abc
现在所有排在3前面的行应该是背面绿色
i不知道如何使这成为可能
i尝试了这么多,但没有找到合适的方式
ty
sry for bad english im german
Hi how can i realize
if i have a Datagridview looks like
1 abc abc
2 abc abc
1 abc abc
3 abc abc
now all rows with a 3 infront of should be backcoloured green
i dont know how to make this possible
i tryed so much but dont find the right way
ty
sry for bad english im german
推荐答案
的属性System.Windows.Forms.DataGridViewCell.Style
是可读/写的:
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.style%28v=vs .110%29.aspx [ ^ ],
https:// msdn .microsoft.com / zh-CN / library / system.windows.forms.datagridviewcellstyle(v = vs.110).aspx [ ^ ]。
因此,您可以随时修改任何单元格的样式,或者对整行单元格进行修改。
整行,您可以使用属性System.Windows.Forms.DataGridViewColumn.DefaultCellStyle
:
https://msdn.microsoft.com/en-us/library/system.windows.forms。 datagridviewcolumn.defaultcellstyle(v = vs.110).aspx [ ^ ],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn%28v=vs.110%29 .aspx [ ^ ]。
-SA
The property ofSystem.Windows.Forms.DataGridViewCell.Style
is read/write:
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.style%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcellstyle(v=vs.110).aspx[^].
So, you can always modify the style of any cell at any time, or do so for the whole row of cells.
For the whole row, you can use the propertySystem.Windows.Forms.DataGridViewColumn.DefaultCellStyle
:
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.defaultcellstyle(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn%28v=vs.110%29.aspx[^].
—SA
如果您使用的是Asp.Net WebForms应用程序,请按以下步骤操作。
使用OnRowDataBound事件处理程序创建一个gridview,如下所示:
If you are using Asp.Net WebForms application, here is how you do it.
Create a gridview with OnRowDataBound event handler like below:
<asp:GridView ID="gvChangeColor" runat="server" OnRowDataBound="gvChangeColor_RowDataBound">
这里有一个示例代码根据某些条件更改行的背景颜色。您必须修改代码以满足您的需要。
And here is a sample code to change the background color of row based on some condition. You have to modify the code to suit your needs.
Protected Sub gvChangeColor_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(0).Text.StartsWith("3") Then
e.Row.BackColor = Color.Green
End If
End If
End Sub
如果您使用的是WinForms,请参考 Windows DataGridView中的GridView的RowDataBound等效事件 [ ^ ]
以下是如何在WinForms中执行此操作:
If you are using WinForms, then please refer this GridView's RowDataBound equivalent event in Windows DataGridView[^]
Here is how you do it in WinForms:
Private Sub dataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
If e.Value.ToString().StartsWith("3") Then
dataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Green
End If
End Sub
这篇关于如何根据同一行的第一个单元格中的值更改行背面颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!