本文介绍了如何在数据网格视图中查看表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在不使用适配器的情况下使用数据网格视图查看表....请告诉我
how to view the table using data grid view without adapter using..... pls tell me
推荐答案
Imports System.Data.SqlClient
Imports System.Configuration.ConfigurationSettings
Public Class test1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
''This call is required by the Web Form Designer.
<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents dgProduct As System.Web.UI.WebControls.DataGrid
''NOTE: The following placeholder declaration is required by the Web Form Designer.
''Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
''CODEGEN: This method call is required by the Web Form Designer
''Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Dim con As String = AppSettings("ConnectionString")
Dim objSQLCon As New SqlConnection(con)
Dim cmd As SqlCommand
Dim red As SqlDataReader
Function CreateDataSource() As ICollection
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn("Product Code", GetType(String)))
dt.Columns.Add(New DataColumn("Product Description", GetType(String)))
dt.Columns.Add(New DataColumn("Product Availability", GetType(String)))
Dim queryCode As String = "Select Prod_Code,Prod_Description,Prod_Availability from Product"
Dim objCmdCode As New SqlCommand(queryCode, objSQLCon)
Dim rdrCode As SqlDataReader
Dim row As DataRow
objSQLCon.Open()
rdrCode = objCmdCode.ExecuteReader
Do While rdrCode.Read
row = dt.NewRow()
row(0) = rdrCode.GetValue(0)
row(1) = rdrCode.GetValue(1)
row(2) = rdrCode.GetValue(2)
dt.Rows.Add(row)
Loop
objSQLCon.Close()
Dim dv As New DataView(dt)
Return dv
End Function ''CreateDataSource
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
If Not IsPostBack Then
'' Load this data only once.
dgProduct.DataSource = CreateDataSource()
dgProduct.DataBind()
End If
End Sub
End Class
<form id="frmTest" method="post" runat="server">
<asp:datagrid id="dgProduct" runat="server" width="400px" height="128px" autogeneratecolumns="true" xmlns:asp="#unknown">
</asp:datagrid>
</form>
这篇关于如何在数据网格视图中查看表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!