I have two Grid views that display data from a table of bills in a SQL Server Database... One from pay period 1 and one from pay period 2
What I am trying to accomplish is to get the two tables to have the same amount of rows so that the look the same side by side for display purposes.
The aspx page...
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="Budget.WebForm1" %>
Here is the aspx.vb code behind page
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
con = GetConnect()
con.Open()
Dim gvd1 As New DataDisplay
gvd1.Display = "p_DisplayBills"
gvd1.DisplayD = "p_DisplayBills 1, 1"
GridView1.DataSource = gvd1.DataDisplay()
GridView1.DataBind()
Dim gvd2 As New DataDisplay
gvd2.Display = "p_DisplayBills"
gvd2.DisplayD = "p_DisplayBills 1, 2"
GridView2.DataSource = gvd2.DataDisplay()
GridView2.DataBind()
Dim rows1 As Integer = GridView1.Rows.Count()
Dim rows2 As Integer = GridView2.Rows.Count()
If rows1 < rows2 Then
'Some Thing that will add extra empty rows to gridview 1 for display purposes....
ElseIf rows2 < rows1 Then
'Some Thing that will add extra empty rows to grid view 2 for display purposes....
End If
con.Close()
GridView1.Attributes.Add("class", "table table-striped")
GridView2.Attributes.Add("class", "table table-striped")
End Sub
End Class
the class to call the data from the SQL Server
Public Class DataDisplay
Public Property DisplayD As String
Public Property Display As String
Public Function DataDisplay()
Dim str As String = DisplayD
Dim cmd As New SqlCommand(str, con)
Dim Ds As New DataSet()
Dim da As New SqlDataAdapter(cmd)
da.Fill(Ds, Display)
Return Ds
End Function
End Class
Dim gvd1 As New DataDisplay
gvd1.Display = "p_DisplayBills"
gvd1.DisplayD = "p_DisplayBills 1, 1"
GridView1.DataSource = gvd1.DataDisplay()
GridView1.DataBind()
Dim gvd2 As New DataDisplay
gvd2.Display = "p_DisplayBills"
gvd2.DisplayD = "p_DisplayBills 1, 2"
GridView2.DataSource = gvd2.DataDisplay()
GridView2.DataBind()
Dim rows1 As Integer = GridView1.Rows.Count()
Dim rows2 As Integer = GridView2.Rows.Count()
If rows1 < rows2 Then
Dim ds as DataSet = gvd1.DataDisplay()
Dim dr as DataRow = ds.Table[0].NewRow()
dr["ColumnName"] = "";
dr["ColumnName"] = "";
dr["ColumnName"] = "";
' how much column exists just add there
ds.Table[0].Rows.Add(dr)
GridView1.DataSource = ds.Table[0]
GridView1.DataBind()
' same this logic apply for gv2
ElseIf rows2 < rows1 Then
'Some Thing that will add extra empty rows to grid view 2 for display purposes....
End If
I plugged that in and I get the following errors... Could it be becuase I am using a stored procedure that does joins on multiple tables?
Error 6 Identifier expected.
Dim dr as DataRow = ds.Table[0].NewRow() it shows a blue swiggly line under the zero
Error 7 'dr' is not declared. It may be inaccessible due to its protection level.
Error 8 Identifier expected.
dr["ColumnName"] = ""; the blue swiggly line is under the " in Column Name
Thanks for your help!
Error 9 'dr' is not declared. It may be inaccessible due to its protection level.
Try replacing the [ ] with ( )
c# uses [] and VB uses ()
This example uses DataTables instead of the DataSet. It uses the Employees table from the NorthWind database, but you could change the SQL to anything.
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class gvDataDisplay
Inherits System.Web.UI.Page
Dim dt1, dt2 As New DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dt1 = DataDisplay("SELECT top 1 * FROM [Employees]")
dt2 = DataDisplay("SELECT top 2 * FROM [Employees]")
Dim rows1 As Integer = dt1.Rows.Count
Dim rows2 As Integer = dt2.Rows.Count
If rows1 < rows2 Then
' add rows to dt1
addRecs(dt1, rows2 - rows1)
ElseIf rows2 < rows1 Then
' add rows to dt2
addRecs(dt2, rows1 - rows2)
End If
GridView1.DataSource = dt1
GridView1.DataBind()
GridView2.DataSource = dt2
GridView2.DataBind()
End Sub
Protected Sub addRecs(tblIn As DataTable, recCount As Int16)
Dim tblDR As DataRow = tblIn.Rows(0)
Dim rowType As System.Type
For I As Int16 = 1 To recCount
Dim dr As DataRow = tblIn.NewRow
For J As Int16 = 0 To tblIn.Columns.Count - 1
rowType = tblDR.Item(J).GetType
Select Case rowType.ToString
Case "System.String"
dr(J) = String.Empty
Case "System.Integer", "System.Int16", "System.Int32"
dr(J) = 0
'Case "System.Date"
' dr(J) = ""
End Select
Next J
tblIn.Rows.Add(dr)
Next I
End Sub
Public Function DataDisplay(sqlIn As String) As DataTable
Dim dt As New DataTable
Dim connstr As String = "Data Source=myServer;Initial Catalog=Northwind;Integrated Security=True"
Using SQLConn As New System.Data.SqlClient.SqlConnection(connstr)
Using cmd As New SqlCommand(sqlIn, SQLConn)
SQLConn.Open()
Using Reader As SqlDataReader = cmd.ExecuteReader()
If Reader.HasRows Then
dt.Load(Reader)
End If 'reader.HasRows
End Using
End Using
End Using
Return dt
End Function
End Class
Thank you everyone for you input and help...
Here is what I ultimally ended up doing...
Thanks
ElseIf rows2 < rows1 Then
Dim ds As DataSet = gvd2.DataDisplay()
Dim x As Integer = rows1 - rows2
For i As Integer = 1 To x
Dim dr = ds.Tables(0).NewRow()
ds.Tables(0).Rows.Add(dr)
GridView2.DataSource = ds.Tables(0)
GridView2.DataBind()
Next
沒有留言:
張貼留言