Selecting all Cells in ONLY 1 Row

Mr_Fulano
Mr_Fulano used Ask the Experts™
on
Hi, I'm using VB 2005, WinForms. I want to change the Font of all the Cells within a Row on one of my DataGridViews. I can do the Font change using this code below, but this changes the Font of ALL the Cells in the DataGridView, not just the ones in the Row I want to change.

How can I limit the code to ONLY one cell if I know the Row index I want is: dgvRow.Index .

For Each dgvCell As DataGridViewCell In dgvRow.Cells

I need to do this below, but clearly this doesn't work:

For Each dgvCell As DataGridViewCell In dgvRow.IndexOF(dgvRow.Index )

Thanks,
Fulano
For Each dgvCell As DataGridViewCell In dgvRow.Cells
If dgvRow.Cells("CanceledDate").Value.ToString Is String.Empty Then
dgvCell.Style.Font = New Font("Microsoft San Serif", 8, FontStyle.Regular)
Else
dgvCell.Style.ForeColor = Color.Gray
dgvCell.Style.Font = New Font("Microsoft San Serif", 8, FontStyle.Strikeout)
End If
Next

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2013
Commented:

Author

Commented:
Hi Guru Sami,

There was a mistake in my original post, which I just noticed. I want to limit the code to ONLY >>ROW<< if I know the Row index I want is: dgvRow.Index , not to a specific cell. Sorry, for the confusion I created.

Can you assist with that question. If not, I will award you the points and repost a new question.

Thanks,
Fulano
Top Expert 2013

Commented:
I am not able to test it...but did you try this as in first exampl where you keep your rowIndex fixed and iterate through column index:
---> dataGridView1[columnIndex, rowIndex].Style.Font
Or something like DataGridView1.Rows[rowIndex].Cells[i].Style.Font...

Sorry I might be wrong...as I said I don't have VS right now to test this.
Also you don't have to close the post...may be someone else can look at your clarified question and comment appropriately.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Commented:
what is dgvRow in your sample ?

Are you interate through all of your rows .

Author

Commented:
Hi Jinal,

1). what is dgvRow in your sample ? >> dgvRow is each row in my DataGridView

2). Are you interate through all of your rows . >> I am iterating row by row, so yes.

Ideally, what I would like is to go row by row, and change the font style of ONLY the rows that have a Date in the CanceledDate cell of the DGV.

Thanks for your help,
Fulano


Author

Commented:
OK, here's an UPDATE...

I was able to solve PART of the problem...the code in the snippet below does exactly what I want it to do. -- So, you would think that we could say, problem solved, right...not so much!!!

Even though this code works for what I was looking for, it causes a huge problem. If I try to Sort the DataGridView (either before or after) it is shown, it will change the font style and color of ALL the rows.

But even worse, if I try to Sort the any of the columns by clicking on the header, it not only changes the font style, but it also locks up the application.

What the heck does this thing do?!?

Can someone shed some light on this one...?

BTW...this is the only thing wrong with my app. Everything else works fine thus far.

Thanks,
Fulano
Private Sub dgvUserList_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles dgvUserList.RowsAdded
 
For Each dgvRow As DataGridViewRow In dgvUserList.Rows
If dgvRow.Cells("CanceledDate").Value.ToString IsNot String.Empty Then
dgvUserList.Rows(dgvRow.Index).DefaultCellStyle.ForeColor = Color.Gray
dgvUserList.Rows(dgvRow.Index).DefaultCellStyle.Font = New Font("Microsoft San Serif", 8, FontStyle.Strikeout)
End If
Next
End Sub

Open in new window

Author

Commented:
OK, here is the solution!!!

You cannot use the _RowsAdded event like I was using...it really messes things up. The _CellFormatting event is a much better approach.

Thanks for all your help,
Fulano
Private Sub dgvUserList_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvUserList.CellFormatting
        If dgvUserList.Rows(e.RowIndex).Cells("CanceledDate").Value.ToString IsNot String.Empty Then
            e.CellStyle.ForeColor = Color.Gray
            e.CellStyle.Font = New Font("Microsoft San Serif", 8, FontStyle.Strikeout)
        End If
    End Sub

Open in new window

Author

Commented:
Good approach!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial