Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot see Datagrid

Status
Not open for further replies.

Chopstik

Technical User
Oct 24, 2001
2,180
US
I'm teaching myself the basics of .NET (VB.NET) at the moment and have encountered something that doesn't make sense and am hoping someone can point out what I'm clearly not seeing. I've created a datagrid and bound some data to it, but I cannot see the datagrid when I attempt to open the page. I've attached my code below. Can someone point out what I may be missing? Thanks.
Code:
Public Class TestConnection
    Inherits System.Web.UI.Page
Protected WithEvents lblTest As System.Web.UI.WebControls.Label
Protected WithEvents lblMe As System.Web.UI.WebControls.Label
Protected WithEvents myDA As System.Data.SqlClient.SqlDataAdapter
Protected WithEvents myDS As System.Data.DataSet
Protected WithEvents myDT As System.Data.DataTable
Protected WithEvents myDG As System.Web.UI.WebControls.DataGrid

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
'    Dim strConn As String
    Dim myCN As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
    myCN.Open()
    Dim myDA As New SqlDataAdapter("SELECT sugstn_id, sugstn_txt, lst_mod_id, lst_mod_dt, apprv_flg FROM T_SUGSTN", myCN)

    Dim myDS As New DataSet
    myDA.Fill(myDS)

    Dim myDT As DataTable
    myDT = myDS.Tables(0)

    Dim myDG As DataGrid = New DataGrid
    myDG.DataSource = myDT
    myDG.DataBind()
End Sub

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
MOst likey there are no rows being returned from your query. Make sure you are gettng rows returned
 
I did check that and was able to view them when I tried inputting them into one of my label fields. I just couldn't view anything when I attempted to view them in the datagrid.

(But just to be on the safe side, I'll go check again just to be certain.)

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Yep, if I add the following code to the bottom of my earlier code, then it does return the results I expect to see. Not sure why it won't work in the datagrid. Any thoughts are greatly appreciated! Thanks!
Code:
    Dim sb As StringBuilder = New StringBuilder("")

    Dim myRow As DataRow
    For Each myRow In myDT.Rows
        sb.Append(myRow("sugstn_txt") & "<br/>")
        'lblTest.Text = lblTest.Text & myRow("sugstn_txt") & "<br />"
    Next
    lblTest.Text = sb.ToString

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
can you show the aspx page where you have code for datagrid...make sure you have AutoGenerateColumns set to true.

-DNG
 
Try this
replace this code:
Code:
    Dim myDT As DataTable
    myDT = myDS.Tables(0)

    Dim myDG As DataGrid = New DataGrid
    myDG.DataSource = myDT
with this code
Code:
    Dim myDG As DataGrid = New DataGrid
    myDG.DataSource = [b]myDS[/b]
 
The code on the .aspx page is as follows:
Code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="TestConnection.aspx.vb" Inherits="SugBox_NET.TestConnection"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>TestConnection</title>
		<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
		<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5">[/URL]
	</HEAD>
	<body MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" runat="server">
			<asp:Label ID="lblTest" Runat="server">None</asp:Label>
			<br>
			<asp:Label ID="lblMe" text="What?" Runat="server"></asp:Label>
			<asp:DataGrid id="myDG" style="Z-INDEX: 101; LEFT: 64px; POSITION: absolute; TOP: 104px" runat="server"
				Width="752px" Height="320px" BorderColor="#663399" CellPadding="5" BorderWidth="5px"
				visible="True" HeaderStyle-BackColor="#33ff33" AutoGenerateColumns="True"></asp:DataGrid>
		</form>
	</body>
</HTML>
The autogenerate is set to true as is the visible property.

I also tried the change jebenson suggested an it also does not work. But that begs the question of whether I did it right in the first place. As I said, I'm just learning it (again) so it's just a matter of getting my feet wet at the moment...

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
[lol]
Oddly enough, that was where I was going to obtain my code originally. The only difference being that I was using the dataset to populate my datagrid instead of the .ExecuteReader. But this doesn't really address my problem and I'd be curious to determine why I am having this issue (so I know how to deal with it should I encounter it in the future). Any other thoughts? Thanks!

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Just for the heck of it, I changed it to follow the same methodology listed in 4GuysFromRolla and have the same issue. Not sure what would be causing it but it's really aggravating. Any other thoughts? Thanks!

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Code:
    [b]Dim myDG As DataGrid = New DataGrid[/b]
    myDG.DataSource = myDT
    myDG.DataBind()
The problem is the first line of code above. You've created a completely new DataGrid in the page load event and bound the source to it (rather than binding the source to the grid on the actual page). Remove that line and you should be fine...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanks, ca8msm, that was spot on! I had that in the code originally because I was adding it manually at first, then decided to add the control from the toolbox and tried to change the code accordingly. I stripped that out and now everything works as it should. Thanks again!

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top