Welcome to WebmasterWorld Guest from 52.91.39.106

Forum Moderators: ocean10000

Message Too Old, No Replies

stuck on a datagrid

could use a little guidance

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

Preferred Member

10+ Year Member

joined:Nov 24, 2005
posts: 429
votes: 0


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....
2:59 pm on Oct 8, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month

joined:Jan 14, 2004
posts:864
votes: 3


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>
3:05 pm on Oct 8, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 24, 2005
posts: 429
votes: 0


Thank you. I will look tonight when I am home. I appreciate it
4:13 pm on Oct 8, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 24, 2005
posts: 429
votes: 0


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
4:45 pm on Oct 8, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month

joined:Jan 14, 2004
posts:864
votes: 3


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]
11:27 pm on Oct 8, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 24, 2005
posts: 429
votes: 0


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?
12:38 am on Oct 9, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month

joined:Jan 14, 2004
posts:864
votes: 3


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]
12:51 am on Oct 9, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 24, 2005
posts: 429
votes: 0


OK. I will try and report back.
1:05 am on Oct 9, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 24, 2005
posts: 429
votes: 0


Do you have the VB version?
4:39 pm on Oct 10, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month

joined:Jan 14, 2004
posts:864
votes: 3


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
6:42 pm on Oct 10, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 24, 2005
posts: 429
votes: 0


Thanks I'll look.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members