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
Related
I have the following code:
I am getting the error in counts.Item(value) = 1.
Set xmlNodeList = xmlEncounter.documentElement.selectNodes("//BillingLIne/Receipts")
If xmlNodeList.length > 1 Then
For i_Loop1 = 1 To xmlNodeList.length
strserviceChecked_GL(i_Loop1) = xmlNodeList.Item(i_Loop1 - 1).Attributes.getNamedItem("ServiceID").nodeValue
Id1tobechecked_GL(i_Loop1) = xmlNodeList.Item(i_Loop1 - 1).Attributes.getNamedItem("Receptid1").nodeValue
Id2tobechecked_GL(i_Loop1) = xmlNodeList.Item(i_Loop1 - 1).Attributes.getNamedItem("Receptid2").nodeValue
Id3tobechecked_GL(i_Loop1) = xmlNodeList.Item(i_Loop1 - 1).Attributes.getNamedItem("Receptid3").nodeValue
Id4tobechecked_GL(i_Loop1) = xmlNodeList.Item(i_Loop1 - 1).Attributes.getNamedItem("Receptid4").nodeValue
Next i_Loop1
End If
Dim value As Variant
Dim counts As Collection
Set counts = New Collection
For Each value In strserviceChecked_GL
If Not Exists(counts,value) Then
counts.Item(value) = 1
Else
counts.Item(value) = counts.Item(value) + 1
End If
Next
Public Function Exists(ByVal oCol As Collection, ByVal vKey As Variant) As Boolean
On Error Resume Next
oCol.Item vKey
Exists = (Err.Number = 0)
Err.Clear
End Function
When a collection has no item for a key, you need to add one, and give it a value as follows.
For Each value In strserviceChecked_GL
If Not Exists(counts,value) Then
' Key 'value' is not in 'counts' collection.
' Add key 'value', and give it the value 1.
counts.Add 1, CStr(value)
'counts.Item(value) = 1
Else
counts.Item(value) = counts.Item(value) + 1
End If
Next
I'm trying to combine the row cells when the campaign code and vehicle no are repeated as shown in below image. The result listed below is with gridview 20 page size
Problem
When the grid view page size is set with 2 for example, the row cell no longer combined. The result show each separated record.
If campaign code is sorted ascending/descending, the last record row cells will always not combine even though the campaign code and vehicle no are matched. Below image shown campaign code sorted in ascending. So when the campaign code is sorted descending, all the CMP002 are combined, while the last record of CMP001 will not be combined as shown in below image anymore.
Code Behind
Private Sub GV_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GV.RowDataBound
For rowIndex As Integer = GV.Rows.Count - 2 To 0 Step -1
Dim gvRow As GridViewRow = GV.Rows(rowIndex)
Dim gvPreviousRow As GridViewRow = GV.Rows(rowIndex + 1)
Dim sCurrCampaignCode As String = GV.DataKeys(rowIndex).Values("CAMPAIGN_CODE")
Dim sCurrVehicleNo As String = GV.DataKeys(rowIndex).Values("VEHICLE_NO")
Dim sPreviousCampaignCode As String = GV.DataKeys(rowIndex + 1).Values("CAMPAIG_CODE")
Dim sPreviousVehicleNo As String = GV.DataKeys(rowIndex + 1).Values("VEHICLE_NO")
If sCurrCampaignCode = sPreviousCampaignCode AndAlso sCurrVehicleNo = sPreviousVehicleNo Then
If sCurrCampaignCode = sPreviousCampaignCode Then
If gvPreviousRow.Cells(1).RowSpan < 2 Then
gvRow.Cells(1).RowSpan = 2
gvRow.Cells(2).RowSpan = 2
gvRow.Cells(3).RowSpan = 2
Else
gvRow.Cells(1).RowSpan = gvPreviousRow.Cells(1).RowSpan + 1
gvRow.Cells(2).RowSpan = gvPreviousRow.Cells(2).RowSpan + 1
gvRow.Cells(3).RowSpan = gvPreviousRow.Cells(3).RowSpan + 1
End If
gvPreviousRow.Cells(1).Visible = False
gvPreviousRow.Cells(2).Visible = False
gvPreviousRow.Cells(3).Visible = False
End If
End If
Next
End Sub
I just found a solution. Code have to be moved from RowDataBound to OnDataBound instead
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 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 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