Forum Moderators: open

Message Too Old, No Replies

stuck on a datagrid

could use a little guidance

         

hal12b

3:09 am on Oct 8, 2011 (gmt 0)

10+ Year Member



Question - I am using a datagrid and it is displaying info from a table called let's say tblInfo, such as

First Name
Last Name
City
State
Zip
Height
Weight
Hair Color

Everything works great


But I want to store the Height, Weight, and Hair Color now in a another table called tblStats, so the original table will now just be

First Name
Last Name
City
State
Zip
Stats


How do I adjust the query and datagrid so it works. Let's say that the new table called tblStats looks like this

1 5'10 170 Brown
2 5'8 144 Red
3 5'10 170 Black
4 etc....

Ocean10000

2:59 pm on Oct 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would imagine it would look like this. I generated this via a few built in wizards in Visual Studio 2010

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Height" HeaderText="Height"
SortExpression="Height" />
<asp:BoundField DataField="Weight" HeaderText="Weight"
SortExpression="Weight" />
<asp:BoundField DataField="Hair_Color" HeaderText="Hair_Color"
SortExpression="Hair_Color" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:db1ConnectionString %>"
ProviderName="<%$ ConnectionStrings:db1ConnectionString.ProviderName %>"
SelectCommand="SELECT [ID], [Height], [Weight], [Hair Color] AS Hair_Color FROM [tblInfo]">
</asp:SqlDataSource>

hal12b

3:05 pm on Oct 8, 2011 (gmt 0)

10+ Year Member



Thank you. I will look tonight when I am home. I appreciate it

hal12b

4:13 pm on Oct 8, 2011 (gmt 0)

10+ Year Member



Let me ask you another question, if am displaying info on a page like this



<%# Utils.CheckForNull_Text2(Container.DataItem("CITY"))%>


How could put an if else statement here? I am not sure.. getting errors

Ocean10000

4:45 pm on Oct 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For a Gridview the BoundField has a property NullDisplayText which is blank by default. So if the field is null, it will be displayed as a blank or what ever value you put in the NullDisplayText field. So you would not even need to use that additional function command since it is built into the BoundField control.

Reference
BoundField.NullDisplayText Property [msdn.microsoft.com]
BoundField.ConvertEmptyStringToNull Property [msdn.microsoft.com]

hal12b

11:27 pm on Oct 8, 2011 (gmt 0)

10+ Year Member



I mean this is in the code

<%# Utils.CheckForNull_Text2(Container.DataItem("TREAD_LOCATION"))%>

and it displays a list of numbers such as 1,2,3. Knowing this, I'd like to display a message on the page instead of a number displaying. Like this

<%If # Utils.CheckForNull_Text2(Container.DataItem("TREAD_LOCATION")) = "2" then
"hello everybody"

elseif Utils.CheckForNull_Text2(Container.DataItem("TREAD_LOCATION")) = "3" then

"good night"

else

end if

%>


Any ideas?

Ocean10000

12:38 am on Oct 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



See below for an example


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>

<asp:TemplateField>
<ItemTemplate>
<%# Tread_Location_Lookup(Eval("ID").ToString())%>
</ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>

<script runat="server" language="C#">
string Tread_Location_Lookup(string Tread_Location)
{
if (Tread_Location =="2")
{
return "hello everybody";
}
else if (Tread_Location =="3")
{
return "good night";
}
return string.Empty;
}
</script>


Reference
GridView Examples for ASP.NET 2.0: Working with TemplateFields [msdn.microsoft.com]

hal12b

12:51 am on Oct 9, 2011 (gmt 0)

10+ Year Member



OK. I will try and report back.

hal12b

1:05 am on Oct 9, 2011 (gmt 0)

10+ Year Member



Do you have the VB version?

Ocean10000

4:39 pm on Oct 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I actually don't have VB.net stuff installed on this machine, so I can't test the following code.

Function Tread_Location_Lookup(Tread_Location as string) as string
If (Tread_Location =="2") then
return "hello everybody"
Else If (Tread_Location =="3") then
return "good night"
END IF
return string.Empty
End Function

hal12b

6:42 pm on Oct 10, 2011 (gmt 0)

10+ Year Member



Thanks I'll look.