Link to home
Start Free TrialLog in
Avatar of SVir
SVir

asked on

ugrent: ASP.net Datagrid question. Please help

I am building an asp.net web page that has a datagrid control as follows:

<asp:datagrid id="MyDataGrid" AutoGenerateColumns="False" runat="server" align="center" width="760" Font-Name="Arial" Font-Size="x-small" HeaderStyle-Font-Bold="True" HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White" AlternatingItemStyle-BackColor="#dddddd" AllowPaging="True" Allowsorting="True" PageSize="3" OnPageIndexChanged="dgPostsPageChanged" OnSortCommand="dgPostsSort">
                        <AlternatingItemStyle BackColor="#DDDDDD"></AlternatingItemStyle>
                        <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="Navy"></HeaderStyle>
                        <PagerStyle NextPageText="Forward" PrevPageText="Back" Position="Bottom" PageButtonCount="5" Visible="False"></PagerStyle>
                        <Columns>
                              <asp:TemplateColumn SortExpression="p1" HeaderText="Thread">
                                    <ItemTemplate>
                                          <input type="checkbox" id="chk1" runat="server"> <a href="messagetest.aspx">
                                                <%# DataBinder.Eval(Container.DataItem, "p1") %>
                                          </a>
                                    </ItemTemplate>
                              </asp:TemplateColumn>
                              <asp:TemplateColumn SortExpression="c11" HeaderText="Date Posted">
                                    <ItemTemplate>
                                          <%# DataBinder.Eval(Container.DataItem, "c11") %>
                                    </ItemTemplate>
                              </asp:TemplateColumn>
                        </Columns>
                  </asp:datagrid>

The relevent Code in the CodeBehind file is:
Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        bindData()
End Sub

Private Sub bindData()
        SqlConnection1.Open()
       
        Dim Ds As DataSet = New DataSet("a")
        Dim mycommand As SqlClient.SqlCommand = New SqlClient.SqlCommand("Select * from s1", SqlConnection1)
        SqlDataAdapter1.SelectCommand = mycommand
        SqlDataAdapter1.Fill(Ds, "a")
        Dim dv As New DataView(Ds.Tables("a"))
        'lblMsg.Text = dv.Sort()
        dv.Sort = SortExpression
        MyDataGrid.DataSource = dv
        MyDataGrid.DataBind()
       
        SqlConnection1.Close()
    End Sub

---------------------------------------------------------------------------------------------------------------------------------------
I am trying to put some condition code in place. For example I want to do something like this:
<asp:TemplateColumn SortExpression="p1" HeaderText="Thread">
                                    <ItemTemplate>
                                    <%If  DataBinder.Eval(Container.DataItem, "p1") <10 then
                                                                                                Response.write "Below 10"
                                                                                                Else if DataBinder.Eval(Container.DataItem, "p1") <100 then
                                                                                                Response.write "Below 100"
                                                                                                Else
                                                                                                Response.write "100 or more"
                                                                                                End IF
                                                                                                %>
                                    </ItemTemplate>
                              </asp:TemplateColumn>



How can I do this. Please help
SOLUTION
Avatar of naveenkohli
naveenkohli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kylgarmor
kylgarmor

oops.  Forgot to change the column index from my test app.  Should be this instead ;)

    Private Sub MyDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles MyDataGrid.ItemDataBound
        If ((e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem)) Then

            Dim rv As DataRowView = CType(e.Item.DataItem, DataRowView)
            ' Get value of "p1" column at index 0.
            Dim p1val As Int32 = Convert.ToInt32(rv.Row.ItemArray(0))
            If (p1val < 10) Then
                e.Item.Cells(0).Text = "Below 10"
            ElseIf (p1val < 100) Then
                e.Item.Cells(0).Text = "Less than 100"
            Else
                e.Item.Cells(0).Text = "100 or more"
            End If

        End If
    End Sub
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SVir

ASKER

Thanks for the responses.

I will assign the points after trying out the suggestions.
Avatar of SVir

ASKER

Hi

I implemented my code as follows.

 <ItemTemplate>
                                  <input type="checkbox" id="chk1" runat="server"> <a href="messagetest.aspx">
                                       <%# getShowString(DataBinder.Eval(Container.DataItem, "p1").ToString()) %>
                                  </a>
                             </ItemTemplate>


Public Function getShowString(Byval strValue as string) as string
If Clng(strValue) <10 then
      getShowString =  "Below 10"
Else if  Clng(strValue) <100 then
      getShowString =  "Below 100"
Else
      getShowString =  "100 or  more "                                         
End IF

End Function


Who do you guys think is closest to this approach. I will assign points as you guys tell me.