Yes/No format string in datagrid?

J

Jeremy

I am displaying a boolean field in a grid & want to format it Yes/No. Can
someone tell me what the formatstring for this is? That is,

DataGridTextBoxColumn.Format = ??

Jeremy
 
K

Ken Tucker [MVP]

Hi,

Create a class that inherits from datagridtextboxcolumn to do that.
Add the class to the grids tablestyle.

Dim ds As New DataSet

Dim phasetable As DataTable

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

Dim conn As SqlConnection

Dim strConn As String

Dim strSQL As String

Dim da As SqlDataAdapter

'strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

'strConn &= "Data Source = Northwind.mdb;"

strConn = "Server = (local);"

strConn &= "Database = NorthWind;"

strConn &= "Integrated Security = SSPI;"



conn = New SqlConnection(strConn)

da = New SqlDataAdapter("Select * From Products", conn)

da.Fill(ds, "Products")






Dim ts As New DataGridTableStyle

ts.MappingName = ds.Tables("Products").TableName

Dim colDiscontinued As New YesNoColumn

With colDiscontinued

..MappingName = "Discontinued"

..HeaderText = "Discontinued"

..Width = 80

End With

Dim colName As New DataGridTextBoxColumn

With colName

..MappingName = "ProductName"

..HeaderText = "Product Name"

..Width = 180

End With



ts.GridColumnStyles.Add(colName)

ts.GridColumnStyles.Add(colDiscontinued)

DataGrid1.TableStyles.Add(ts)

phasetable = ds.Tables("Products")

DataGrid1.DataSource = phasetable

ts = Nothing

colDiscontinued = Nothing

colName = Nothing

End Sub



The datagrid column



Public Class YesNoColumn

Inherits DataGridTextBoxColumn







Public Sub New()

Me.ReadOnly = True

End Sub

Protected Overrides Function GetColumnValueAtRow(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Object

Dim b As Boolean = False

Try

b = CType(MyBase.GetColumnValueAtRow(source, rowNum), Boolean)

Catch ex As Exception

End Try

If b = True Then

Return "Yes"

Else

Return "No"

End If

End Function

End Class



Ken

--------------------------

I am displaying a boolean field in a grid & want to format it Yes/No. Can
someone tell me what the formatstring for this is? That is,

DataGridTextBoxColumn.Format = ??

Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top