Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi!
I need help.
I want to format part of a String for a DataGridViewCel in vb.net.

The thing that I'm doing is, inputting details of a person in a datagridview.
So the data is supposed to be formatted with different fonts and then displayed on the DGV cell.
SO bascially my data consists of "Name", "Details 1..." and "Details 2..."
My "Name" has to be bold and the size of that has to be larger than the other text.
"Details 1" and "Details 2" are of the same size and font.
All these 3 come on 3 different lines.

So what I've done till now...
1. I've set multiline property to true in the DGV
VB
Private Sub DGV_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles Me.CellFormatting
        e.CellStyle.WrapMode = DataGridViewTriState.True
        Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        Me.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
End Sub


2. I've created a column and added rows into it
VB
Me.Rows.Add(PName & Environment.NewLine & Assembly & Environment.NewLine & Sex & ", " & Group & ", " & Custom)

(All variables are of String type)
You see, the Newline variables separate the three regions of data.

Question: How do I set only "PName" to a different font, different size and make it bold and leave the rest of the string as it is?
Posted

How? You don't!

It works exactly the same as a normal TextBox control. You can only have a single font in a cell.

You cannot use multiple fonts, sizes and formatting styles in the same cell using the default TextBox column. In order to implement this, you'd have to design your own version of a DataGridViewTextBoxColumn using a RichTextBox control instead of a TextBox control.

Warning! Creating your own custom DGV column types is NOT easy to do! There's a lot of ground to cover. You can start by going over this list[^] of articles on the subject.
 
Share this answer
 
v2
Comments
Abey Kuruvila 14-Jun-12 12:07pm    
@Dave.
I came across the "RichTextBoxCell" solution online...it was too complicated for me to understand as I couldn't understand the flow of events in implementing it. It'll be really nice if you could in simple steps explain how to implement the richtextbox solution. THanks :-D
Dave Kreskowiak 14-Jun-12 13:20pm    
There are no "simple steps" for creating a custom DGV Column. It's a complex control to create and you have to create a few of them so you can handle displaying data and editing data in the same column.

I already told you, go through the list of articles I linked to. Keep banging away at it until it clicks. This doesn't get any easier than those examples.
Abey Kuruvila 14-Jun-12 14:01pm    
Well...
Ok. I shall try my fortune on those links :)
Thanks for the help.
VB
For Each gr1 As GridViewRow In gridview1.Rows
gr1.Cells(0).Font.Bold = True
gr1.Cells(0).Font.Size = 12
gr1.Cells(1).Font.Bold = False
gr1.Cells(1).Font.Size = 10
gr1.Cells(2).Font.Bold = False
gr1.Cells(2).Font.Size = 10
Next

gridview1 is the name of gridview
It write the Code of Binddata in Grid View
 
Share this answer
 
v2
Comments
Abey Kuruvila 14-Jun-12 12:06pm    
@Prasad.
That would work if it is multiple cells. But mine is just a single cell, multiline-wraped.
Upniwesh 15-Jun-12 1:00am    
Dim gr1 As GridViewRow
For counter As Int32 = 0 To gridview1.Rows -1 Step 1
gr1 = gridview1.Rows(counter)
If counter = 0 Then
gr1.Cells(0).Font.Bold = True
gr1.Cells(0).Font.Size = 12
Else
gr1.Cells(0).Font.Bold = False
gr1.Cells(0).Font.Size = 10
End If
Next

i hope it will be work...........
Upniwesh 15-Jun-12 1:03am    
sorry in above code 1 mistake

Dim gr1 As GridViewRow
For counter As Int32 = 0 To gridview1.Rows.Count -1 Step 1
gr1 = gridview1.Rows(counter)
If counter = 0 Then
gr1.Cells(0).Font.Bold = True
gr1.Cells(0).Font.Size = 12
Else
gr1.Cells(0).Font.Bold = False
gr1.Cells(0).Font.Size = 10
End If
Next

Check this and try......
Abey Kuruvila 18-Jun-12 14:06pm    
Hey. Thanks for that reply.
But again...that code will make the entire contents of the cell bold, which is not my purpose. Its only part of the cell contents that I wanna make bold...Like say, if my cell contains, "This is a bold text", I would want only "bold" to be made bold and of a different font size.

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