Datagridview - Skip header - vb.net

I'm adding data from catia to datagridview. A portion of code where I'm having trouble is shown below. When I bind the datatable to datagridview, the data gets pasted starting from the header and I'm not sure how to skip the header and start from row 1. Appreciate any help. Thank you. enter image description here
Dim txtLines1 As New List(Of String())
_StringList.ToList().ForEach(Sub(x) txtLines1.Add(x.Split(New Char() {";"})))
If txtLines1.Count > 0 Then
txtLines1.Item(5).ToList.ForEach(Sub(x) txtDataTable.Columns.Add(New DataColumn(x.ToString)))
txtLines1.RemoveAt(1)
End If
If txtLines1.Count > 0 Then
txtLines1.ToList.ForEach(Sub(x) txtDataTable.Rows.Add(x.ToArray))
End If

You are using the values in Item 5 (the 6th one) of your list as your column name. That is what is displaying there. For each column in the data grid, you could update it to give it a more meaningful name.
txtLines1.Item(5).ToList.ForEach(Sub(x) txtDataTable.Columns.Add(New DataColumn(x.ToString)))
This is a way to give it a meaningful name:
txtDataTable.Columns(4).ColumnName = "5th Column"
txtDataTable.Columns(4).Caption = "5th Column"
Based on this 2nd set of code below, it then removes the 2nd item from the list, before adding the items to a DataTable. Not sure why you would remove the 2nd item.
txtLines1.RemoveAt(1)
End If
If txtLines1.Count > 0 Then
txtLines1.ToList.ForEach(Sub(x) txtDataTable.Rows.Add(x.ToArray))
End If
So your grid results display:
The 6th item as the column names
Followed by as the data:
1st Item
3rd List item (You removed the 2nd one - Item(1))
4th List item
5th List Item
6th List Item
...

Related

How to pass Value from Last Loop?

I have been attempting many ways to retrieve 2 collections together, while the first collection holds a comma-separated value in a column, we can not find a solution to passing the first collection value to the second For Each.
This code simply retrieves database rows and adds each result to our list control using the Add() method.
Dim transferstable As New DataTable
count = 0
For Each row As DataRow In transferstable.Rows
Dim name = Truncate(row.Item("name"), 42)
ListControl1.Add(name, row.Item("username")", row.Item("added"), avatars, row.Item("online"), images(count), 0)
count += 1
Next
Problem
We need to nest the loops, so we get the value from the first collection from the "avatars" column (image1,image2,image3) and call it from Add() - 4th parameter.
We only get always 1 string result into the view, while the actual query reports many rows with 2 strings (image1, image2) so I tried this:
Dim lst As New List(Of String) From {
transferstable.Rows(0).Item(8)
}
count = 0
For Each item As String In lst
For Each row As DataRow In transferstable.Rows
Dim name = Truncate(row.Item("name"), 42)
ListControl1.Add(name, row.Item("username")", row.Item("added"), item, row.Item("online"), images(count), 0)
count += 1
Next
Next
And the still the same single result! (8) is the GROUP_CONCAT column for "avatars" How do we pass this list over to the 4th parameter?
We want to retrieve these as URL remote images and render them to view with Bitmap.
Expected result:
A list of 15x15 pictures that represent each split result from GROUP_CONCAT(avatars)
I've been at all different ways to do this for most of the day, I know nesting is the right direction but I can't figure out why only 1 result is coming back (image1 - not image1,image2, etc.)
Some physical image files do not exist anymore, so rendering that to view also has it halt after a few single string results, so it quits and gives an error, like a 404 but does not proceed with the 180 other rows.
https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/for-each-next-statement

i can't fill my gridview vb.net

In my project , i have 2 windows forms and 1 gridview contains 5 columns (name gridv)
I want to fill 4 columns from forms1 and the 5th columun from the forms2
but i can't do it (error in my appli execution )
my code (how to fill the 5th column)
Dim selectrow As Integer = Form1.gridv.CurrentRow.Index ' selectrow mean selected row indice
MessageBox.Show("ligne selectionnée : " & Convert.ToDouble(selectrow))
Form1.gridv.Rows.Add(corp_mail.Text = Form1.gridv.Item(4, selectrow).Value) ' fill the 5th column`
You haven't shown the error but from what information you have given this is what your code is doing:
evaluates if corp_mail.Text is equal to the value of the 5th column of the selected row. This is a true or false, not a string.
Adds (or tries to) a new row to the grid with true or false as the value of the first cell.
If I understand correctly, this is what you want to do:
Form1.gridv.Item(4, selectrow).Value = corp_mail.Text
This will set the value of the 5th column of the selected row.
Although you mention a second grid on another form, not a textbox, so you probably have more work to do. But the idea is the same, instead of the TextBox value get the value from the other grid.

vb.net check the value of each column for only the 1st row datatable

I want to check the first row only of a data table. I want to verify that the right name is in the right column. The first row has header name and they have to be in the right order. for example I want to check the first row and the first column to check that it has "First Name"
If I do the following it give me the right column data but it gives me the 2nd row not the first. I don't get it since I am asking for row 0 and col 0.
Dim ExcelData As DataTable
field = ExcelData.Rows(0)(0).ToString()
(actually I will want to do a IF to check this column. first wanted to see what value is there before I code it all )

Data Validation of a Filtered table

I have a Data table with an Auto Filter (shown Below).
Sub Tariff_Filter()
Dim columnNumber, tableRow, tableColumn, tableWidth As Integer
Dim tableName, columnName As String
tableName = "Tariff_Table"
columnName = ActiveSheet.Range("A1").Value
'This clears the existing filter
ActiveSheet.ListObjects(tableName).Range.AutoFilter
'Assign some numbers we need to know about the table to check the headers
tableRow = ActiveSheet.ListObjects(tableName).Range.Row
tableColumn = ActiveSheet.ListObjects(tableName).Range.Column
tableWidth = ActiveSheet.ListObjects(tableName).Range.Columns.Count
'If a column title with the specified value does not exist VBA throws an error which we need to catch
On Error GoTo ErrorHandler
'Search through the table column header row to find the specified column and assign the number to columnNumber
columnNumber = Application.WorksheetFunction.Match(columnName, Range(Cells(tableRow, tableColumn), Cells(tableRow, tableColumn + tableWidth)), 0)
'Apply the filter "1" to the found columnNumber
ActiveSheet.ListObjects(tableName).Range.AutoFilter field:=columnNumber, Criteria1:="1"
'Exit the sub otherwise the "error handling" will be provoked
Exit Sub
ErrorHandler:
MsgBox columnName & "Please Specify Required Channel"
End Sub
As i cant seem to figure out how to get my combo-box's to show only the visible cells after filtering the table i was wondering if there is a way i can create a a validation box to show the visible cells or copy the visible data into a seperate table underneath. I can then use the validation box/ secondary table as a focus point for the combo-box's on the user-form.
Thanks in advance
If I'm understanding your question correctly, you would like to have a data-validation drop-down list that updates as the table is filtered and only displays visible items for a given column.
You can do this by using the following formula in Data Validation (I'm assuming your table header row starts in A1 and it's col A you need to display):
=OFFSET($A$2,,,SUBTOTAL(103,TableName[column name]))
This formula expands from the starting cell (A2) by a specified height in number of rows. We are defining the height using SUBTOTAL with function number 103 - this means that the height is defined using COUNTA, but only on visible cells, so it will expand and collapse as the table is filtered.
Be aware: since the height is defined using a counta function, it will only count cells containing data, therefore if you have blanks in your table, the range will not be defined correctly. Also if you have any repeated data, these will be repeated in your drop-down box, this method will not condense them into a neat, unique list.
Hope this is helpful.
D

Add Tab Pages equal to number of Rows

My Access DataBase has few Rows under single person. Some Person's row count is 1, but another person's row count more than 1.
I want create Tab Pages to insert that person's details.
When Creating Tab Pages,
Tab Pages count must be equal to Row Count. (If that person has 04 rows - 04 tab pages must be created.
Separate MS-Access DB row data (Column) put in to that tab Pages
If click 1st Tab Page : -----> 1st Rows of that Person Details (Field Data / Column) should be there with TextBoxes or Labels.
If Click 4th Tab Page : ------> 4th Row of that Person Details (Field Data / Column) should be there with TextBoxes or Labels.
I already created tab Pages according to row count with this code
Dim newPage As New TabPage()
Dim RecCount As Integer
While QReaderQ.Read()
RecCount = RecCount + 1 ' Count How many Rows
End While
TabControl1.TabPages.Clear()
For xXx = RecCount To 1 Step -1 ' to Desending Order ---->3,2,1
newPage = New TabPage 'create new instance
If xXx = 1 Then
newPage.Text = "Repeat - 1"
Else
newPage.Text = "Repeat - " & xXx.ToString
End If
TabControl1.TabPages.Add(newPage)
Next
I want put details from Database in to Tab Pages.
Example:
1st time repeat data to 1st Tab Page (repeat Data mean DB Column).
4th time repeat data to 4th Tab Page (repeat Data mean DB Column).
Since you're using Access, I'm going to assume you're using OleDb. You don't indicate what QReaderQ is, but based on the name it sounds like it's an OleDbDataReader.
Since (again, according to your posted code) you want to go in descending order, I'd suggest you use the OleDbDataAdapter instead - DataReaders are forward-only, so you can't go in reverse order.
I'd also suggest moving your code to create each tab page to its own function, and calling that function from within your For loop.
Putting this all together it might look something like this:
Dim TabContent As DataTable
' Pass in your selection string and your connection object
Dim Adapter As New OleDbDataAdapter("SELECT * FROM table", con)
Dim newPage As TabPage
' Fill the DataTable TabContent with the result from your select command
Adapter.Fill(TabContent)
TabControl1.TabPages.Clear()
For xXx As Integer = TabContent.Rows.Count To 1 Step -1
' Pass in the current row - use xXx - 1 since the row collection is 0 based
newPage = InitializeTabPage(TabContent.Rows(xXx - 1))
newPage.Text = "Repeat - " + xXx.ToString()
TabControl1.TabPages.Add(newPage)
Next
InitializeTabPage is a function that returns a TabPage. You'll need to create the controls on this page.
Public Function InitializeTabPage(ByVal Row As DataRow) As TabPage
Dim newPage As New TabPage()
' Create the control and bind the data from the row to them
Return newPage
End Function
In the InitializeTabPage function, you're taking a DataRow from the table. You'll need to create the TextBoxes and Labels on each tab page and then bind the appropriate data to them from the row.
For example, if you have a label called lblName, and a column in your row Name, you could do this:
lblName.Text = Row("Name").ToString()
Some helpful links:
OleDbDataAdapter Class
A Really Simple Database - Tutorial on VB.NET and Access