Click here to Skip to main content
14,879,397 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 18-May-15 8:25am
v2
Comments
Mathi Mani 18-May-15 14:20pm
   
By datarow do you mean a row in data gridview? If it is a data gridview you can make use of RowDataBound event to add formatting to the loaded data, which in your case is changing background color.
Member 11700218 18-May-15 14:25pm
   
yes can you give me a code i dont understand how to realize this
Maciej Los 18-May-15 14:47pm
   
What have you tried? Where are you stuck?
What framework: WPF, WinForms, WebControls?
Sergey Alexandrovich Kryukov 18-May-15 16:10pm
   
WinForms, which can be seen by the simple type name "DataGridView". But this is a rare case; most of other simple type names are ambiguous, need full type names for clarification.
I answered the question, please see.
—SA
Sergey Alexandrovich Kryukov 18-May-15 16:12pm
   
What have you tried so far?
—SA

The property of System.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 property 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
   
v2
Comments
Maciej Los 18-May-15 16:52pm
   
My vote of 5, Maestro ;)
Sergey Alexandrovich Kryukov 18-May-15 17:01pm
   
Thank you very much, Maciej.
—SA
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.
C#
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

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:

C#
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
   
v3
Comments
Maciej Los 18-May-15 16:02pm
   
Tag is vb.net, not c#
Sergey Alexandrovich Kryukov 18-May-15 16:11pm
   
It would not be a big problem. Worst problem is: the question is on System.Windows.Forms; the answer is on ASP.NET; marked as "off-topic".
—SA
Maciej Los 18-May-15 16:24pm
   
Sergey, Mathi Mani included answer for WinForms.
Sergey Alexandrovich Kryukov 18-May-15 16:29pm
   
Better. It has other problems: 1) hard-coded '3', 2) what happens if the condition is false? the property remains the same? :-)
Thank you, Maciej.
—SA
Mathi Mani 18-May-15 16:35pm
   
Sergy, thank you for your suggestions. But if you look closely at the solution provided, you can find the words "sample code" and "You have to modify the code to suit your needs". And there is no need to down vote the solution as it has answer for Winforms. "3" was hard-coded to make it easy for the person raised the question to understand the solution and relate it to his problem in hand.
Maciej Los 18-May-15 16:51pm
   
Mathi, Sergey upvoted your answer (3 is neutral = no points). My vote of 4 for effort, and i'll promise to give a 5 stars, if you improve your answer as Sergey suggested. OK?
Sergey Alexandrovich Kryukov 18-May-15 17:05pm
   
I understand. I down-voted earlier on "off-topic" grounds (believe me, fast answers on wrong UI library is a problem here), and fixed that vote later, which makes zero points, as if there were no votes... As to the "3" in the sample: the problem is this: I do understand that this is only for illustration, but some naive readers takes it as a sample for real code style, so I would recommend to avoid misleading them.
—SA
Mathi Mani 18-May-15 16:40pm
   
Thanks you Maciej. Updated the solution to Vb.net.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900