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="Tru e" HeaderStyle-BackColor="Nav y" HeaderStyle-ForeColor="Whi te" AlternatingItemStyle-BackC olor="#ddd ddd" AllowPaging="True" Allowsorting="True" PageSize="3" OnPageIndexChanged="dgPost sPageChang ed" OnSortCommand="dgPostsSort ">
<AlternatingItemStyle BackColor="#DDDDDD"></Alte rnatingIte mStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="Navy"></HeaderS tyle>
<PagerStyle NextPageText="Forward" PrevPageText="Back" Position="Bottom" PageButtonCount="5" Visible="False"></PagerSty le>
<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("Sele ct * from s1", SqlConnection1)
SqlDataAdapter1.SelectComm and = 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
<asp:datagrid id="MyDataGrid" AutoGenerateColumns="False
<AlternatingItemStyle BackColor="#DDDDDD"></Alte
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="Navy"></HeaderS
<PagerStyle NextPageText="Forward" PrevPageText="Back" Position="Bottom" PageButtonCount="5" Visible="False"></PagerSty
<Columns>
<asp:TemplateColumn SortExpression="p1" HeaderText="Thread">
<ItemTemplate>
<input type="checkbox" id="chk1" runat="server"> <a href="messagetest.aspx">
<%# DataBinder.Eval(Container.
</a>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn SortExpression="c11" HeaderText="Date Posted">
<ItemTemplate>
<%# DataBinder.Eval(Container.
</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("Sele
SqlDataAdapter1.SelectComm
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.
Response.write "Below 10"
Else if DataBinder.Eval(Container.
Response.write "Below 100"
Else
Response.write "100 or more"
End IF
%>
</ItemTemplate>
</asp:TemplateColumn>
How can I do this. Please help
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the responses.
I will assign the points after trying out the suggestions.
I will assign the points after trying out the suggestions.
ASKER
Hi
I implemented my code as follows.
<ItemTemplate>
<input type="checkbox" id="chk1" runat="server"> <a href="messagetest.aspx">
<%# getShowString(DataBinder.E val(Contai ner.DataIt em, "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.
I implemented my code as follows.
<ItemTemplate>
<input type="checkbox" id="chk1" runat="server"> <a href="messagetest.aspx">
<%# getShowString(DataBinder.E
</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.
Private Sub MyDataGrid_ItemDataBound(B
If ((e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingIt
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.Ite
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