asp.net GridView not showing data from Stored Procedure
I have two GridViews, the first 开发者_JAVA百科works fine as I defined the datasource when I dropped it onto the asp page via visual studio and I specified the table to use.
The other GridView (dgvParams below) has just been dropped onto the asp page and I want to use it at run time:
Imports System.Data
Imports System.Data.SqlClient
Imports myDataBase
Partial Class TestWeb
Inherits System.Web.UI.Page
Dim data As New cDatabase
Public Sub New()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dgvParams = New GridView
LoadParamsGrid()
End Sub
Public Sub LoadParamsGrid()
' Create the dataset
Dim strCon As String = data.DBConn.ConnectionString
Dim strSQL As String = "dbo.GetParameters"
Dim dataAdapter As New SqlDataAdapter(strSQL, strCon)
Dim table As New DataTable
Try
dataAdapter.Fill(table)
Catch ex As Exception
' try again
dataAdapter.Fill(table)
End Try
dgvParams.DataSource = table
dgvParams.DataBind()
End Sub
End Class
When I step through the code I can see row and column counts are as expected and call "dgvParams.DataSource = table".
I think I am missing another step somewhere so that I can physically see the data on the GridView? Any suggestions?
Thank you
Is the AutoGenerateColumns property on dgvParams set to True or are the columns already explicitly defined?
I changed fro using a table to a dataset and it just "worked"... although not right away! Not sure really what made this work but this is what I am using now:
Dim ds As New DataSet
Try
dataAdapter.Fill(ds)
Catch ex As Exception
' try again
dataAdapter.Fill(ds)
End Try
dgvParams.DataSource = ds.Tables(0)
dgvParams.DataBind()
You have:
dgvParams = new GridView();
But I don't see where you add it to the web page so you can see it. Any dynamically created controls must be added to the control collection of the page or another control like:
panel.Controls.Add(dgvParams);
Where panel would be an that is used to mark where you want to place the gridview in the page. Or, embed the in the page, give it the dgvParams ID, and bind to that instead.
Or, if you have the dgvParams already in the page as an , remove the dgvParams = new GridView() statement.
Also, if you bind to a dataset, you need to set DataMember to the data table name so it knows which table to bind to.
HTH.
精彩评论