468,168 Members | 1,626 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,168 developers. It's quick & easy.

Datagrid -True or False column

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
Nov 21 '05 #1
2 1373
ben
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
Nov 21 '05 #2
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

--------------------------
"Agnes" <ag***@dynamictech.com.hk> wrote in message
news:eU**************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by GregM | last post: by
7 posts views Thread by Scott Schluer | last post: by
reply views Thread by BK Kim | last post: by
7 posts views Thread by Dave | last post: by
1 post views Thread by arupasna | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.