DataTable.Rows.Count always returns 0 - VB.NET - vb.net

I'm trying to populate DataGridView cells with information from two (parent, child tables). Everything works just fine, except for this part table.Rows.Count. It always returns 0, no matter that I have one record in the table.
This is the code:
Private Sub Query__tbl_purchase_order_articlesDataGridView_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles Query__tbl_purchase_order_articlesDataGridView.CellEndEdit
Dim table As DataTable = Orders_DBDataSet.Tables("tbl_list_of_articles")
Dim selectedRowCount As Integer = Query__tbl_purchase_order_articlesDataGridView.CurrentCell.RowIndex
Dim art_ID As Integer = CInt(Query__tbl_purchase_order_articlesDataGridView.Rows(selectedRowCount).Cells(0).Value)
Dim art_ttl As String
Dim art_ttl_tr As String
Dim mu As String
Dim art_ttl_i As Integer
Dim art_ttl_tr_i As Integer
Dim mu_i As Integer
Dim art_ID_t_i As Integer
Dim art_ttl_t_i As Integer
Dim art_ttl_tr_t_i As Integer
Dim mu_t_i As Integer
Dim i As Integer
'search for column index in DataGridView
For i = 0 To Query__tbl_purchase_order_articlesDataGridView.Columns.Count - 1
If Query__tbl_purchase_order_articlesDataGridView.Columns(i).HeaderText.ToString = "article_name" Then
art_ttl_i = i
End If
If Query__tbl_purchase_order_articlesDataGridView.Columns(i).HeaderText.ToString = "article_name_tr" Then
art_ttl_tr_i = i
End If
If Query__tbl_purchase_order_articlesDataGridView.Columns(i).HeaderText.ToString = "masurement_unit" Then
mu_i = i
End If
Next
'search for column index in "table"
For i = 0 To table.Columns.Count - 1
If table.Columns(i).ColumnName.ToString = "article_ID" Then
art_ID_t_i = i
End If
If table.Columns(i).ColumnName.ToString = "article_name" Then
art_ttl_t_i = i
End If
If table.Columns(i).ColumnName.ToString = "article_name_tr" Then
art_ttl_tr_t_i = i
End If
If table.Columns(i).ColumnName.ToString = "masurement_unit" Then
mu_t_i = i
End If
Next
For i = 0 To table.Rows.Count
If art_ID = CInt(table.Rows(i).Item(art_ID_t_i).ToString()) Then
art_ttl = table.Rows(i).Item(art_ttl_t_i).ToString()
art_ttl_tr = table.Rows(i).Item(art_ttl_tr_t_i).ToString()
mu = table.Rows(i).Item(mu_t_i).ToString()
Query__tbl_purchase_order_articlesDataGridView.Rows(selectedRowCount).Cells(art_ttl_i).Value = art_ttl
Query__tbl_purchase_order_articlesDataGridView.Rows(selectedRowCount).Cells(art_ttl_tr_i).Value = art_ttl_tr
Query__tbl_purchase_order_articlesDataGridView.Rows(selectedRowCount).Cells(mu_i).Value = mu
Exit For
End If
Next
End Sub
Also, I tied to test it like this, but the result is the same. It can find a table index, but always return 0 as a table row count.
'test ========================================
Dim x, y As Integer
For x = 0 To Orders_DBDataSet.Tables.Count - 1
If Orders_DBDataSet.Tables(x).TableName.ToString = "tbl_list_of_articles" Then
y = x
End If
Next
x = 0
For Each row As DataRow In table.Rows
x = x + 1
Next
'============================================
x = Orders_DBDataSet.Tables(y).Rows.Count

Most of the times this happened to me because I was not actually using the instance of the dataset "connected" to the form. What does this mean? You either don't populate the dataset instance tied to the form in the Load event, or you are populating it but you are somehow using other instance of it. This has a higher chance of happening if you are sending data between forms as well.

Related

Find value of visible rows in datagridview

I'm able to get the correct row count, but the problem is when I run through the loop.
OrdNum = dgvWsNorth.Rows(i).Cells("ORDNUM").Value.ToString()
This line will loop through all visible and non visible rows = to the row count.
I need it to loop through only the visible rows = to the row count.
Dim AllRows As Integer = dgvWsNorth.Rows.GetRowCount(DataGridViewElementStates.Visible)
Dim OrdNum As Integer
Dim LinNum As Integer
Dim UnitPriority As Integer
Dim Ws_N As Integer = My.Settings.NorthLine
For i = 0 To AllRows - 1
OrdNum = dgvWsNorth.Rows(i).Cells("ORDNUM").Value.ToString()
LinNum = dgvWsNorth.Rows(i).Cells("LINE").Value.ToString()
If IsDBNull(dgvWsNorth.Rows(i).Cells("UNIT_PRIORITY").Value) Then
UnitPriority = 999
Else
UnitPriority = dgvWsNorth.Rows(i).Cells("UNIT_PRIORITY").Value.ToString()
End If
clsScheduler.UPDATE_ASSIGNED_WS(Ws_N, OrdNum, LinNum, clsLogin.plant_id, clsLogin.dept_id, UnitPriority)
Next
How about looping through all rows, and only counting the rows that are visible?
Dim i = 0
Dim OrdNum As Integer
Dim LinNum As Integer
Dim UnitPriority As Integer
Dim Ws_N As Integer = My.Settings.NorthLine
For Each r As DataGridViewRow In dgvWsNorth.Rows
If r.Visible Then
OrdNum = r.Cells("ORDNUM").Value.ToString()
LinNum = r.Cells("LINE").Value.ToString()
If IsDBNull(r.Cells("UNIT_PRIORITY").Value) Then
UnitPriority = 999
Else
UnitPriority = r.Cells("UNIT_PRIORITY").Value.ToString()
End If
clsScheduler.UPDATE_ASSIGNED_WS(Ws_N, OrdNum, LinNum, clsLogin.plant_id, clsLogin.dept_id, UnitPriority)
i += 1
End If
Next
Actually, I decided to give this a shot and I think you can do this with a Lambda on your for each statement
Instead of a for next loop use a for each with a lambda expression
For each r as datagridviewrow in DataGridView1.Rows.Cast(Of Datagridviewrow)().Where(Function(row) row.Visible = true)
... your loop code using r instead of rows(i)
Next
This should only iterate through rows that are visible.

I want to align Dynamic pictureboxs with Dynamic Labels

Here is my function that allows me to get from a webpage the image link for my PictureBoxs and the title for my labels
Public Shared Function getPics(website As String, pattern As String)
Dim tempTitles As New List(Of String)()
Dim tempTitles2 As New List(Of String)()
Dim lestitres As New List(Of titlesclass)
Dim webClient As New WebClient()
webClient.Headers.Add("user-agent", "null")
Dim counter As Integer = 0
Dim counter2 As Integer = 0
Dim counter3 As Integer = 0
Dim counter4 As Integer = 1
Dim counter5 As Integer = 0
Dim counter6 As Integer = 0
'If the website happens to go offline, at least your application wont crash.
Dim content As String = webClient.DownloadString(website)
Dim query = From title In Regex.Matches(content, pattern).Cast(Of Match)
Select New With {Key .Link = String.Concat("http://www.gamestop.com", title.Groups("Data").Value),
Key .Title = title.Groups("Dataa").Value}
For Each letitre In query.Distinct
'MsgBox(letitre.Link & " ======= " & letitre.Title)
Next
'For Each title As Match In (New Regex(pattern).Matches(content)) 'Since you are only pulling a few strings, I thought a regex would be better.
' Dim letitre As New titlesclass
' letitre.Link = title.Groups("Data").Value
' letitre.Title = title.Groups("Dataa").Value
' lestitres.Add(letitre)
' 'tempTitles2.Add(title.Groups("Dataa").Value)
'Next
Dim titles = tempTitles.Distinct().ToArray() 'remove duplicate titles
'Dim titles2 = tempTitles2.Distinct().ToArray()
Dim titles2 = lestitres.Distinct().ToArray()
lestitres.Clear()
'For Each title As titlesclass In titles2
For Each letitre In query.Distinct
'ListBox.Items.Add(title) 'what you do with the values from here is up to you.
Dim ImageInBytes() As Byte = webClient.DownloadData(letitre.Link)
Dim ImageStream As New IO.MemoryStream(ImageInBytes)
Dim MyPic As New PictureBox
Dim MyLab As New Label
If (counter2 > 0 AndAlso ((counter2 Mod 4 = 0) OrElse counter3 = 1)) Then
counter3 = 1
counter4 += 1
If (counter2 Mod 4 = 0) Then
counter5 = 0
counter6 += 170
End If
MyPic.Location = New Point(counter5, MyPic.Location.Y + counter6)
MyLab.Location = New Point(counter5, MyPic.Location.Y + counter6)
If counter4 = 4 Then
counter3 = 0
End If
counter5 += 200
Else
MyPic.Location = New Point(counter, MyPic.Location.Y)
MyLab.Location = New Point(counter, MyPic.Location.Y)
End If
counter += 200
counter2 += 1
MyPic.SizeMode = PictureBoxSizeMode.AutoSize
MyLab.Text = letitre.Title
MyPic.Image = New System.Drawing.Bitmap(ImageStream)
Form2.Controls.Add(MyPic)
Form2.Controls.Add(MyLab)
Next
End Function
The class named titlesclass contain two elements which i will store my Link and Title in :
Public Class titlesclass
Public Property Link As String
Public Property Title As String
End Class
And My little button does all the work
Dim websiteURL1 As String = "http://www.gamestop.com/collection/upcoming-video-games"
Class1.getPics(websiteURL1, "<img src=""(?<Data>[^>]*)""><p>(?<Dataa>[^>]*)<br>")
What i'm trying to do is to show 4 pictureboxs per row with the lables right bellow each picture , for now some labels doesn't show , some shows just in the right place and some shows very far bellow ! I tested the values i'm getting with a Message Box and it shows me the informations in the order i need , i'm not sure if i screw up in the x,y values or if it's something else ...
Edit : I can already show the 4 pictureboxes per row , the labels also , but the Y of some labels isn't well adjusted it can go far far bellow !
Here is some pictures that will help you to understand my situation Don't mind the buttons and listboxs , it's just for the test :
My list containt a lot of pictures , so i just showed you some when the thing work kind of well , when it shows couple of rows far from the designed picture
http://img110.xooimage.com/files/f/a/d/picture1-5239f7c.png
http://img110.xooimage.com/files/8/f/8/picture-2-5239f7e.png
http://img110.xooimage.com/files/4/7/b/picture-3-5239f80.png
http://img110.xooimage.com/files/f/0/f/picture4-5239f82.png
So I cleaned the way you where generating the position of the PictureBox by using a row principle and increment:
Note:
If you need space at the top to add information start the row count at 1 instead of 0
Note 2:
Here the image dimensions are harcoded but you could use dynamic ones which would be more fluid. I just cleaned the positioning code not the rest.
Replace your function by this one:
Public Shared Sub getPics(website As String, pattern As String)
Dim tempTitles As New List(Of String)()
Dim tempTitles2 As New List(Of String)()
Dim lestitres As New List(Of titlesclass)
Dim webClient As New WebClient()
webClient.Headers.Add("user-agent", "null")
Dim counter As Integer = 0
Dim counter2 As Integer = 0
Dim counter3 As Integer = 0
Dim counter4 As Integer = 1
Dim counter5 As Integer = 0
Dim counter6 As Integer = 0
'If the website happens to go offline, at least your application wont crash.
'Handle default proxy
Dim proxy As IWebProxy = WebRequest.GetSystemWebProxy()
proxy.Credentials = CredentialCache.DefaultCredentials
webClient.Proxy = proxy
Dim content As String = webClient.DownloadString(website)
Dim query = From title In Regex.Matches(content, pattern).Cast(Of Match)
Select New With {Key .Link = String.Concat("http://www.gamestop.com", title.Groups("Data").Value),
Key .Title = title.Groups("Dataa").Value}
Dim titles = tempTitles.Distinct().ToArray() 'remove duplicate titles
Dim titles2 = lestitres.Distinct().ToArray()
lestitres.Clear()
'Count Items
Dim item As Integer = 0
'Count Row
Dim row As Integer = 0
'image: 222*122
For Each letitre In query.Distinct
Dim ImageInBytes() As Byte = webClient.DownloadData(letitre.Link)
Dim ImageStream As New IO.MemoryStream(ImageInBytes)
Dim MyPic As New PictureBox
Dim MyLab As New Label
'x = numéro item fois largeur image
'y = numéro de ligne fois hauteur image
MyPic.Location = New Point(item * 222, row * 122)
MyLab.Location = New Point(item * 222, row * 122)
MyPic.SizeMode = PictureBoxSizeMode.AutoSize
MyLab.Text = letitre.Title
MyPic.Image = New System.Drawing.Bitmap(ImageStream)
Form1.Controls.Add(MyPic)
Form1.Controls.Add(MyLab)
'Bring Labels to front
For Each ctrl As Control In Form1.Controls
'If the control is of type label
If TypeOf ctrl Is Label Then
'Then bring to front
ctrl.BringToFront()
End If
Next
'Increment the item count
item = item + 1
'If item is multiple of 4 or could check 4 then
If item Mod 4 = 0 Then
'Reset counter
item = 0
'Increment Row
row = row + 1
End If
Next
End Sub
Example return:

how do i change the colour of a button based on a string input

For my computing project I am creating a game of risk this requires me to randomly generate the initial countries of the players (I have started with 2 players for simplicity), for this I am going to have the buttons (which represent the countries) to change colour to denote which of the players country it is.
I have tried to assign each player a string value of their colour then set the button back colour to that value. The problem with that at the moment is with the line of "b.Tag.BackColor = Players_Colours(N)" as the value that Players_Colours(N) gives is "S"c for some reason.
Any ideas why that is the case or anything would be appreciated.
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Public Function Random_country_picker(ByVal players)
Dim County_List = {"Oxfordshire", "Buckinhamshire", "Berkshire", "London", "Hampshire", "Surrey", "West_Sussex", "East_Sussex", "Kent", "Bedfordshire", "Cambridgeshire", "Hertfordshire", "Essex", "Cornwall", "Devon", "Gloucestershire", "Whiltshire", "Lancashire", "Cumbria", "Cheshire", "Manchester", "Merseyside", "Lincolnshire", "Staffordshire", "Herefordshire", "County_Durham", "North_Yorkshire", "South_Yorkshire", "Warwickshire", "Northumberland", "West_Yorkshire", "East_Yorkshire", "Shropshire", "Northamptonshire", "Derbyshire", "Nottinghamshire", "Worcestershire", "Somerset", "Dorset", "Isle_Of_White", "Norfolk", "Suffolk"}
Dim value As String
Dim num = 0
Dim nump = 42 / players
Dim N As Integer
Dim Players_Colours As String
Dim z As String
Players_Colours = {"Pink", "Red"}.ToString
N = 0
Randomize()
Dim rnd As New Random()
Dim index As Integer
While nump > num
If N > Players_Colours.Length Then
N = 0
End If
index = rnd.Next(0, County_List.Length)
value = County_List(index)
For Each b As Windows.Forms.Button In Me.Controls
If b.Tag = index Then
b.Tag.BackColor = Players_Colours(N)
End If
Next
Array.Clear(County_List, index, 1)
num += 1
N += 1
End While
Return 0
End Function
Random_country_picker(2)

Listbox formatting VB

I would like to format my listbox so that the output becomes something like this.
This is method, not in the main form tho:
Public Function GetSeatInfoStrings(ByVal choice As DisplayOptions,
ByRef strSeatInfoStrings As String()) As Integer
Dim count As Integer = GetNumOfSeats(choice)
If (count <= 0) Then
Return 0
End If
strSeatInfoStrings = New String(count - 1) {}
Dim StrReservation As String = ""
strSeatInfoStrings = New String(count - 1) {}
Dim i As Integer = 0 'counter for return array
'is the element corresponding with the index empty
For index As Integer = 0 To m_totNumOfSeats - 1
Dim strName As String = ""
Dim reserved As Boolean = Not String.IsNullOrEmpty(m_nameList(index))
'if the criteria below are not met, skip to add info in the array
If (choice = DisplayOptions.AllSeats) Or
(reserved And choice = DisplayOptions.ReservedSeats) Or
((Not reserved) And (choice = DisplayOptions.VacantSeats)) Then
If (reserved) Then
StrReservation = "Reserved"
strName = m_nameList(index)
Else
StrReservation = "Vacant"
strName = "..........."
End If
strSeatInfoStrings(i) = String.Format("{0,4} {1,-8} {2, -20} {3,10:f2}",
index + 1, StrReservation, strName, m_priceList(index))
i += 1
End If
Next
Return count
End Function
I don't know how to format the listbox as the strSeatInfoStrings(i) in the main form.
My listbox
This is what I've done
Private Sub UpdateGUI()
'Clear the listbox and make it ready for new data.
ReservationList.Items.Clear()
'size of array is determined in the callee method
Dim seatInfoStrings As String() = Nothing
Dim calcOption As DisplayOptions = DirectCast(cmbDisplayOptions.SelectedIndex, DisplayOptions)
Dim count As Integer = m_seatMngr.GetSeatInfoStrings(calcOption, seatInfoStrings)
If count > 0 Then
ReservationList.Items.AddRange(seatInfoStrings)
Else
ReservationList.Items.Add("Nothing to display!")
End If
Found the error! I forgot to call the UpdateGUI() in the IntializeGUI().

Error on inserting new line on gridview

I am trying to add a new row into a gridview but for some reason i'm having a problem in the for loop.
Directly goes to dtCurrentTable.Rows.Add(drCurrentRow) and of course, have an error "'row' argument cannot be null.Parameter name: row", because the dtcurrentTable.NewRow was not executed.
Why is this happening?
Private Sub AddNewRowToGrid()
Dim rowIndex As Integer = 0
If Not IsNothing(ViewState("CurrentTable")) Then
Dim dtCurrentTable As DataTable = CType(ViewState("CurrentTable"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then
For i as Integer = 1 To i <= dtCurrentTable.Rows.Count
' Extraem-se os valores das Textbox
Dim box1 As TextBox = Dados.Rows(rowIndex).Cells(0).FindControl("Artigo")
Dim box2 As TextBox = Dados.Rows(rowIndex).Cells(1).FindControl("Descricao")
Dim box3 As TextBox = Dados.Rows(rowIndex).Cells(2).FindControl("IVA")
Dim box4 As TextBox = Dados.Rows(rowIndex).Cells(3).FindControl("PU")
Dim box5 As TextBox = Dados.Rows(rowIndex).Cells(4).FindControl("Desconto")
Dim box6 As TextBox = Dados.Rows(rowIndex).Cells(5).FindControl("UN")
Dim box7 As TextBox = Dados.Rows(rowIndex).Cells(6).FindControl("Quantidade")
Dim box8 As TextBox = Dados.Rows(rowIndex).Cells(7).FindControl("TotalLiquido")
drCurrentRow = dtCurrentTable.NewRow
dtCurrentTable.Rows(i - 1)("Artigo") = box1.Text
dtCurrentTable.Rows(i - 1)("Descricao") = box2.Text
dtCurrentTable.Rows(i - 1)("IVA") = box3.Text
dtCurrentTable.Rows(i - 1)("PU") = box4.Text
dtCurrentTable.Rows(i - 1)("Desconto") = box5.Text
dtCurrentTable.Rows(i - 1)("UN") = box6.Text
dtCurrentTable.Rows(i - 1)("Quantidade") = box7.Text
dtCurrentTable.Rows(i - 1)("TotalLiquido") = box8.Text
rowIndex += 1
Next i
dtCurrentTable.Rows.Add(drCurrentRow)
ViewState("CurrentTable") = dtCurrentTable
Dados.DataSource = dtCurrentTable
Dados.DataBind()
End If
Else
Response.Write("ViewState null")
End If
SetPreviousData()
End Sub
Your For loop is defined wrong, thats why you're getting an error:
For i as Integer = 1 To i <= dtCurrentTable.Rows.Count
evaluates to
For i as Integer = 1 To True
(because i is always <= Rows.Count)
which VB translates as
For i as Integer = 1 To -1
which means your loop never runs.
It
should be
For i as Integer = 1 To dtCurrentTable.Rows.Count
Also, there's something odd about the way you use drCurrentRow = dtCurrentTable.NewRow. Why is that inside the loop when you don't do anything with it in the loop? It gets executed multiple times and then dtCurrentTable.Rows.Add(drCurrentRow) only gets called once.
Its hard for me to correct because I can't figure out what you're trying to do, but that bit looks dodgy to me.