Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Cell in DataGridView and Change Color

Status
Not open for further replies.

ousoonerjoe

Programmer
Jun 12, 2007
925
0
0
US
Using VB.Net 2010.

Using a DataGridView to display/edit scheduling information, the user specifies color values that are stored in a separate table. The table contains the Column Name (variable columns, ID's won't work here), JobNum (a unique value that identifies the row), and ColorID. Where the issue comes in, the grid could have 500+ rows with anywhere from 5 to 25 columns per row. As Jobs complete, they fall off the grid and remaining rows shift up making the use of storing the Row Index obsolete. When loading and setting the specified cell backgrounds, it can get rather time consuming to process before returning control back to the user.

Obviously, looping through the grid is an option, but was hoping there was a more efficient way. A quick ability to Find Row in a column with given value I think would be best, but am open to any other methods or ideas. Efficiency is primary here as the cell background colors are being set when the grid is populated.

Any assistance is appreciated. Thank you.

--------------------------------------------------
Bluto: What? Over? Did you say "over"? Nothing is over until we decide it is! Was it over when the Germans bombed Pearl Harbor? No!
Otter: Germans?
Boon: Forget it, he's rolling.
--------------------------------------------------
 
Hi,

Have you looked at the cellformatting of the datagridview?
If your column has always the same name you can use maybe this code.

Code:
    Private Sub dgvOverview_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvOverview.CellFormatting

        ' Check if row has been selected
        If dgvOverview.Rows(e.RowIndex).Selected = False Then
            ' Check if ship has been announced 
            If dgvOverview.Rows(e.RowIndex).Cells("Gemeld").Value = True Then
                ' Set the back color
                e.CellStyle.BackColor = Color.PaleGreen
            End If
        End If

    End Sub

Is the above code the compleet row changes to a color if a column contains a certain value.
If you want to change a certain cell you can have a check if the right column is hit.

Hope it helps you to get what you need.

Greetings,
Ernst Jan
 
I've thought about that, but with the color definitions being stored in a table on the database, the repeated calls would cause for some serious slow down and lag. I was hoping to load all at once while the user was expecting a "wait event". If there was a way to simply find the RowIndex, it would load so much faster and smoother.

--------------------------------------------------
Bluto: What? Over? Did you say "over"? Nothing is over until we decide it is! Was it over when the Germans bombed Pearl Harbor? No!
Otter: Germans?
Boon: Forget it, he's rolling.
--------------------------------------------------
 
Currently, the following loop is what is in place now:
Code:
For p = 0 To (PdgDt.Rows.Count - 1) Step 1
	JobNum = NotNull(PdgDt.Rows(p).Item("JobNum"), "")
	StageName = NotNull(PdgDt.Rows(p).Item("StageName"), "")
	BgColor = Color.FromArgb(NotNull(PdgDt.Rows(p).Item("CellColor"), 0))
	[b][COLOR=navy]For j = 0 To (dgAsyLine.Rows.Count - 1) Step 1
		If JobNum = dgAsyLine.Item("JobNum", j).Value Then
			dgAsyLine.Item(StageName, j).Style.BackColor = BgColor
		End If
	Next j[/color][/b]
	frmProgress.pgbMain.Value = (frmProgress.pgbMain.Value + 1)
	Application.DoEvents()
Next p
While it works and works well, it is SLOW. Ideally, removing the j For Next loop, i think would be a vast improvement, depending on how the Find works.

--------------------------------------------------
Bluto: What? Over? Did you say "over"? Nothing is over until we decide it is! Was it over when the Germans bombed Pearl Harbor? No!
Otter: Germans?
Boon: Forget it, he's rolling.
--------------------------------------------------
 
Can you link the color value during loading of the data from database?.
This way you have a column containing the needed color for that row. You can hide the color column.
Then using the cellformating to show the color.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top