Problem with my usercontrol listview fill data from msaccess - vb.net

I created listview ms activex by vb.net to my access database
i add that code in vb.net to fill my listview
Public Sub FillListview(ByVal CurrntDb As String)
Dim conn As OleDbConnection
Dim cmd As OleDbCommand
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim itemcoll(100) As String
ListView1.View = View.Details
ListView1.GridLines = True
ListView1.Items.Clear()
conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" & CurrntDb)
Dim strQ As String = String.Empty
strQ = "SELECT Accounts.ID AS [م], Customers.Customer AS [العميل], Accounts.Debit AS [مدين], Accounts.Credit AS [دائن], Accounts.Dates AS [التاريخ], Accounts.Notes AS [البيان] FROM Accounts INNER JOIN Customers ON Accounts.Customer_ID = Customers.Customer_ID;"
cmd = New OleDbCommand(strQ, conn)
da = New OleDbDataAdapter(cmd)
ds = New DataSet
conn.Open()
da.Fill(ds)
Dim i As Integer = 0
Dim j As Integer = 0
' adding the columns in ListView
For i = 0 To ds.Tables(0).Columns.Count - 1
ListView1.Columns.Add(ds.Tables(0).Columns(i).ColumnName.ToString())
Next
'Now adding the Items in Listview
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
itemcoll(j) = ds.Tables(0).Rows(i)(j).ToString()
Next
Dim lvi As New ListViewItem(itemcoll)
ListView1.Items.Add(lvi)
ListView1.Refresh()
Next
conn.Close()
End Sub
and regestir it in my access database
and call it in access code by that code
Dim xlist As New AForTestListviewBySedo.UserControl1
Call xlist.FillListview("C:\Users\Elsayed\Desktop\New folder\mydb.accdb")
but they give me nothing not error but nothing
I tested that code in usercontrol by add button in it
button works good
What can I do ?

the solution is with my code in access to call record
i need to set object at first
the correct code is
Option Explicit
Option Compare Database
Public listv As MsAccessListviewACX1_00.UCBySedo
Public Sub LoadListview()
Set listv = Me.UCBySedo9.Object
listv.FillListview ("C:\Users\Elsayed\Desktop\New folder\mydb.accdb")
End Sub
Private Sub Command115_Click()
Call LoadListview
End Sub

Related

how to add list in `DATAGRIDVIEW` with combobox on runtime

i tried the following code but it adds the list every time when we start new row so if we have two rows its add it two times if we move to three rows it adds the list for three time
Please provide me simple and easy way to add the list into the combobox of datagridview
Dim CON As New MySqlConnection("server=localhost; username=root; password=Masoom1; database=airtech_db;")
Dim cmd As New MySqlCommand("Select * from `Suppliers`;", CON)
Dim da As New MySqlDataAdapter("Select * from `Suppliers`;", CON)
Dim ds As New DataSet
Dim dr As MySqlDataReader
Dim TOTAL_SUPPLIERS As Integer
CON.Open()
da.Fill(ds)
dr = cmd.ExecuteReader
TOTAL_SUPPLIERS = ds.Tables(0).Rows.Count
Dim TOTAL_SUPPLIERS_ARRAY(TOTAL_SUPPLIERS) As String, ARRAYINDEX As Integer
ARRAYINDEX = 0
Do While dr.Read() = True
TOTAL_SUPPLIERS_ARRAY(ARRAYINDEX) = dr("Supplier_Name").ToString()
ARRAYINDEX += 1
Loop
CON.Close()
Dim cbCell As New DataGridViewComboBoxCell
For k = 0 To DataGridView1.Rows.Count - 1
cbCell = DataGridView1.Rows(k).Cells("Supplier_Name")
For iIndex = 0 To UBound(TOTAL_SUPPLIERS_ARRAY) - 1
cbCell.Items.Add(TOTAL_SUPPLIERS_ARRAY(iIndex))
Next
Next
You can set it only once for a column.
For example you can populate combobox when loading a form.
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyForm.Load
With Me.DataGridView.Columns("Supplier_Name")
.DataSource = GetSupplierNames()
End With
End Sub
Private Function GetSupplierNames() As List(Of String)
Dim query As String = "SELECT Supplier_Name FROM Suppliers"
Using connection As New MySqlConnection(connectionString)
Using command As New MySqlCommand(query, connection)
connection.Open()
Dim supplierNames = new List(Of String)()
Using reader AS MySqlDataReader = command.ExecuteReader()
While reader.Read()
Dim name As String = reader.GetString(0)
supplierNames.Add(name)
End While
End Using
Return supplierNames
End Using
End Using
End Function
well I found a simple solution to existing code to fix the problem as every time it runs clear the existing list and let the list add again so add cbCell.Items.Clear()to code helped
Dim cbCell As New DataGridViewComboBoxCell
For k = 0 To DataGridView1.Rows.Count - 1
cbCell = DataGridView1.Rows(k).Cells("Supplier_Name")
cbCell.Items.Clear()
For iIndex = 0 To UBound(TOTAL_SUPPLIERS_ARRAY)
cbCell.Items.Add(TOTAL_SUPPLIERS_ARRAY(iIndex))
Next
Next

Delete Row / Remove Row

I want to delete my rows in gridview, but when I try many code the error are same.
When I try this
Using sqlCon As New SqlConnection(PyrDLL.Koneksi.ConnectionString)
Using cmd As New SqlCommand()
cmd.CommandText = "xyz"
cmd.Connection = sqlCon
sqlCon.Open()
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
Gridview1.DataSource = dt
Gridview1.DataBind()
sqlCon.Close()
If Gridview1.Rows.Count = 0 Then
Dim dtempty As DataTable = Nothing
dtempty = (DirectCast(Gridview1.DataSource, DataTable)).Clone()
dtempty.Rows.Add(dtempty.NewRow())
Gridview1.DataSource = dtempty
Gridview1.DataBind()
Gridview1.Rows(0).Visible = False
'Gridview1.Rows(0).Controls.Clear()
Else
For i As Integer = 0 To dt.Rows.Count - 1
dt.Rows(i)("rupiah") = PyrDLL.Decrypt(dt.Rows(i)("rupiah"))
dt.Rows(i)("rupiah") = Decimal.Parse(dt.Rows(i)("rupiah")).ToString()
'i = i + 1
Next
Gridview1.DataBind()
For i As Integer = Gridview1.Rows.Count - 1 To 1 Step -1
Dim row As GridViewRow = Gridview1.Rows(i)
Dim prevrow As GridViewRow = Gridview1.Rows(i - 1)
If (TryCast(Gridview1.Rows(i).Cells(1).FindControl("lblketerangan"), Label).Text.ToString() = TryCast(Gridview1.Rows(i - 1).Cells(1).FindControl("lblketerangan"), Label).Text.ToString()) And (TryCast(Gridview1.Rows(i).Cells(0).FindControl("lblcomp"), Label).Text.ToString() = TryCast(Gridview1.Rows(i - 1).Cells(0).FindControl("lblcomp"), Label).Text.ToString()) Then
Dim total As Integer = Convert.ToDecimal(TryCast(Gridview1.Rows(i - 1).Cells(2).FindControl("lblrupiah"), Label).Text)
Dim total2 As Integer = Convert.ToDecimal(TryCast(Gridview1.Rows(i).Cells(2).FindControl("lblrupiah"), Label).Text)
Dim total3 As Decimal
total3 = total + total2
DirectCast(Gridview1.Rows(i - 1).Cells(2).FindControl("lblrupiah"), Label).Text = Decimal.Parse(total3).ToString()
row.Visible = False
'Gridview1.Rows.Remove(Gridview1.Rows(i)) <--- if i comment here its run without problem
End If
Next
End If
End Using
End Using
the error
Remove' is not a member of 'System.Web.UI.WebControls.GridViewRowCollection'
Can you help me?
now, i hide the row after i sum it but i want to auto delete / remove row after i sum it not only hide
Thanks
I am not sure what you are trying to do when there are no rows, the gridview would be empty
You should do the following instead:
'Call GetData() every time you want to bind data to the gridview
Private Function GetData()
Gridview1.DataSource = Nothing
Gridview1.DataBind()
Dim strQuery = "SELECT..."
Dim dt As New DataTable()
Using sqlCon As New SqlConnection(PyrDLL.Koneksi.ConnectionString)
Using cmd As New SqlCommand(strQuery, sqlCon)
'Add cmd parameters as required to prevent SqlInjection
sqlCon.Open()
Using ada As New SqlDataAdapter(cmd)
ada.Fill(dt)
Gridview1.DataSource = dt
Gridview1.DataBind()
End Using
End Using
End Using
End Function
When handling the data in the gridview you can use RowDataBound, you could then call a separate function to handle deleting a row after your logic is handled where the criteria is met to delete a row
Protected Sub Gridview1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles Gridview1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
'Handle databound logic
End If
End Sub
You should be able to tailor this to your needs, just called GetData() everytime you need to rebind data to the gridview
I solved my problem
i cant remove rows from data gridview but i remove data from data table and bind it after thats
thanks everyone

Find String with row, column number from Datatable in VB.Net

Im reading excel sheet having two section as input and output section and import that to two Datagridview one for input and another one for Output section in VB.net 2008 Windows application.
If i have 10 rows and 10 columns for input section, then in my 11th row i have a text like 'End of Input Data' like the same i have in 11th column.
So by checking this,if i get the row and column number of this string i can import the data in to two data grid views. i can import only these row and column data in input gridview.
Below is the code for reading excel sheet. I don't know how to find string in Datatable. Or is there any other way to do that?
Private Sub ImpGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImpGrid.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim conStr As String, sheetName As String
Dim filePath As String = "C:\SIG.XLS"
Dim extension As String = ".xls"
conStr = String.Empty
conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filePath & ";Extended Properties=""Excel 8.0;HDR=No;IMEX=1"""
Using con As New OleDbConnection(conStr)
Using cmd As New OleDbCommand()
Using oda As New OleDbDataAdapter()
Dim dt As New DataTable()
' cmd.CommandText = (Convert.ToString("SELECT * From [") & sheetName) + "]"
cmd.CommandText = "select * from [Sheet1$]"
cmd.Connection = con
con.Open()
oda.SelectCommand = cmd
oda.Fill(dt)
con.Close()
'Populate DataGridView.
Loggridview.DataSource = dt
End Using
End Using
End Using
End Sub
Here's a function that will returns a list of hits. Each result is a Tuple with Item1 being the rowId and Item2 being the columnId
Public Structure SearchHit
Public ReadOnly RowIndex As Integer
Public ReadOnly CellIndex As Integer
Public Sub New(rowIndex As Integer, cellIndex As Integer)
Me.RowIndex = rowIndex
Me.CellIndex = cellIndex
End Sub
End Structure
Public Function FindData(ByVal value As String, table As DataTable) As List(Of SearchHit)
Dim results As New List(Of SearchHit)
For rowId = 0 To table.Rows.Count - 1
Dim row = table.Rows(rowId)
For colId = 0 To table.Columns.Count - 1
Dim cellValue = row(colId)
If value.Equals(cellValue) Then
results.Add(New SearchHit(rowId, colId))
End If
Next
Next
Return results
End Function

what is the correct format for calling a db in access, with a 'folder with spaces'?

Public connstring As String = "Provider = Microsoft.ACE.Oledb.12.0; Data Source = C:\Users\blablabla\Document\Visual Studio 2013\Project\rentalsystem\rental_db.accdb"
i placed this line of code under a module so i wont have to call it everytime. and this is the code that the code above is connected to...
Public Sub loadLVusers()
LVusers.Items.Clear()
Dim sqlcode As String = "select id_code, user_lastname, user_firstname, user_midname, user_username, user_password, user_email, user_privilege from tblUsers"
Dim sqlcomd As New OleDb.OleDbCommand(sqlcode)
sqlcomd.Connection = New OleDb.OleDbConnection(connstring)
sqlcomd.Connection.Open()
Dim DA As New OleDb.OleDbDataAdapter(sqlcomd)
Dim DS As New DataSet
DA.Fill(DS, "Pi")
If DS.Tables("Pi").Rows.Count > 0 Then
Dim Ic(100) As String
For r = 0 To DS.Tables("Pi").Rows.Count - 1
For c = 0 To DS.Tables("Pi").Columns.Count - 1
Ic(c) = DS.Tables("Pi").Rows(r)(c).ToString
Next
Dim LVI As New ListViewItem(Ic)
LVusers.Items.Add(LVI)
Next
End If
End Sub
now when the form/window that it is attached to loads, the form/window does not open. and then it highlights
sqlcomd.Connection = New OleDb.OleDbConnection(connstring)
so im guessing that has something to do with the file path format

DataGridView ScrollBar bug

When I get data for the DataGridView, my form freezes until the While loop completes, but then my scrollbar worked fine. I tried calling Application.DoEvents(); but that didn't work either.
If I get the data in a thread, then my form does not freeze, but the scrollbar disables and does not work after the While completes. I tried a BackgroundWorker but the scrollbar has a problem when using that too.
Private Sub dg()
myth = New Threading.Thread(AddressOf dgd)
myth.IsBackground = True
myth.Start()
End Sub
Private Sub dgd()
Dim x As Integer
If DataGridView1.Rows.Count = 0 Then x = 0 Else x = DataGridView1.Rows.Count
Try
Dim conn35a As New OleDbConnection("connstring")
Dim cmd35a As New OleDbCommand
cmd35a.CommandText = "Select count(*) from asd where downur Is Null"
cmd35a.CommandType = CommandType.Text
cmd35a.Connection = conn35a
conn35a.Open()
Dim returnValueaa As Integer = cmd35a.ExecuteScalar()
conn35a.Close()
Dim komut As String = "Select * from asd where downur Is Null"
Dim conn2 As New OleDbConnection("connstring")
conn2.Open()
Dim cmd2 As New OleDbCommand(komut, conn2)
Dim dr2 As OleDbDataReader = cmd2.ExecuteReader
If dr2.HasRows Then
While dr2.Read
Dim conn35 As New OleDbConnection("connstring")
Dim cmd35 As New OleDbCommand
cmd35.CommandText = "select count(*) from grid where ur = '" + dr2.Item("ur").ToString + "'"
cmd35.CommandType = CommandType.Text
cmd35.Connection = conn35
conn35.Open()
Dim returnValuea = cmd35.ExecuteScalar()
conn35.Close()
If returnValuea = 0 Then
DataGridView1.Rows.Add()
DataGridView1.Rows.Item(x).Cells(0).Value = x + 1
DataGridView1.Rows.Item(x).Cells(4).Value = "ID"
DataGridView1.Rows.Item(x).Cells(5).Value = dr2.Item("ur").ToString
DataGridView1.Rows.Item(x).Cells(6).Value = dr2.Item("ch").ToString
DataGridView1.Rows.Item(x).Cells(7).Value = dr2.Item("ti").ToString
DataGridView1.Rows.Item(x).Cells(8).Value = ".."
Dim client2 As New WebClient
Dim url As String = dr2.Item("pic").ToString
DataGridView1.Rows.Item(x).Cells(12).Value = New Bitmap(New MemoryStream(client2.DownloadData(url)))
DataGridView1.Rows.Item(x).Cells(13).Value = dr2.Item("vi")
DataGridView1.Rows.Item(x).Cells(14).Value = dr2.Item("su").ToString()
Dim con4 As New OleDbConnection("connstring")
con4.Open()
Dim cmd5 = New OleDbCommand("INSERT INTO grid (ur) VALUES (#ur)", con4)
cmd5.CommandType = CommandType.Text
cmd5.Parameters.Add("#ur", OleDbType.VarChar, 500).Value = dr2.Item("ur").ToString
cmd5.ExecuteNonQuery()
con4.Close()
x += 1
End If
End While
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
I had the same problem.
I solved this by removing the Thread and calling the method directly