Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Changing Font in DataGridView

Posted on 2009-06-30
7
980 Views
Last Modified: 2012-05-07
Hi, I'm using VB 2005, WinForms. I have a DataGridView that contains a list of users. Some users are "active" users and some are "inactive." I use a BindingSource Filter to show each type. -- All that works great.

However, for the sake of clarity, I'd like to change the Font of the "inactive" users to a font with a "strike-through" line. Hence, showing that the user has been crossed out or removed from the list in some graphic manner.

How can I change my DataGridView's Font to "strike-through"?

I tried >>   dgvMyUsers.Font.Strikeout = True , but that doesn't work. It gets a squiggly blue line under it. What's missing?

Thanks for your help,
Fulano
0
Comment
Question by:Mr_Fulano
  • 5
  • 2
7 Comments
 
LVL 8

Accepted Solution

by:
Bob Hoffman earned 500 total points
ID: 24751362
there is one event named "DataGridView1_RowsAdded" you can add your code to change font color in that event. The event fire when the datasource is bound to your grid.


Private Sub DataGridView1_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded 
For Each r1 As DataGridViewRow In DataGridView1.Rows 
For Each cell As DataGridViewCell In r1.Cells 
cell.Style.BackColor = Color.DarkRed 
cell.Style.Font = New Font("Tahoma", 8, FontStyle.Bold) 
Next 
Next 
End Sub

Open in new window

0
 

Author Comment

by:Mr_Fulano
ID: 24751452
HI HBHoffman, I don't want to change all my rows. I just want to change the row Font of specific rows that contain certain filtered data. I was hoping to add a single line of code that would change a row's Font style when something happens in a logic statement or a filter request. Your code seems to want to change all the rows.  

There has to be something in my code below that intelli-sense doesn't like or that is missing.

dgvMyUsers.Font.Strikeout = True

Thanks for your help,
Fulano
0
 

Author Comment

by:Mr_Fulano
ID: 24751466
Hi HBHoffman,

I figured it out...its:

dgvMyUsers.Font = New Font(dgvMyUsers.Font, FontStyle.Strikeout)

I'll award you the points for trying to help...thanks anyways!

Fulano
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Closing Comment

by:Mr_Fulano
ID: 31598632
The solution is not what worked for me. Please see my post below for a solution to my question that I worked out myself. -- Thanks for trying!!!
0
 

Author Comment

by:Mr_Fulano
ID: 24751567
NOTE: Here is a version of HBHoffman's code that will change only the "Inactive" records.

In my code an empty string is equal to an active user.

HBHoffman's code was good, but I needed to play with it to make it work for my needs. However, HBHoffman did a great job in trying to help me an in producing code that helped me get to where I needed to get to.

Thanks again,
Fulano
Private Sub DataGridView1_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded 
For Each r1 As DataGridViewRow In DataGridView1.Rows 
For Each cell As DataGridViewCell In r1.Cells 
If r1.Cells("CurrentStatus").Value.ToString Is String.Empty Then
                    cell.Style.Font = New Font("Microsoft San Serif", 8, FontStyle.Regular)
                Else
                    cell.Style.Font = New Font("Microsoft San Serif", 8, FontStyle.Strikeout)
                End If
Next 
Next 
End Sub

Open in new window

0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24756066
Yea, I wasn't sure what condition constituted an "inactive" user. I probably should have included the code that demonstrates how to test cell values. Glad you got it fugured out.
0
 

Author Comment

by:Mr_Fulano
ID: 24760575
I was able to figure it out only with your help HBHoffman. Thank you for your assistance as it was very valuable.

Fulano
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Gridview selected row 9 49
Database Connections Not being returned to Connection Pool 7 34
ASP/VB email question 4 46
VB.Net - MemoryMappedFiles - Confirm receipt 2 24
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question