Click here to Skip to main content
16,020,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to view the table using data grid view without adapter using..... pls tell me
Posted
Comments
Tejas Vaishnav 4-Nov-11 7:05am    
Please be specify what you want...
provide some more detail of your question....
mkcm2011 4-Nov-11 8:12am    
i have two tables. Now what i have to do is , i want both tables data in one data grid view (c sharp code)...i have used join query already but there is some problem with me that i do not want dataadapter in my code i just want it with dataset only.

thanks in advance

1 solution

Hi,

a dataset is actually a class that you can manipulate on your own through code. It uses other classes like datatables, datarows, ... If you take a look at the MSDN library on the DataGrid.DataSource property, you'll see an example on how the datasource get's filled with a dataview that's entirely made up with code. Also be sure to check out the DataSet class and the DataTable class.

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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900