I have a set of asp hidden field controls which I wish to set the values according to my data tables column names, the amount of columns returned differ, so I am setting the unused hidden fields to 0 if not used. Below is what I've attempted so far just struggling to set the correct hidden field accordingly.
VB-
Dim dt As DataTable
Dim ds As New DataSet()
ds = Getdata(4)
dt = ds.Tables(0)
Dim ColCnt As String = dt.Columns.Count 'Current ColCnt is 3
For Each column As DataColumn In dt.Columns
Select Case ColCnt
Case 2
hxValue.Value = column.ColumnName 'set to 1st Column Name
hxValue1.Value = 0 'Not used
hyValue.Value = column.ColumnName 'Set To 2nd Column Name
Case 3
hxValue.Value = column.ColumnName 'set to 1st Column Name
hxValue1.Value = column.ColumnName 'set to 2nd Column Name
hyValue.Value = column.ColumnName 'set to 3rd Column Name
End Select
Next
Try this, I don't think you need a For Each loop :
Dim ColCnt As Int = dt.Columns.Count 'Current ColCnt is 3
Select Case ColCnt
Case 2
hxValue.Value = dt.Columns[0].ColumnName 'set to 1st Column Name
hxValue1.Value = 0 'Not used
hyValue.Value = dt.Columns[1].ColumnName 'Set To 2nd Column Name
Case 3
hxValue.Value = dt.Columns[0].ColumnName 'set to 1st Column Name
hxValue1.Value = dt.Columns[1].ColumnName 'set to 2nd Column Name
hyValue.Value = dt.Columns[2].ColumnName 'set to 3rd Column Name
End Select
Related
I am Using Macro for pivot table in Excel...
when i am trying to put pivot table for a data of around 80,000 rows my pivot table is not appearing but if i take only 65000 rows than its working can anyone help me in debugging this error.
These are the VBA Codes that i am using
Option Explicit
Sub Macro1()
'Declare Variables
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
'Delete Preivous Pivot Table Worksheet & Insert a New Blank Worksheet With Same Name
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets("Data")
'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="EmployeePivotTable")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="EmployeePivotTable")
'Insert Row Fields
With ActiveSheet.PivotTables("EmployeePivotTable").PivotFields("FilePeriod")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("EmployeePivotTable").PivotFields("ProjectNumber")
.Orientation = xlRowField
.Position = 2
End With
'Insert Column Fields
With ActiveSheet.PivotTables("EmployeePivotTable").PivotFields("EffectiveFTE")
.Orientation = xlColumnField
.Position = 1
End With
'Format Pivot Table
ActiveSheet.PivotTables("EmployeePivotTable").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("EmployeePivotTable").TableStyle2 = "PivotStyleMedium9"
End Sub
I have Dataview and I want to add only few records from that Dataview to another new Dataview.
Please suggest how can i do that.
Your question is not very clear, but I'll show you some code.
Suppose that you have 2 datagridviews (dtgv and dtgv2) with 2 columns and many rows.
' Create a condition
Dim condition As Boolean = False
' Loop through all rows in the first dtgv
For i As Integer = 0 To dtgv.Rows.Count - 1
' If your condition is true
If condition = True Then
' Create a row with dtgv's values
Dim row As Object() = New Object() {dtgv.Rows(i).Cells(0).Value, _
dtgv.Rows(i).Cells(1).Value}
' Add this row to the second dtgv (dtgv2)
dtgv2.Rows.Add(row)
End If
Next
I have list of all days in a month (as GridView header) refer to which month is selected by user which then be displayed in GridView.
Display list of Days in a month
Dim dt As New DataTable
Dim col As New DataColumn
dt.Columns.AddRange(New DataColumn(0) {New DataColumn("Date")})
For i As Integer = 1 To gsTotalDD 'gsTotalDD is total of days in a month
dt.Columns.Add(i)
Next
dt.Rows.Add("Status")
gvRptStatus.DataSource = dt
gvRptStatus.DataBind()
I had a table named tblUpload inside the database which storing data that been uploaded together with date of data uploaded.
What I am trying to do is looping through all the header value (list of days) and check either there is day matching with the date of data uploaded. When there is matching value, the status row on the code dt.Rows.Add("Status") will show status done and leave cell empty if no matching days found.
Example : GridView date (fist row), second header column is 1 and check with database has matching value or not. If yes then on (second row) status, at second column will display done.
EDIT
So I came up with checking the matching value like this :
Dim lCnn As New SqlConnection(gsConnString)
Dim lCmd As New SqlCommand
Dim lsCmd As String
Dim dt As New DataTable
Dim newDt As New DataTable
Dim sda As New SqlDataAdapter
Dim dtrow As DataRow = newDt.NewRow()
lCnn.Open()
lCmd.Connection = lCnn
lsCmd = "SELECT DISTINCT DATEPART (d, [date])"
lsCmd &= " FROM dbUpload..tblUpload"
lCmd.CommandText = lsCmd
sda.SelectCommand = lCmd
sda.Fill(newDt)
dt.Columns.AddRange(New DataColumn(0) {New DataColumn("Date")})
For i As Integer = 1 To gsTotalDD 'gsTotalDD is total of days in a month
dt.Columns.Add(i)
value2 = i
For Each col As DataColumn In newDt.Columns
value = newDt.Rows(i - 1).Item(col)
If value = value2 Then
MsgBox("Same value")
End If
Next
Next
By doing this, I manage to check the value but it's not the right way to do it since I wanted to set the status as done if got matching value or leave status empty if vice versa.
Thank you.
I have Multiple Rows of same product with different Qty.
I want single row of each product by doing some of Qty in datatable in vb.net
So you want to group by the product and sum the quantity per group?
You could use LINQ's GroupBy + Sum and fill a second table with the same schema:
Dim productGroups = dt.AsEnumerable().GroupBy(Function(row) row.Field(Of String)("Product"))
Dim aggregatedTable As DataTable = dt.Clone() ' empty table, same columns
For Each grp In productGroups
Dim newRow = aggregatedTable.Rows.Add()
Dim sumOfQuantity As Int32 = grp.Sum(Function(row) row.Field(Of Int32)("Qty"))
newRow.SetField("Product", grp.Key)
newRow.SetField("Qty", sumOfQuantity)
Next
The same with query syntax if you prefer it:
Dim productGroups = From row In dt.AsEnumerable()
Group row By Product = row.Field(Of String)("Product") Into Group
I'm wanting to get the values from specific columns in a data row by index, I'm having a problem accessing the value from the second row that is returned, below is what I'm trying to achieve
Dim daSeries As New dsSVTableAdapters.clsCH
Dim dtSeries As New dsSV.SeriesDataTable
Dim drSeries As dsSV.SeriesRow
dtSeries = daSeries.CSeries(1)
drSeries = dtSeries.Rows(0)
Dim RowCnt As Integer = dtSeries.Rows.Count 'Current RowCnt is 2
Select Case RowCnt
Case 1 'Only One row exists
hxValue1.Value = drSeries.YFieldName 'access 1st row YFieldName
hyValue.Value = drSeries.XFieldName 'access 1st row XFieldName
Case 2 'Two rows exists
For i As Integer = 0 To dtSeries.Rows.Count - 1
If i = 0 Then 'First Row index
hxValue1.Value = drSeries.YFieldName 'access 1st row YFieldName
hyValue.Value = drSeries.XFieldName 'access 1st row XFieldName
ElseIf i = 1 Then '2nd Row index
hxValue2.Value = drSeries.YFieldName 'access 2nd row YFieldName
End If
Next
End Select
Your drSeries variable points always at the first row. You should change to the second row inside the loop
Case 2 'Two rows exists
For i As Integer = 0 To dtSeries.Rows.Count - 1
' Simply add this to your loop
drSeries = dtSeries.Rows(i)
If i = 0 Then 'First Row index
hxValue1.Value = drSeries.YFieldName 'access 1st row YFieldName
hyValue.Value = drSeries.XFieldName 'access 1st row XFieldName
ElseIf i = 1 Then '2nd Row index
hxValue2.Value = drSeries.YFieldName 'access 2nd row YFieldName
End If
Next