PDA

View Full Version : Need help with DataGrid


steurm
12-26-2004, 01:26 AM
Here you can use the ItemDatabound event:
(suppose the boolean-column is the first one of the datagrid--> index=0)




if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)


{


if (e.Item.Cells[0].Text=='1')


e.Item.Cells[0].Text='Done';


else


e.Item.Cells[0].Text='Pending';


}

--
Steurm
www.steurm.net/steurm (http://www.steurm.net/steurm)
http://www.steurm.net/steurm/imgs/steurm_small.gif

brightmosquito
12-26-2004, 02:35 AM
I have boolen values in database, how can I display themin otherstrings in DataGrid?

for example, valuein databse is 1, I want to display it as "Done", value in database is 0, I want to diaplay it as "Pending"

brightmosquito
12-26-2004, 08:36 AM
I don't really get it. Here is my code. In database, "Done" columncontains the boolean values.
Would you pleaseshow me how to do it?

<script language="VB"runat="server">
Sub Page_Load()
Dim strSQL As String = "SELECT Done, CustomerName FROM Tasks"
Dim cmdData As SqlCommand = New SqlCommand(strSQL, conDB)
Dim drData As SqlDataReader = cmdData.ExecuteReader()

dgTasks.DataSource = drData
dgTasks.DataBind()
End Sub
</script>

<asp:DataGrid ID="dgTasks" AutoGenerateColumns="false" runat="server">
<columns>
<asp:BoundColumn DataField="Done"HeaderText="Status"/>
<asp:BoundColumn DataField="CustomerName"HeaderText="Customer Name"/>
</columns>
</asp:DataGrid>

Swood
12-27-2004, 07:18 AM
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound


If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then


Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)


Dim a As String = drv.Row("Done").ToString()


If a = "1" Then


e.Item.Cells(0).Text = "Done"


End If


End If


End Sub

Michael
01-06-2005, 03:08 AM
The code there posted should work fine, however you could also avoid it and get the database to do it for you.


Do the comparison on the SQL command using a case statement or some other similar technique, less for you to do programming wise :)