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
Related
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()
enter image description hereI'm trying to validate a textbox where users will put an ID. The ID has to be numeric and at the same time compare to a valid ID in the database. When I was just validating for numeric, I didn't have any problems. But now that I have two conditions, my code doesn't work properly. Whenever I type in letters in the textbox and click a button, it gives me an error. The boolean is throwing me off lol. Below is my code:
Thank you in advance.
Protected Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
Dim dt As DataTable
Dim dr As DataRow
Dim Conn As New SqlConnection("Data Source=Computer;Initial Catalog=Catalog;Persist Security Info=True;User ID=userid;Password=password")
Dim cmd As New SqlCommand("SELECT COUNT(*) FROM [tbl] WHERE [ID]=#Value", Conn)
cmd.Parameters.Add("#Value", SqlDbType.NVarChar).Value = txtId.Text
Conn.Open()
Dim valueExistsInDB As Boolean = CBool(CInt(cmd.ExecuteScalar()) > 0)
Conn.Close()
If (IsNumeric(txtId.Text)) AndAlso valueExistsInDB = True AndAlso txtId.Text IsNot Nothing Then
dt = GetDataTable("SELECT ID, LEFT(SANZ_ID, PATINDEX('%.%', SANZ_ID) -1) AS City, CASE WHEN ST_DIR_ID = 1 THEN 'NB' WHEN ST_DIR_ID = 2 THEN 'SB' WHEN ST_DIR_ID = 3 THEN 'EB' WHEN ST_DIR_ID = 4 THEN 'WB' END AS ST_DIR, STREET_OF_TRAVEL, CROSS_STREET, (SELECT TOP 1 CASE WHEN STATUS_ID = 1 THEN 'F' WHEN STATUS_ID = 2 THEN 'P' WHEN STATUS_ID = 3 THEN 'I' WHEN STATUS_ID = 4 THEN 'N' WHEN STATUS_ID = 5 THEN 'A' END FROM tbl where dbo.tbl.ID=ID) AS STATUS FROM tbl WHERE ID=" & txtId.Text)
dr = dt.Rows(0)
labelStreet.Text = dr("street_of_travel")
labelCrossStreet.Text = dr("cross_street")
labelCity.Text = dr("city")
labelDir.Text = dr("st_dir")
labelAda.Text = dr("STATUS")
'dropdownStatus.SelectedValue=
dropdownStatus.Visible = True
txtNotes.Visible = True
btnSave.Visible = True
Else
MessageBox.Show("ID not found! Please input a valid ID.")
End If
End Sub
If ID is a numeric field then you should pass a numeric parameter not an NVarChar one
' First try to convert the input text to an integer '
' this should be done here before acting on the db '
Dim id As Integer
if Not Int32.TryParse(txtId.Text, id) Then
MessageBox.Show("Error, not a valid number")
return
End If
Dim cmdText = "SELECT COUNT(*) FROM [tbl] WHERE [ID]=#Value"
Using Conn = New SqlConnection(....)
Using cmd As New SqlCommand(cmdText, Conn)
cmd.Parameters.Add("#Value", SqlDbType.Int).Value = id
Conn.Open()
Dim valueExistsInDB = CBool(CInt(cmd.ExecuteScalar()) > 0)
' At this point you don't need anymore to check if the input value'
' is numeric and your if is more simple.....'
if valueExistsInDB Then
......
... continue with your code ....
Else
MessageBox.Show("ID not found! Please input a valid ID.")
End if
End Using
End Using
Hi i am trying to build a Gridview Based on my Web Service Data.
I am not not getting any errors when I compile the code, however the gridview does not display. Not sure if this is because I am building the gridview wrong or the data is not being pulled correctly. The web service does work because I have tested it.
Here is the code i am using to bind the gridview:
Private Sub BindGrid()
Dim objVen As New ISISVendor.VendorInterfaceClient
Dim LoginResp As New ISISVendor.LoginResponse
Dim POResp As New ISISVendor.PoSummaryResponse
Dim pod As New ISISVendor.PoDetailResponse
Dim dt As New DataTable
dt.Columns.Add("Status")
dt.Columns.Add("Sender")
dt.Columns.Add("PO Number")
dt.Columns.Add("Date")
dt.Columns.Add("Action")
gvv.DataSource = dt
POResp = objVen.GetOpenPos("3274")
Dim dr As DataRow
If POResp.Pos.Count > 0 Then
For j As Integer = 0 To POResp.Pos.Count - 1
dr = dt.NewRow
dr.Item("Status") = POResp.Pos(j).Status
dr.Item("Sender") = "COMPANY TEST"
dr.Item("PO Number") = POResp.Pos(j).PoNumber
dr.Item("Date") = POResp.Pos(j).PoDate
dr.Item("Action") = ""
Next
End If
gvv.DataBind()
End Sub
Seem you are not adding the datarow to the data table
Please try as below
POResp = objVen.GetOpenPos("3274")
Dim dr As DataRow
If POResp.Pos.Count > 0 Then
For j As Integer = 0 To POResp.Pos.Count - 1
dr = dt.NewRow
dr.Item("Status") = POResp.Pos(j).Status
dr.Item("Sender") = "COMPANY TEST"
dr.Item("PO Number") = POResp.Pos(j).PoNumber
dr.Item("Date") = POResp.Pos(j).PoDate
dr.Item("Action") = ""
dt.Rows.Add(dr)
Next
End If
gvv.DataSource = dt
gvv.DataBind()
I want to generate dynamic table in Xtrareport in a proper format.
So I am using this code
dynamicTable.Borders = DevExpress.XtraPrinting.BorderSide.All
dynamicTable.BorderWidth = 1
But it is not giving proper output as I desire..
It is generating table by separating each cell row by row, so it doesn't give a table appearance.!
[1]: http://i.stack.imgur.com/iaKl9.png
The complete code:
Dim label As New XRLabel()
label.Width = 500
label.Font = New System.Drawing.Font("Verdana", 10.0F, FontStyle.Bold)
PageHeader1.Controls.Add(label)
If rowsCount > 0 Then
Dim padding As Integer = 5
Dim tableWidth As Integer = Me.PageWidth - Me.Margins.Left - Me.Margins.Right - padding * 2
Dim dynamicTable As XRTable = XRTable.CreateTable(New Rectangle(padding, 1, tableWidth, 40), 1, 0) ' table column count
dynamicTable.Width = tableWidth
dynamicTable.Rows.FirstRow.Width = tableWidth
dynamicTable.Borders = DevExpress.XtraPrinting.BorderSide.All
'dynamicTable.Borders = DirectCast(((DevExpress.XtraPrinting.BorderSide.Left Or DevExpress.XtraPrinting.BorderSide.Top) Or DevExpress.XtraPrinting.BorderSide.Right) Or DevExpress.XtraPrinting.BorderSide.Bottom, DevExpress.XtraPrinting.BorderSide)
dynamicTable.BorderWidth = 1
Dim i As Integer = 0
dynamicTable.BeginInit()
For Each dc As DataColumn In ds.Tables(0).Columns
Dim cell As New XRTableCell()
Dim binding As New XRBinding("Text", ds, ds.Tables(0).Columns(i).ColumnName)
cell.DataBindings.Add(binding)
cell.CanGrow = False
cell.CanShrink = True
cell.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter
'cell.WidthF = 10
'cell.Width = 20
cell.Text = dc.ColumnName
dynamicTable.Rows.FirstRow.Cells.Add(cell)
i += 1
Next dc
dynamicTable.Font = New System.Drawing.Font("Verdana", 8.0F)
dynamicTable.AdjustSize()
dynamicTable.EndInit()
Detail.Controls.Add(dynamicTable)
'label.Text = String.Format("Data table: {0}", Test)
Me.DataSource = ds
Me.DataMember = Test
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