DatagridView HowToDo?

I have a column in the grid which has coded Data, 0 or 1 which shall be displayed as "Deactive" for a 0 and "Active" for a 1. Is this possible to do?

The application is VB2005 Express and Windows Forms

Thanks

Manfred

[269 byte] By [ManfredRoesch] at [2007-12-25]
# 1

Use the datagridview's cellformatting event for that.



Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable("Names")
dt.Columns.Add("Name")
dt.Columns.Add("State")
dt.Columns.Add("Active")
dt.LoadDataRow(New Object() {"Ken Tucker", "Florida", "0"}, True)
dt.LoadDataRow(New Object() {"Cor Ligthert", "Netherlands", "1"}, True)
dt.LoadDataRow(New Object() {"Terry Burns", "United Kingdom", "1"}, True)
dt.LoadDataRow(New Object() {"Armin Zignler", "Germany", "1"}, True)
dt.LoadDataRow(New Object() {"Herfried K. Wagner", "Austria", "1"}, True)
dt.LoadDataRow(New Object() {"Jay B Harlow", "New York", "0"}, True)
DataGridView1.DataSource = dt
DataGridView1.AllowUserToAddRows = False
End Sub

Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If e.ColumnIndex = 2 Then
Dim s As String
s = IIf(e.Value = "0", "Deactive", "Active").ToString
e.Value = s
End If
End Sub
End Class


KenTucker at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

Whow, this is easy! This is exactly wath I was looking for.

Thank for this great tip!

Manfred

ManfredRoesch at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...