Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: string VB.NET DataGridView , +
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
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
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 14-Jun-12 4:17am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2
Comments
Abey Kuruvila at 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 at 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 at 14-Jun-12 14:01pm
   
Well...
Ok. I shall try my fortune on those links :)
Thanks for the help.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
Abey Kuruvila at 14-Jun-12 12:06pm
   
@Prasad.
That would work if it is multiple cells. But mine is just a single cell, multiline-wraped.
Upniwesh at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 385
1 Sergey Alexandrovich Kryukov 329
2 CPallini 270
3 DamithSL 214
4 Maciej Los 192
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 14 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100