Search and display data in data grid view
Dim cmd As New OracleCommand("Select JV_CODE,JV_ACC_NAME,DEBIT,CREDIT From VOUCHER_DETAIL where VOUCHERNO =:Vno", sgcnn)
cmd.Parameters.Add("#Vno", OracleDbType.NVarchar2).Value = txtJVNo.Text.ToString.Trim
Dim daVD As New OracleDataAdapter(cmd)
Dim dtVD As New DataTable()
daVD.Fill(dtVD)
dgvAccDetail.AutoGenerateColumns = False
dgvAccDetail.DataSource = dtVD
dgvAccDetail.Rows(0).Cells(0).Value = dtVD.Rows(0).Item("JV_CODE").ToString.Trim
dgvAccDetail.Rows(0).Cells(1).Value = dtVD.Rows(0).Item("JV_ACC_NAME").ToString.Trim
dgvAccDetail.Rows(0).Cells(2).Value = dtVD.Rows(0).Item("DEBIT").ToString.Trim
dgvAccDetail.Rows(0).Cells(3).Value = dtVD.Rows(0).Item("CREDIT").ToString.Trim
Catch ex As Exception
MsgBox(ex.Message)
End Try
There are two rows in dtVD data table but result shows only one I stack with this some can tell what I'm doing wrong ?
The reason for this is that you are only assigning the one row. You have a couple options:
First, this code below here shouldn't really be necessary, of you setup the property binding on the grid. You'll get better performance that way if you use a bind. You don't have your GUI code here, but essentially your DataPropertyName field on the columns should be set.
dgvAccDetail.Rows(0).Cells(0).Value = dtVD.Rows(0).Item("JV_CODE").ToString.Trim
dgvAccDetail.Rows(0).Cells(1).Value = dtVD.Rows(0).Item("JV_ACC_NAME").ToString.Trim
dgvAccDetail.Rows(0).Cells(2).Value = dtVD.Rows(0).Item("DEBIT").ToString.Trim
dgvAccDetail.Rows(0).Cells(3).Value = dtVD.Rows(0).Item("CREDIT").ToString.Trim
If you insist on manually assigning the cell values, you cannot just do row 0 (that's the first row). You need to do all the rows in a loop.
For i as Integer = 0 to dtVD.Rows.Count - 1
dgvAccDetail.Rows(i).Cells(0).Value = dtVD.Rows(i).Item("JV_CODE").ToString.Trim
dgvAccDetail.Rows(i).Cells(1).Value = dtVD.Rows(i).Item("JV_ACC_NAME").ToString.Trim
dgvAccDetail.Rows(i).Cells(2).Value = dtVD.Rows(i).Item("DEBIT").ToString.Trim
dgvAccDetail.Rows(i).Cells(3).Value = dtVD.Rows(i).Item("CREDIT").ToString.Trim
Next
Related
I try to make grid columns during runtime, and one of column using repositorycheckedit, I tried several methods but none worked
here is my code
Dim dtSet As New DataSet()
Dim dtTable As New DataTable("menu")
Dim grCol As New DataColumn
Dim rpChk As New RepositoryItemCheckEdit
grCol.ColumnName = "smenu"
grCol.Caption = "Menu"
dtTable.Columns.Add("Menu")
dtTable.Columns(0).ColumnName = "smenu"
dtTable.Columns.Add("New")
dtTable.Columns(1).ColumnName = "snew"
dtTable.Rows.Add("Nama", 1)
dtSet.Tables.Add(dtTable)
gr.DataSource = dtSet
gr.DataMember = "menu"
rpChk.ValueChecked = 1
rpChk.ValueUnchecked = 0
grV.Columns("snew").ColumnEdit = rpChk
somehow, the column supposed to show checkbox always grayed out, if i try to mark the checkbox, it will become null after lost focus/change cell
pplease anyone.? thanks
Source #2 (After revised) and still no result as expected
Dim dtTable As New DataTable("menu")
Dim rpChk As New RepositoryItemCheckEdit
dtTable.Columns.Add("Menu")
dtTable.Columns(0).ColumnName = "smenu"
dtTable.Columns.Add("New")
dtTable.Columns(1).ColumnName = "snew"
dtTable.Rows.Add("Nama", CSByte(1))
dtTable.Rows.Add("Nama1", CSByte(0))
dtTable.Rows.Add("Nama2", CSByte(1))
dtTable.Rows.Add("Nama3", CSByte(0))
gr.DataSource = dtTable
rpChk.ValueChecked = 1
rpChk.ValueUnchecked = 0
grV.Columns("snew").ColumnEdit = rpChk
After tried several times, maybe someone needs answer
So finally I solved it. When create columns we need to define what type for the columns
dtTable.Columns.Add("Menu",GetType(String))
dtTable.Columns(0).ColumnName = "smenu"
dtTable.Columns.Add("New",GetType(Boolean))
dtTable.Columns(1).ColumnName = "snew"
And things finally worked.
This is my code
Public Sub InvoicItmCall()
'If IteDataTb.Rows.Count = 0 Then Exit Sub
SellingDGView.Rows.Add()
SellingDGView.Rows.Item(0).Cells(0).Value = SellingDGView.Rows.Item(0).Cells(0).Value + 1
SellingDGView.Rows.Item(0).Cells(1).Value = IteDataTb.Rows(0).Item("IteFName")
SellingDGView.Rows.Item(0).Cells(2).Value = IteDataTb.Rows(0).Item("IteFSizeUnit")
SellingDGView.Rows.Item(0).Cells(4).Value = IteDataTb.Rows(0).Item("IteFSellpris")
SellingDGView.Rows.Item(0).Cells(6).Value = IteDataTb.Rows(0).Item("IteVat")
'Dim unused = SellingDGView.Rows.Add(rows)
End Sub
Right now, the code works fine and fills the first row on the grid, this is for a barcode scanner. When I scan one barcode, it fills the row with the appropriate data. However, when I scan another code, it overwrites the older row.
I want it to add a new row and add the other data to it.
So when I scan a different item after the first one, it should fill up a new row.
How do I do that?
Thanks in advance :)
EDIT:
I have found a workaround
If IteDataTb.Rows.Count > 0 Then
For Each row As DataRow In IteDataTb.Rows
SellingDGView.Rows.Add(row.Item("IteFName"), row.Item("IteFSizeUnit"), row.Item("IteFSellpris"), row.Item("IteVat"))
Next row
End If
This creates a new row every time and fills it. But now I want to fill specific cells.
How do I do that?
Right now it fills everything inappropriately.
If you wanted to do it the way you originally were, I'd do it like this:
Dim index = SellingDGView.Rows.Add()
Dim newRow = SellingDGView.Rows(index)
'Dim newRow = SellingDGView.Rows(SellingDGView.Rows.Add())
Dim dataRow = IteDataTb.Rows(0)
newRow.Cells(0).Value = SellingDGView.Rows(0).Cells(0).Value + 1
newRow.Cells(1).Value = dataRow("IteFName")
newRow.Cells(2).Value = dataRow("IteFSizeUnit")
newRow.Cells(4).Value = dataRow("IteFSellpris")
newRow.Cells(6).Value = dataRow("IteVat")
If you wanted to use the more appropriate overload of Add that I mentioned and you used in your second example:
For Each row As DataRow In IteDataTb.Rows
SellingDGView.Rows.Add(SellingDGView.Rows(0).Cells(0).Value + 1,
row("IteFName"),
row("IteFSizeUnit"),
Nothing,
row("IteFSellpris"),
Nothing,
row("IteVat"))
Next
The If statement is pointless.
Also, I doubt that you're getting the next ID in the best way with that expression that adds 1 but that's a different question.
Every time that you need a new row, you can do as follows:
Dim i As Integer = SellingDGView.Rows.Add()
SellingDGView.Rows.Item(i).Cells(0).Value = i
SellingDGView.Rows.Item(i).Cells(1).Value = IteDataTb.Rows(j).Item("IteFName")
SellingDGView.Rows.Item(i).Cells(2).Value = IteDataTb.Rows(j).Item("IteFSizeUnit")
SellingDGView.Rows.Item(i).Cells(4).Value = IteDataTb.Rows(j).Item("IteFSellpris")
SellingDGView.Rows.Item(i).Cells(6).Value = IteDataTb.Rows(j).Item("IteVat")
Herein, I've assumed that the variable "j" indicates to your desired row in IteDataTb datagridview. I mean that you should control the "j" variable yourself. I can help you if know more about the IteDataTb.
Update 5/21/17. Thank you for the suggestion of using a Table. That was helpful. I actually figured it out. I made myinputable a global variable by declaring the Dim statement at the top and making it a Datagridview type. Now I can turn it off in the other event that I needed to do it.
I am a novice. I have created a Datagridview in VB 2015 to capture a bunch of data from the use. When the user is finished with the data entry, I want to store the cell values in my variables. I do not know how to capture any event from my dynamically created datagridview "myinputable." My code is below. Please help.
Private Sub inputmodel()
Dim prompt As String
Dim k As Integer
'
' first get the problem title and the number of objectives and alternatives
'
prompt = "Enter problem title: "
title = InputBox(prompt)
prompt = "Enter number of criteria: "
nobj = InputBox(prompt)
prompt = "Enter number of alternatives: "
nalt = InputBox(prompt)
'
' now create the table
'
Dim Myinputable As New DataGridView
Dim combocol As New DataGridViewComboBoxColumn
combocol.Items.AddRange("Increasing", "Decreaing", "Threashold")
For k = 1 To 6
If k <> 2 Then
Dim nc As New DataGridViewTextBoxColumn
nc.Name = ""
Myinputable.Columns.Add(nc)
Else
Myinputable.Columns.AddRange(combocol)
End If
Next k
' now add the rows and place the spreadsheet on the form
Myinputable.Rows.Add(nobj - 1)
Myinputable.Location = New Point(25, 50)
Myinputable.AutoSize = True
Me.Controls.Add(Myinputable)
FlowLayoutPanel1.Visible = True
Myinputable.Columns(0).Name = "Name"
Myinputable.Columns(0).HeaderText = "Name"
Myinputable.Columns(1).Name = "Type"
Myinputable.Columns(1).HeaderText = "Type"
Myinputable.Columns(2).Name = "LThresh"
Myinputable.Columns(2).HeaderText = "Lower Threshold"
'Myinputable.Columns(2).ValueType = co
Myinputable.Columns(3).Name = "UThresh"
Myinputable.Columns(3).HeaderText = "Upper Threshold"
Myinputable.Columns(4).Name = "ABMin"
Myinputable.Columns(4).HeaderText = "Abs. Minimum"
Myinputable.Columns(5).Name = "ABMax"
Myinputable.Columns(5).HeaderText = "Abs. Maximum "
Myinputable.Rows(0).Cells(0).Value = "Help"
If Myinputable.Capture = True Then
MsgBox(" damn ")
End If
End Sub
As #Plutonix suggests, you should start by creating a DataTable. Add columns of the appropriate types to the DataTable and then bind it to the grid, i.e. assign it to the DataSource of your grid, e.g.
Dim table As New DataTable
With table.Columns
.Add("ID", GetType(Integer))
.Add("Name", GetType(String))
End With
DataGridView1.DataSource = table
That will automatically add the appropriate columns to the grid if it doesn't already have any, or you can add the columns in the designer and set their DataPropertyName to tell them which DataColumn to bind to. As the user makes changes in the grid, the data will be automatically pushed to the underlying DataTable.
When you're done, you can access the data via the DataTable and even save the lot to a database with a single call to the Update method of a data adapter if you wish.
I am not a c# developer and I found myself at work working with a huge C# application...hug..
Now I am trying to populate an existing DataGrid with some more data which has been 'created' in a different way...let me show you what I mean.
This is how the existing DataView is generated:
Dim lDataSet As System.Data.DataSet
Dim lSqlCommand As New System.Data.SqlClient.SqlCommand
Dim lConvert As New PeoplePlanner.Common.Data.Convert
lSqlCommand.CommandType = CommandType.StoredProcedure
lsqlCommand.CommandText = "AccessIntegrationEmployeeInboundSearch"
lDataSet = objDatabase.GetDataSet(lSqlCommand)
If Not IsNothing(lDataSet) Then
objDataView = lDataSet.Tables(0).DefaultView
End If
There is probably be stuff missing in that code but hopefully it is enough to get an overview. It uses Stored Procedure (SP) to get some data from the DB and it returns the result inside 'lDataSet' and then we do 'objDataView = lDataSet.Tables(0).DefaultView'.
Now the new data which I want to 'merge in' it has the same fields but it is not the response of a SP but of an operation I have created which returns back a List of OnBoardingEmployee(let's call it like that) and if I want to correctly show ONLY the data inside that list this is what I do:
Dim lDataView As System.Data.DataView
Dim op = CoreInjector.Inject(Of IGetAllDataHubIncomingMessagesToBeProcess).Execute()
lDataView = Common.Detail.DataGridOperationHelper.ConvertToDataTable(op.OnBoardingEmployee).DefaultView
objDataView = lDataView
Where:
op contains a list of OnBoardingEmployee which has the same fields as the existing table
lDataView basically is the same as objDataView that's why as last step I assign objDataView = lDataView
Now I would basically like to merge, join, add, whatever
The objDataView created for the first table:
objDataView = lDataSet.Tables(0).DefaultView
with the one I have created afterwards:
lDataView = Common.Detail.DataGridOperationHelper.ConvertToDataTable(x.OnBoardingEmployee).DefaultView
How do I do that? :'(
They have the same data structure or better say they use the same view.
Thanks for the help :)
I have got it working :) this is my implementation:
Dim employeeJSONTable As DataTable
Dim employeeExistingTable As DataTable
Dim myDataRow As DataRow
Dim employeeID As Integer
if Request.QueryString.Get("Function") = "HubInbound" then
Dim employeeJSONList = CoreInjector.Inject(Of IGetAllDataHubIncomingMessagesToBeProcess).Execute()
employeeJSONTable = Common.Detail.DataGridOperationHelper.ConvertToDataTable(employeeJSONList.OnBoardingEmployee)
employeeJSONTable.PrimaryKey = New DataColumn(){ employeeJSONTable.Columns("EmployeeID")}
End If
lDataSet = objDatabase.GetDataSet(lSqlCommand)
employeeExistingTable = lDataSet.Tables(0)
For Each row As DataRow In employeeExistingTable.Rows
employeeID = row.Item("EmployeeID")
myDataRow = employeeJSONTable.Rows.Find(employeeID)
if (myDataRow Is Nothing) then
employeeJSONTable.ImportRow(row)
End If
Next row
If Not IsNothing(employeeJSONTable.DefaultView) And Request.QueryString.Get("Function") = "HubInbound" Then
objDataView = employeeJSONTable.DefaultView
Elseif Not IsNothing(lDataSet) Then
objDataView = lDataSet.Tables(0).DefaultView
End if
I have a RasGridView with 2 rows of data fetched from SQL to be displayed in the grid.
If oSQL.IsConnected Then
Try
ds = oSQL.GetDataSet("Select * from calc_umns order by calc_id", CommandType.Text)
If ds.Tables(0).Rows.Count > 0 Then
ds.Tables(0).TableName = "getSavedCalc"
RadGridView1.MasterGridViewTemplate.AutoGenerateColumns = False
RadGridView1.DataSource = ds
RadGridView1.DataMember = "getSavedCalc"
End If
Catch ex As Exception
End Try
End If
But the grid shows two rows with no data in it. could anyone guide where I am going wrong?
The FieldName property should be SET unique name and field value doesn't correspond to the SQL column names only FieldName property does.