Format Strings in DataGrid Column Style

  • Thread starter Thread starter Bernie Hunt
  • Start date Start date
B

Bernie Hunt

I'm looking for information on how to format a strings in a Datagrid. The
application has telephone numbers coming from a database and they are a
string of 10 digits. I would like to add in the "-" dash dividers between
the area and office codes and the line number.

Can I do this with .format?

I have to format them on display only I cannot change the underlying
database.

Any suggestions?

Thanks,
Bernie
 
Bernie said:
I'm looking for information on how to format a strings in a Datagrid. The
application has telephone numbers coming from a database and they are a
string of 10 digits. I would like to add in the "-" dash dividers between
the area and office codes and the line number.

Can I do this with .format?

I have to format them on display only I cannot change the underlying
database.

Any suggestions?

Thanks,
Bernie
I'm using V 2003, and it would be great if that feature worked! Would
save me a lot of special coding. I have to format values as they are
retrieved. Depending on the database you are using, you should be able
to format the selected columns when you query them.

T
 
Bernie,

If the underlying data source is a DataTable, you could add another
Column and set its expression value to a Substringed format of the
original telephone number string. You could then use a DataGrid table
style to hide the unformatted telephone number column.

Public Class Form1
Inherits System.Windows.Forms.Form

' form generated code

Dim peopleTable As New DataTable

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

peopleTable.Columns.Add("name", GetType(System.String))
peopleTable.Columns.Add("telephone", GetType(System.String))
peopleTable.Columns.Add("telephoneFormat",
GetType(System.String))
peopleTable.Columns("telephoneFormat").Expression = "'(' +
substring(telephone,1,3) + ') ' + substring(telephone,4,3) + '-' +
substring(telephone,7,4)"

peopleTable.Rows.Add(New Object() {"bob", "3214567890"})
peopleTable.Rows.Add(New Object() {"joe", "1236547890"})

peopleTable.TableName = "people"

Me.DataGrid1.DataSource = peopleTable

End Sub

End Class


john
 
I've never tried this, but the Binding class has a Format and Parse event
that you should be able to use
You can get the collection of Bindings by getting the BindingManagerBase (in
reality it's a CurrencyManager) from the BindingContext property of the
DataGrid

/claes
 
Back
Top