Need help with DataGrid

Discussion in 'ASP.NET / ASP.NET Core' started by steurm, Dec 26, 2004.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. 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
    [​IMG]
     
  2. 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"
     
  3. 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>
     
  4. 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
     
  5. 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 :)
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page