|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Format Strings in DataGrid Column StyleI'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 Hunt wrote:
Show quoteHide quote >I'm looking for information on how to format a strings in a Datagrid. The I'm using V 2003, and it would be great if that feature worked! Would >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 > > 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 John,
Good idea, it works great! Thanks! Bernie "jayeldee" <jayel***@gmail.com> wrote in news:1147360118.471951.160720 @i39g2000cwa.googlegroups.com:Show quoteHide quote > 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 Show quoteHide quote "Bernie Hunt" <bh***@optonline.net> wrote in message news:Xns97C0683E61A6bhuntoptonlinenet@207.46.248.16... > 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 |
|||||||||||||||||||||||