Datagrid -True or False column

  • Thread starter Thread starter Agnes
  • Start date Start date
A

Agnes

I got a true or false column in my Datagrid, The datafield is "bit" in the
sql tables,
However, I want to display "A" if the field is true, "B" is false .
Is this possible to do ?? or I should add one more field in my tables and
put "A"or "B" in it ? thanks
 
I use the following code to alter my datagrid:

Sub Datagrid4_ItemDataBound(ByVal sender As System.Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)
'Get the column number based on the header text
Dim Column As Integer = IndexByName(Datagrid1, "Name")

If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType =
ListItemType.Item Then 'If this is not a header or footer row
'then change the contents of your datagrid cell based on an if statement
if e.Item.Cells(Column).Text = "1" then
e.Item.Cells(Column).Text= "A"
End If
End Sub

..
Shared Function IndexByName(ByVal grid As
System.Web.UI.WebControls.DataGrid, ByVal name As String) As Integer
Dim col As Integer
For col = 0 To grid.Columns.Count - 1 Step col + 1
If grid.Columns(col).HeaderText = name Then
Return col
End If
Next
Return -1
End Function

I havent tested this code. I changed mine to do what you are trying to
do...

HTH
 
Hi,

Create a class that inherits from datagridtextboxcolumn to do that.
Add the class to the grids tablestyle. Here is an example that displays yes
or no


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 got a true or false column in my Datagrid, The datafield is "bit" in the
sql tables,
However, I want to display "A" if the field is true, "B" is false .
Is this possible to do ?? or I should add one more field in my tables and
put "A"or "B" in it ? thanks
 

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

Back
Top