Question DataGridView - those pesky dots (ellipses), can I get rid of them?

BugMan

Well-known member
Joined
Dec 27, 2010
Messages
52
Location
Tallahassee FL
Programming Experience
10+
The DataGridView works nicely, but unless the text fits completely into the cell with margin to spare, the text is truncated and those truncated letters replaced with "...".

Is there any way to tell dgv NOT to add these pesky dots?
 
You could set WrapMode to True in the columns DefaultCellStyle.
There is also AutoSizeMode that can be set for the column to display all data.
 
You could set WrapMode to True in the columns DefaultCellStyle.
There is also AutoSizeMode that can be set for the column to display all data.

As always, thanks for your quick & knowledgeable reply John! The grid in my app is kinda like the touch screen at a fast food joint, and the rows & columns must not wrap or autosize. It sounds like the dots are not optional. They waste space in the cell, but I will just have to make the font smaller so that the text will fit into the cell more easily. Not desirable, but I'll have to live with it. Thanks again.
 
It is also possible to write a custom DataGridViewTextBoxCell and override painting to not draw ellipsis. Here is an example, you can change existing DataGridViewTextBoxColumn with NoEllipsisColumn.
VB.NET:
Public Class NoEllipsisColumn
    Inherits DataGridViewTextBoxColumn
    Public Overrides Property CellTemplate() As System.Windows.Forms.DataGridViewCell
        Get
            Return New NoEllipsisCell
        End Get
        Set(ByVal value As System.Windows.Forms.DataGridViewCell)
            MyBase.CellTemplate = value
        End Set
    End Property
End Class

Public Class NoEllipsisCell
    Inherits DataGridViewTextBoxCell
    Protected Overrides Sub Paint(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal cellBounds As System.Drawing.Rectangle, ByVal rowIndex As Integer, ByVal cellState As System.Windows.Forms.DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle, ByVal paintParts As System.Windows.Forms.DataGridViewPaintParts)
        paintParts = paintParts And Not DataGridViewPaintParts.ContentForeground
        MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
        Dim format As New StringFormat(StringFormatFlags.NoWrap)
        format.LineAlignment = StringAlignment.Center
        format.[B]Trimming [/B]= StringTrimming.Word 'or Character 
        Dim brush As New SolidBrush(cellStyle.ForeColor)
        graphics.DrawString(formattedValue, cellStyle.Font, brush, cellBounds, format)
        brush.Dispose()
        format.Dispose()
    End Sub
End Class
 
Uh Oh. I tried that code, surely did something wrong... got this error:

VB.NET:
Could not find any resources appropriate for the specified culture or the neutral culture.

I removed the code, but now when I try to view the form it won't show at all, and I get these error messages:

VB.NET:
BugWin 2011 Form1.Designer.vb Line:339 Column:1 Hide Call Stack
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager manager, String exceptionText, String helpLink)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializePropertyAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement, CodePropertyReferenceExpression propertyReferenceEx, Boolean reportError)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)


BugWin 2011 Form1.Designer.vb Line:354 Column:1 Hide Call Stack

at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager manager, String exceptionText, String helpLink)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializePropertyAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement, CodePropertyReferenceExpression propertyReferenceEx, Boolean reportError)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)

I quit VB and said no to 'save changes', but the problem still exists. There is some kind of autosave in VB, isn't there? Will have to turn that off.

So somehow I need to get my form back, hopefully I won't lose my work from today. I did back up my computer last night, so should be able to recover my work, though my lose a couple of hours of work. Any quick suggestions?
 
I have my form back. I just had to remove a couple of lines of code (machine generated) that defined the text of a tooltip. Can't imagine why that caused an error, since I entered that text into the Properties box for the control. Hopefully this is it for the squirrelly behavior.
 
I don't think that is related to that code, at least I can't see how it can in any way.
To use these classes you add a new class file to project and copy-paste the posted classes into it. Rebuild and the NoEllipsisColumn will appear as a valid column choice in the grids 'Add column' dialog. It can of course be added to grid in code also, just like any other column.
 
JohnH,

The problem was that I did not create a new class for your code but just put it into the Form1 class. VB.NET is a new world to me after VB6!

I see the NoEllipsisColumn option when adding columns at design time, but mine are created at run time. I don't quite understand what code to use when creating the columns without ellipses, can you tell me what to do? Probably just one line of code? Here's what I have so far...

VB.NET:
        For i = 1 To DataGrid.ColumnCount - 1 Step 2
            DataGrid.Columns(i). ' [COLOR="red"]Put "NoEllipsisColumn " code here???[/COLOR]
            DataGrid.Columns(i).Width = 50  'num count column
            DataGrid.Columns(i).DefaultCellStyle.Font = New Font(DataGrid.DefaultCellStyle.Font, FontStyle.Italic)
            DataGrid.Columns(i).DefaultCellStyle.Font = New Font("Arial", 8, FontStyle.Regular)

        Next

Thanks in advance!
 
To create a new instance of a class use the New keyword:
VB.NET:
Dim col as New NoEllipsisColumn
then to add it to the columns collection use the Add method or the Insert method, for example:
VB.NET:
grid.Columns.Add(col)
Since you didn't know this, you probably didn't create any columns yourself in the first place, so you probably are binding to a datasource and having columns auto-generated. You can still add the column in code first, just remember to set DataPropertyName to the column it is supposed to map to. Even better, add the column in designer and set the DataPropertyName there. Do as much as you can in designer, Visual Basic is first and foremost Visual.
 
JohnH... good news bad news. Your suggestions made me realize that I could indeed create the columns in design mode after all, and then just reformat them in run time. And, ta da, the dots are gone since I created the cols in design mode using your code.

Now, however, just one more problem. Say I have cells that have persons names in them, 'Jim Smith' and 'Jim Jones' for example. If the entire name won't fit completely, then it removes the entire last name. It'd be best if it just truncated a letter or two, resulting in 'Jim Smit' and 'Jim Jone', but instead it does 'Jim ' and 'Jim '.

I tried some different settings, but could not make it behave differently. I could just make the columns wider, but the whole key here is to get as many columns on the screen as possible to minimize or eliminate user scrolling. So as long as we can tell that it's Jim Smith instead of Jim Jones, we don't need the entire name displayed. Any suggestions?

Thanks millions for your time & effort. Getting good help like this means the difference in writing a successful app or not.
 
I explained this in code:
format.Trimming = StringTrimming.Word 'or Character
 
Back
Top