473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Different styles in one DataGridView cell

116 64KB
Hi

I would like to show 2 lines in a DGV cell, the top line being a different font format than the 2nd line.

ie
<b>My Name</b>
(My Location)

Is there any way to do this? Even if it's using something like SelStart, counting the characters then changing the font and then doing the rest.... or changing formatting at the line break .... or override/paint etc?

Many thanks!
Apr 1 '13 #1

✓ answered by robertybob

Finally found the answer myself which is posted below in case anyone else needs something similar.

Many thanks to [_Joe_] on MS forums for work that led to this solution. Original MS post is here.

Expand|Select|Wrap|Line Numbers
  1.     Private Sub DGV_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
  2.         If e.ColumnIndex = 8 Then ' the column I want formatted
  3.             e.Handled = True
  4.             e.PaintBackground(e.CellBounds, True)
  5.             Dim val As String = DataGridView1.Item(8, e.RowIndex).Value
  6.             Dim CrLf() As Char = {vbCr, vbLf, vbCrLf}
  7.             Dim valstr = val.Split(CrLf) ' splits the rows
  8.             If valstr.Count >= 2 Then
  9.                 Dim upperfont As Font = New Font(e.CellStyle.Font, FontStyle.Bold)
  10.                 Dim lowerfont As Font = New Font("Arial", 8, FontStyle.Regular)
  11.                 Dim upperbrush As SolidBrush = New SolidBrush(Color.White)
  12.                 Dim lowerbrush As SolidBrush = New SolidBrush(Color.DarkGray)
  13.                 Dim upperstr As String = valstr(0)
  14.                 Dim lowerstr As String = valstr(2) ' 2nd part of split seems to be stored as empty so we need 3rd part
  15.                 Dim uppersize As SizeF = e.Graphics.MeasureString(upperstr, e.CellStyle.Font, e.CellBounds.Size)
  16.                 Dim lowersize As SizeF = e.Graphics.MeasureString(lowerstr, lowerfont, e.CellBounds.Size)
  17.                 e.Graphics.DrawString(upperstr, upperfont, upperbrush, e.CellBounds.X + ((e.CellBounds.Width - uppersize.Width) / 2), e.CellBounds.Location.Y + 5)
  18.                 e.Graphics.DrawString(lowerstr, lowerfont, lowerbrush, e.CellBounds.X + ((e.CellBounds.Width - lowersize.Width) / 2), e.CellBounds.Location.Y + 22)
  19.             Else
  20.                 e.PaintContent(e.CellBounds)
  21.             End If
  22.         End If
  23.     End Sub
  24.  

1 1798
robertybob
116 64KB
Finally found the answer myself which is posted below in case anyone else needs something similar.

Many thanks to [_Joe_] on MS forums for work that led to this solution. Original MS post is here.

Expand|Select|Wrap|Line Numbers
  1.     Private Sub DGV_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
  2.         If e.ColumnIndex = 8 Then ' the column I want formatted
  3.             e.Handled = True
  4.             e.PaintBackground(e.CellBounds, True)
  5.             Dim val As String = DataGridView1.Item(8, e.RowIndex).Value
  6.             Dim CrLf() As Char = {vbCr, vbLf, vbCrLf}
  7.             Dim valstr = val.Split(CrLf) ' splits the rows
  8.             If valstr.Count >= 2 Then
  9.                 Dim upperfont As Font = New Font(e.CellStyle.Font, FontStyle.Bold)
  10.                 Dim lowerfont As Font = New Font("Arial", 8, FontStyle.Regular)
  11.                 Dim upperbrush As SolidBrush = New SolidBrush(Color.White)
  12.                 Dim lowerbrush As SolidBrush = New SolidBrush(Color.DarkGray)
  13.                 Dim upperstr As String = valstr(0)
  14.                 Dim lowerstr As String = valstr(2) ' 2nd part of split seems to be stored as empty so we need 3rd part
  15.                 Dim uppersize As SizeF = e.Graphics.MeasureString(upperstr, e.CellStyle.Font, e.CellBounds.Size)
  16.                 Dim lowersize As SizeF = e.Graphics.MeasureString(lowerstr, lowerfont, e.CellBounds.Size)
  17.                 e.Graphics.DrawString(upperstr, upperfont, upperbrush, e.CellBounds.X + ((e.CellBounds.Width - uppersize.Width) / 2), e.CellBounds.Location.Y + 5)
  18.                 e.Graphics.DrawString(lowerstr, lowerfont, lowerbrush, e.CellBounds.X + ((e.CellBounds.Width - lowersize.Width) / 2), e.CellBounds.Location.Y + 22)
  19.             Else
  20.                 e.PaintContent(e.CellBounds)
  21.             End If
  22.         End If
  23.     End Sub
  24.  
Apr 4 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Gandalf | last post by:
I wish to apply different styles to items in a DropDownList. However, any styles applied in the codebehind (using the ListItem.Addtributes property) do not get rendered. This is an old question but...
0
by: Steve | last post by:
I am looking for an example of a custom user control ( 2 labels and 2 datetime pickers ) in a datagridview cell. Every example I have come across handles only once control such as a datetimepicker...
11
by: dgk | last post by:
Is there a way to change the foreground or background color of a single cell in an unbound datagridview?
3
by: Rich | last post by:
Hello, If I want to update data displayed in a datagrideview/datagridview cell, how can I determine what cell I am updating? I am looking at the click event below, for example. Can I get...
10
by: steve | last post by:
Hi All I would like to be able to change the cell borders on certain cells to none at runtime to make a group of cells appear to be merged I have tried the following in the cellformatting...
2
by: galletg | last post by:
please; how to write by code in a datagrid view cell a text in a textbox. thank you for your help. georges gallet
12
by: cj | last post by:
When viewing a datatable in a datagridview one of the columns in it is a "note" field which can be quite long. I would like to have the note field of the currently selected row of the datagrid...
0
by: varunk2 | last post by:
Hi How to autocomplete a datagridview cell in c# windows forms. Please anybody help me... Thanks in advance Regards,
0
by: hydro123 | last post by:
I am using VC++2008 and am trying to read data enetered in unbound datagridview to implement in function. Under button_click event I entered the following: // initialize varaibles from...
0
by: ziketo | last post by:
Hi, I searched a lot about changing the DataGridView cell borders. BYTES helped me so I will write down the solution: 1. You should override the class DataGridViewTextBoxCell, and the new class...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.