I have a gridview like below picture:
In this gridview I want to introduce a new row that does not have any completely out of the cell where the last "Grand Total" there to put a variable mine.
But the question is, how introduce a new row among others?
My code is:
Dim Qty As Double = 0
Dim CostCategory As Double = 0
Dim AssemblyDate As String = Nothing
If GridView1.Rows.Count <> 0 Then
For x As Integer = 0 To GridView1.Rows.Count - 1
For y As Integer = 0 To GridView1.Columns.Count - 1
If y = 0 And GridView1.Rows(x).Cells(1).Text = AssemblyDate Then
GridView1.Rows(x).Cells(1).Text = ""
End If
If GridView1.Rows(x).Cells(1).Text <> Nothing Then
AssemblyDate = GridView1.Rows(x).Cells(1).Text
End If
If y = "9" Then
Qty = Convert.ToDouble(GridView1.Rows(x).Cells(9).Text)
End If
If y = "13" Then
CostCategory = Convert.ToDouble(GridView1.Rows(x).Cells(13).Text)
End If
If y = "14" Then
GridView1.Rows(x).Cells(14).Text = Qty * CostCategory
GridView1.Rows(x).Cells(14).HorizontalAlign = HorizontalAlign.Center
End If
If GridView1.Rows(x).Cells(y).Text = "Yes" Then
GridView1.Rows(x).Cells(y).BackColor = System.Drawing.Color.LawnGreen
ElseIf GridView1.Rows(x).Cells(y).Text = "No" Then
GridView1.Rows(x).Cells(y).BackColor = System.Drawing.Color.Red
End If
Next
Next
End if
before that code i have a query where i fill my gridview:
Dim myQuery As String = SelectQuery & WhereQuery
SqlDataSource1.SelectCommand = myQuery
SqlDataSource1.DataBind()
GridView1.DataSourceID = "SqlDataSource1"
GridView1.DataBind()
Thanks a lot!
Try using this
DataRow dr = tblTable.NewRow(); //Create New Row
dr["ColumnName"] = "Legs"; // Set Column Value
tblTable.Rows.InsertAt(dr, 11); // InsertAt specified position
For VB.Net Grid View
Dim rowNumber As Integer = dGrid.CurrentCell.RowNumber() + 1
Dim myNewRow As DataRow = myDataTable.NewRow()
myDataTable.Rows.InsertAt(myNewRow, rowNumber)
myDataTable.AcceptChanges()
Related
In a GridView I am adding Grouping rows to the underlying table in the RowDataBound, but when I ApplyChanges and loop through each GridViewRow not all rows can be tested for TextBox value etc, the number of rows is reduced by the number of grouping rows inserted. Although it does loop through the grouping rows which I ignore, and misses the last n rows.
Private tmpSiteName As String = ""
Private Sub gvVehicles_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvVehicles.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim drv As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
If tmpSiteName <> drv("lcname").ToString() Then
tmpSiteName = drv("lcname").ToString()
Dim tbl As Table = TryCast(e.Row.Parent, Table)
If tbl IsNot Nothing Then
Dim row As New GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal)
Dim cell As New TableCell()
' Span the row across all of the columns in the Gridview
cell.ColumnSpan = Me.gvVehicles.Columns.Count
cell.Width = Unit.Percentage(100)
'cell.Style.Add("font-weight", "bold")
cell.Style.Add("background-color", "#f5f5f5")
cell.Style.Add("color", "#000000")
Dim span As New HtmlGenericControl("span")
span.InnerHtml = tmpSiteName
cell.Controls.Add(span)
row.Cells.Add(cell)
tbl.Rows.AddAt(tbl.Rows.Count - 1, row)
End If
End If
End If
End Sub
Private Sub ApplyChanges()
Dim AuctionDB As fleet.AuctionDB = New fleet.AuctionDB
Dim OldFleetNo As String = ""
Dim NewFleetNo As String = ""
Dim OldReserve As Integer = 0
Dim NewReserve As Integer = 0
Dim MTA As Long = 0
Dim Sitekey As String = ""
Dim vcaudit As Long = 0
Dim i As Integer = gvVehicles.Rows.Count
Try
For Each DRow As GridViewRow In gvVehicles.Rows
Sitekey = TryCast(DRow.FindControl("hiddenSitekey"), HiddenField).Value
MTA = Val(TryCast(DRow.FindControl("hiddenMTA"), HiddenField).Value)
strChange = ""
' The Grouping rows throw up funny results, but we can catch MTA = 0
If MTA > 0 Then
OldFleetNo = TryCast(DRow.FindControl("hiddenFleetNo"), HiddenField).Value
NewFleetNo = TryCast(DRow.FindControl("txtFleetNo"), TextBox).Text
If NewFleetNo <> OldFleetNo Then
vcaudit = AuctionDB.FleetUpdates(Sitekey, MTA, "FLEET NO", OldReserve, NewFleetNo, Session("user_name"))
updateCount += 1
strChange = "Fleet No. changed from " & OldFleetNo & " to " & NewFleetNo & "<br /><br />"
End If
End If
Next DRow
Catch ex As Exception
ShowMessage("Error: " & ex.Message, "alert-danger")
End Try
End Sub
I have two datatables (dtSF and CurveFitTable) that contain two different values which I have to multiply. The objective is to produce a datatable that contains the product of two values from two different datatables. The twist is, the CurveFitTable came from different csv files in a directory which I already defined.
What I intended to do is to have a datatable like the adjustedCopy table in the image below. Unfortunately, what I'm getting is a single datatable which kept on being overwritten and whenever I attempt to databind it to the grid, the datatable seems to be empty. Please help. T.T
This is my code:
Dim adjusteddemandtable As New DataTable()
Dim adjustedcopy As New DataTable()
Dim multiply_SF As Double
Dim adjusted_Demand As Double
Dim initial_Demand As Double
Dim basecurvestamp As Date
adjusteddemandtable.Columns.Add("Base Curve", GetType(Date))
adjusteddemandtable.Columns("Base Curve").SetOrdinal(0)
adjusteddemandtable.Columns.Add("Adjusted_Demand", GetType(Double))
Dim CurveFitTatble As New DataTable()
Try
For Each filename As String In System.IO.Directory.GetFiles(BackUpDirectory)
CurveFitTatble = GetDataTabletFromCSVFile(filename)
For Each row2 As DataRow In dtSF.Rows()
For Each row As DataRow In CurveFitTatble.Rows()
initial_Demand = row(1)
basecurvestamp = row(0)
multiply_SF = row2(0)
adjusted_Demand = multiply_SF * initial_Demand
Dim rowa As DataRow = adjusteddemandtable.Rows.Add()
rowa(0) = basecurvestamp
rowa(1) = adjusted_Demand
Next
Next
adjustedcopy.Merge(adjusteddemandtable, True, MissingSchemaAction.AddWithKey)
Next
GridView1.DataSource = adjustedcopy
GridView1.DataBind()
Catch ex As Exception
ErrorMessageBox(ex.Message)
End Try
I think, I'm missing something or overlooked an important step. Please advise. Thanks in advance.
I just did what #jmcilhinney told me (that is to replace nested Foreach nested loop. Here is my new code (and fortunately, it is working as its expected output requires)
Try
Dim y As Integer = System.IO.Directory.GetFiles(BackUpDirectory).Length
Dim row1 As DataRow
Dim i As Integer = 0
While i < y
row1 = dtSF.Rows(i)
Dim filenames As String() = System.IO.Directory.GetFiles(BackUpDirectory)
Dim filename As String = filenames(i)
multiply_SF = row1(0)
CurveFitTatble = New DataTable()
Dim TS_Name As String = "TmeStamp" + "_" & i
Dim AD_Name As String = "AdjustedDemand" + "_" & i
If i = 0 Then
adjusteddemandtable.Columns.Add(TS_Name, GetType(Date))
adjusteddemandtable.Columns(TS_Name).SetOrdinal(i)
adjusteddemandtable.Columns.Add(AD_Name, GetType(Double))
adjusteddemandtable.Columns(AD_Name).SetOrdinal(i + 1)
ElseIf i > 0 Then
adjusteddemandtable.Columns.Add(TS_Name, GetType(Date))
adjusteddemandtable.Columns(TS_Name).SetOrdinal(i + 1)
adjusteddemandtable.Columns.Add(AD_Name, GetType(Double))
adjusteddemandtable.Columns(AD_Name).SetOrdinal(i + 2)
End If
'If row1(0) = filename Then
CurveFitTatble = GetDataTabletFromCSVFile(filename)
'For Each row As DataRow In CurveFitTatble.Rows()
Dim row As DataRow
For j As Integer = 0 To CurveFitTatble.Rows.Count - 1
row = CurveFitTatble.Rows(j)
initial_Demand = row(1)
basecurvestamp = row(0)
adjusted_Demand = multiply_SF * initial_Demand
Dim rowa As DataRow = adjusteddemandtable.Rows.Add()
If i = 0 Then
rowa(i) = basecurvestamp
rowa(i + 1) = adjusted_Demand
ElseIf i > 0 Then
rowa(i + 1) = basecurvestamp
rowa(i + 2) = adjusted_Demand
End If
Next
i = i + 1
End While
Catch ex As Exception
ErrorMessageBox(ex.Message)
End Try
I have a shopping cart that using 2d array Cart(3, 20) to store user shop in a session.
It storing data like this:
Cart(0,0) = Product_ID
Cart(1,0) = Product_Name
Cart(2,0) = Product_Price
Cart(3,0) = Product_Qty
I want to count Items based on product_id ( we have not repetitive product_id)
I found a function here:
Function UniqueEntryCount(SourceRange)
Dim MyDataset
Dim dic
Set dic=Server.CreateObject("Scripting.Dictionary")
MyDataset = SourceRange
For i = 1 To UBound(MyDataset, 2)
if not dic.Exists(MyDataset(0, i)) then dic.Add MyDataset(0, i), ""
Next
UniqueEntryCount = dic.Count
Set dic = Nothing
End Function
But one problem is remain, When my Cart is empty, it show me 1
How can solved it?
An unitialized fixed array (Dim a(i, j)) contains i * j empty elements; your
if not dic.Exists(MyDataset(0, i)) then dic.Add MyDataset(0, i), ""
will pick up and count the first empty item. Demonstrated in code:
Dim afCart(3, 4)
Dim dicPID : Set dicPID = countPID00(afCart)
Dim aKeys : aKeys = dicPID.Keys
Dim vKey : vKey = aKeys(0)
WScript.Echo "A", dicPID.Count, TypeName(vKey)
Set dicPID = countPID(afCart)
WScript.Echo "B", dicPID.Count
afCart(0, 0) = "ignored"
afCart(0, 1) = 4711
afCart(0, 2) = 4712
afCart(0, 3) = 4711
' afCart(0, 4) = "not initialized/Empty"
Set dicPID = countPID(afCart)
WScript.Echo "C"
For Each vKey In dicPID.Keys
WScript.Echo "", vKey, "=", dicPID(vKey)
Next
Function countPID00(afCart)
Dim dicRVal : Set dicRVal = CreateObject("Scripting.Dictionary")
Dim MyDataset : MyDataset = afCart ' waste of ressources
Dim iRow
For iRow = 1 To UBound(MyDataset, 2)
If Not dicRVal.Exists(MyDataset(0, iRow)) Then
dicRVal(MyDataset(0, iRow)) = "" ' loss of info; will pick up Empty item
End If
Next
Set countPID00 = dicRVal
End Function ' countPID00
Function countPID(afCart)
Dim dicRVal : Set dicRVal = CreateObject("Scripting.Dictionary")
Dim iRow
For iRow = 1 To UBound(afCart, 2)
If Not IsEmpty(afCart(0, iRow)) Then
dicRVal(afCart(0, iRow)) = dicRVal(afCart(0, iRow)) + 1
End If
Next
Set countPID = dicRVal
End Function ' countPID
output:
A 1 Empty
B 0
C
4711 = 2
4712 = 1
Hey all i am trying to figure out how to add some of my model numbers from my database to am array while looping.
This is my code:
objCmd = New MySqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
If dtReader.HasRows() Then
grdViewItems.DataSource = dtReader
grdViewItems.DataBind()
Dim arrayOfItems As String() = New String() {}
While dtReader.Read()
arrayOfItems(intX) = dtReader.GetString("model_number")
'arrayItems(intX) = dtReader.GetString("model_number")
intX += 1
End While
'Session.Add("arrayOfItems", arrayItems)
Session("arrayOfItems") = arrayOfItems
dtReader.Close()
dtReader = Nothing
objConn.Close()
objConn = Nothing
End If
Problem being that it never populates the array with the values (although the values are present in the gridview because it skips the reading of the values in the while loop.
What am i doing incorrectly here?
Complete code
Dim intX As Integer = 0
objConn = New MySqlConnection(product.strConnString)
objConn.Open()
strSQL = "SELECT prod.id, PP.model_number, description, price, price_new, price_direct, price_builder " & _
"FROM product as prod " & _
"INNER JOIN product_price as PP ON PP.product_id = prod.type " & _
"WHERE prod.id = " & pageID & ";"
Try
objCmd = New MySqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
If dtReader.HasRows() Then
grdViewItems.DataSource = dtReader
grdViewItems.DataBind()
Dim arrayOfItems As New List(Of String)()
For Each row As GridViewRow In grdViewItems.Rows
arrayOfItems(intX) = row.Cells(0).Text.ToString
intX = intX + 1
Next
MsgBox(arrayOfItems(0))
'Session.Add("arrayOfItems", arrayItems)
Session("arrayOfItems") = arrayOfItems
dtReader.Close()
dtReader = Nothing
objConn.Close()
objConn = Nothing
End If
Catch ex As Exception
MsgBox("LoadProductItems: " & ex.Message)
End Try
Instead of array, I would recommend using List of int or string
It looks like the problem is in your loop. Replace the following code
Do While dtReader.Read
arrayOfItems(intX) = dtReader.GetString("model_number")
intX += 1
Loop
'with this code
While dtReader.Read()
arrayOfItems(intX) = dtReader.GetString("model_number")
intX += 1
End While
Updated Answer:
When you assign data to gridview then also save the data in a datatable like this
Dim dt As DataTable = New DataTable
dt = dtReader
`Then get the data from the table
Dim i As Integer = 0
Do While (i < dt.Rows.Count)
dt.Rows(i)("COlumnName").ToString
i = (i + 1)
Loop
Again Updated Answer:
Read the data from the gridview
For Each row As GridViewRow In grdViewItems.Rows
arrayOfItems(intX) = row.Cells(0).Text.ToString ` in cell specify the index of the model_number column
intX = intX + 1
Next
I have one datatable tempDT with value :
Serial_No testong
---------------------------------------
DTSHCSN001205035919201 [ OUT ] <br/> Partner : 90000032 <br/> Date : 16 Feb 2012
DTSHCSN001205035919201 [ IN ] <br/> Partner : 90000032 <br/> Date : 16 Feb 2012
DTSHCSN001205035919201 [ OUT ] <br/> Partner : 80000869 <br/> Date : 31 Mar 2012
DTSHCSN001205035919201 [ IN ] <br/> Partner : 80000869 <br/> Date : 31 Mar 2012
The problem is I want merge duplicate serial_no into one row which the value of testong adding to new column.
I have tried many ways, but I can't find the solution.
Here is my code behind :
Dim tempDt = GetItemDataTable()
Dim dtData As New DataTable
dtData.Columns.Add("Serial_No")
Dim i As Integer = 0
Dim row As DataRow
For Each row In tempDt.Rows
i += 1
Dim dr As DataRow = dtData.NewRow
dr("Serial_No") = row(0)
If dr("Serial_No") = row(0) Then
Dim colBaru As GridViewDataTextColumn = New GridViewDataTextColumn()
colBaru.Caption = i
colBaru.FieldName = i
colBaru.CellStyle.HorizontalAlign = HorizontalAlign.Center
colBaru.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
colBaru.Width = 150
colBaru.UnboundType = DevExpress.Data.UnboundColumnType.Integer
colBaru.VisibleIndex = grid.VisibleColumns.Count
colBaru.PropertiesTextEdit.DisplayFormatString = "d1"
colBaru.PropertiesTextEdit.EncodeHtml = True
grid.Columns.Add(colBaru)
dtData.Columns.Add(i)
dr(i) = row(3)
dtData.Rows.Add(dr)
Else
dr("Serial_No") = row(0)
dtData.Rows.Add(dr)
End If
When I debug the result is :
But I wanted the result is like this :
I have updated my code, so it would look at the columns rather using variable i
Dim tempDt = GetItemDataTable()
Dim dtData As New DataTable
'initial the first and second columns
dtData.Columns.Add("Serial_No")
dtData.Columns.Add("1")
Dim i As Integer = 0
Dim row As DataRow
For Each row In tempDt.Rows
Dim dr As DataRow
i += 1
'check if the serial no exists in the new Data Table
If dtData.Select("Serial_No='" & row(0) & "'").Length > 0 Then
'If found, then get the existing row
dr = dtData.Select("Serial_No='" & row(0) & "'")(0)
'Create new GridViewDataTextColumn
Dim colBaru As GridViewDataTextColumn = New GridViewDataTextColumn()
colBaru.Caption = i
colBaru.FieldName = i
colBaru.CellStyle.HorizontalAlign = HorizontalAlign.Center
colBaru.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
colBaru.UnboundType = DevExpress.Data.UnboundColumnType.Integer
colBaru.VisibleIndex = grid.VisibleColumns.Count
colBaru.PropertiesTextEdit.DisplayFormatString = "d1"
colBaru.PropertiesTextEdit.EncodeHtml = False
grid.Columns.Add(colBaru)
'I assume you are adding the Number as the columns name
'Only need to create if the Column doesn't exist
If dtData.Columns.count - 1 < i Then
dtData.Columns.Add(i.ToString)
End If
'Use variable i here
dr(i) = row(3)
'Comment this out as you don't need to
'dtData.Rows.Add(dr)
Else
'reset value of i
i = 1
'If not found, then create a new row
dr = dtData.NewRow
' i put this to add the serial_no value to datatable
dr("Serial_No") = row(0)
'for adding first value i with same row as serial_no
Dim colBaru As GridViewDataTextColumn = New GridViewDataTextColumn()
colBaru.Caption = i
colBaru.FieldName = i
colBaru.CellStyle.HorizontalAlign = HorizontalAlign.Center
colBaru.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
colBaru.UnboundType = DevExpress.Data.UnboundColumnType.Integer
colBaru.VisibleIndex = grid.VisibleColumns.Count
colBaru.PropertiesTextEdit.DisplayFormatString = "d1"
colBaru.PropertiesTextEdit.EncodeHtml = False
grid.Columns.Add(colBaru)
'dtData.Columns.Add("1")
'Would be better to use back variable i if you have reset it
dr(i) = row(3)
dtData.Rows.Add(dr)
End If
Next