VB.net deleting row from DT - vb.net

I have the below code which works fine, although I want it to only delete the row if the whole row is empty. At the moment if there is no text in the first column it deletes the row even though there maybe text in the other columns.
Also want to trim the text as sometimes there are spaces in the columns with no text. this will ensure that blank rows are deleted.
Any help is greatly appreciated.
Thanks
For i As Integer = dt.Rows.Count - 1 To 0 Step -1
Dim row As DataRow = dt.Rows(i)
If row.Item(0) Is Nothing Then
dt.Rows.Remove(row)
ElseIf row.Item(0).ToString.Trim = "" Then
dt.Rows.Remove(row)
End If
Next

You are deleting the rows from just a datatable not from database.
Below code could help you.
Dim valuesarr As String = String.Empty
For i As Integer = 0 To dt.Rows.Count - 1
Dim lst As New List(Of Object)(dt.Rows(i).ItemArray)
For Each s As Object In lst
valuesarr &= s.ToString 'can use trim here if you want to delete spaces
Next
If String.IsNullOrEmpty(valuesarr) Then
'Remove row here, this row do not have any value
dt.Rows(i).Remove()
End If
Next

Related

For on a dataset, get column value

I am updating some old software in VB, and fixed this, but I'd like to know how to make the second option work.
The problem is in the second For loop in the code below. I'm not sure how to get a column of a specific row.
I have not worked much with VB and not used it for 10 years or so, so bare the question.
Dim i As Integer = 0
Dim dsSettings As New DataSet
dsSettings.Locale = System.Globalization.CultureInfo.InvariantCulture
If System.IO.File.Exists("QuaData\Settings.xml") Then
dsSettings.ReadXml("QuaData\Settings.xml")
ReDim Preserve IdPrefixes(dsSettings.Tables(0).Rows.Count - 1)
For Each Row As DataRow In dsSettings.Tables(0).Rows
For Each Coll As DataColumn In dsSettings.Tables(0).Columns
IdPrefixes(i) = Row("IdPrefix").ToString()
Next
i = i + 1
Next
' Here I cannot see how I can get a column of a row -
' like Rows(1)
' I cannot select Rows(index)(column name)
For i = 0 To dsSettings.Tables(0).Rows.Count - 1
IdPrefixes(i) = dsSettings.Tables(0).Rows(i)("IdPrefix").ToString()
Next
End If

How to compare 2 datagridview columns for duplicates vb.net

I am having difficulty figuring this out. I have 2 DataGridViews and I need to compare 1 column from each and highlight duplicates. I am populating the DataGrids from an SQL database. The code I have so far is below. All this does is highlight every row. I am unsure how to get it to look through the column and find any duplicate values not just in the same row number.
Dim RowOut As Integer
Dim RowIn As Integer
For Each rowOuter As DataGridViewRow In listOrderGrid.Rows
For Each rowInner As DataGridViewRow In listWebPollingGrid.Rows
RowOut = rowOuter.Cells("OrderID").Value
RowIn = rowInner.Cells("OrderID").Value
If rowInner.Cells("OrderID").Value = rowOuter.Cells("OrderID").Value Then
rowOuter.DefaultCellStyle.BackColor = Color.LightGreen
rowInner.DefaultCellStyle.BackColor = Color.LightGreen
End If
If rowOuter.Cells("OrderTypeName").Value = "Paid Out" Then
rowOuter.DefaultCellStyle.BackColor = Color.Violet
End If
Next
Next

First row as header in datagridview

Imported data from CSV to datagridview has header in first row, I could hide that row and name header for every column, problem is that data table has 68 columns. Is it possible to make first row header without having to do that manually for every single column?
Your datagrid should have taken all column names from CSV, if not then you need to have a look at this great article. It will help you a lot. This is the link.
After experimenting and help from user: ooopsoft :
Dim TextFieldParser1 As New Microsoft.VisualBasic.FileIO.TextFieldParser(receiveStream)
TextFieldParser1.Delimiters = New String() {","}
Dim newline() As String = TextFieldParser1.ReadFields()
While Not TextFieldParser1.EndOfData
Dim Row1 As String() = TextFieldParser1.ReadFields()
If DataGridView1.Columns.Count = 0 AndAlso Row1.Count > 0 Then
Dim i As Integer
For i = 0 To Row1.Count - 1
DataGridView1.Columns.Add(newline(i), newline(i))
Next
End If
End While

Is there a way to exclude blank excel cells on import using oledbadapter in a datagridview?

Hello I'm trying to find a way to trim blank cells on my datagridview if the excel cells are blank on import. I found this on MDSN but it doens't seem to be working. Maybe something along these lines will work. I greatly appreciate any help anyone can give!
Dim Empty As Boolean = True
For i As Integer = 0 To dataGridView1.Rows.Count - 1
Empty = True
For j As Integer = 0 To dataGridView1.Columns.Count - 1
If dataGridView1.Rows(i).Cells(j).Value IsNot Nothing AndAlso dataGridView1.Rows(i).Cells(j).Value.ToString() <> "" Then
Empty = False
Exit For
End If
Next
If Empty Then
dataGridView1.Rows.RemoveAt(i)
End If
Next
One problem is you need to work from the bottom up on the DGV:
For i As Integer = DataGridView1.Rows.Count - 1 To 0
If you work from the top down the index of any given row below the row removed changes.

Adding two column values to listbox in vb.net

I have a table named users which has the following columns in it
User_id,user_name,user_pwd,First_Name,Middle_Name,Last_Name and user_type.
I have dataset named dst and created a table called user in the dataset. Now I want to populate listbox with user_Name, First_Name, Last_name of each and every row in the table user.
I am able to add one column value at a time but not getting how to add multiple column values of each row to listbox
Dim dt As DataTable = Dst.Tables("user")
For Each row As DataRow In dt.Rows
lstUsers.Items.Add(row("User_Name"))
Next
Above code works perfectly but I also want to add First_name as well as last_name to the list box at the same time.
Use same approach as you have, but put all values you want in one string.
Dim dt As DataTable = Dst.Tables("user")
For Each row As DataRow In dt.Rows
Dim sItemTemp as String
sItemTemp = String.Format("{0},{1},{2}", row("User_Name"), row("First_Name"), row("Last_Name"))
lstUsers.Items.Add(sItemTemp)
Next
String.Format() function will call .ToString() on all parameters.
In this case if row(ColumnName) is NULL value then .ToString() return just empty string
You have 2 choices:
Using the ListBox:
To use the ListBox, set the font to one that is fixed width like courier new (so that the columns line up), and add the items like this:
For Each row As DataRow In dt.Rows
lstUsers.Items.Add(RPAD(row("User_Name"),16) & RPAD(row("First_Name"),16) & RPAD(row("Last_Name"),16))
Next
The RPAD function is defined like this:
Function RPAD(a As Object, LENGTH As Object) As String
Dim X As Object
X = Len(a)
If (X >= LENGTH) Then
RPAD = a : Exit Function
End If
RPAD = a & Space(LENGTH - X)
End Function
Adjust the LENGTH argument as desired in your case. Add one more for at least one space. This solution is less than ideal because you have to hard-code the column widths.
Use a DataGridView control instead of a ListBox. This is really the best option, and if you need, you can even have it behave like a ListBox by setting the option to select the full row and setting CellBorderStyle to SingleHorizontal. Define the columns in the designer, but no need to set the widths - the columns can auto-size, and I set that option in the code below. if you still prefer to set the widths, comment out the AutoSizeColumnsMode line.
The code to set up the grid and add the rows goes like this:
g.Rows.Clear() ' some of the below options are also cleared, so we set them again
g.AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.AllCells
g.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal
g.SelectionMode = DataGridViewSelectionMode.FullRowSelect
g.AllowUserToAddRows = False
g.AllowUserToDeleteRows = False
g.AllowUserToOrderColumns = True
For Each row As DataRow In dt.Rows
g.Rows.Add(row("User_Name"), row("First_Name"), row("Last_Name"))
Next
You might solved your problem by now but other users like me might have issue with it.
Above answers given worked for me even but I found a same answer in a simple way according to what I want..
cmd = New SqlCommand("select User_Name, First_Name, Last_Name from User")
Dim dr As SqlDataReader = cmd.ExecuteReader(YourConnectionString)
If dr.HasRows Then
Do While dr.Read
lst.Items.Add(dr.Item(0).ToString & " " & dr.Item(1).ToString & " " & dr.Item(2).ToString)
Loop
End If
This worked for me, maybe wrong way but I found it simple :)
May I suggest you use a ListView control instead of Listbox?
If you make the switch, here's a sample subroutine you could use to fill it up with the data you said you want. Adapt it the way you like; there's much room for improvement but you get the general idea:
Public Sub FillUserListView(lstUsers As ListView, Dst As DataSet)
Dim columnsWanted As List(Of String) = New List(Of String)({"User_Name", "First_Name", "Last_Name"})
Dim dt As DataTable = Dst.Tables("user")
Dim columns As Integer = 0
Dim totalColumns = 0
Dim rows As Integer = dt.Rows.Count
'Set the column titles
For Each column As DataColumn In dt.Columns
If columnsWanted.Contains(column.ColumnName) Then
lstUsers.Columns.Add(column.ColumnName)
columns = columns + 1
End If
totalColumns = totalColumns + 1
Next
Dim rowObjects(columns - 1) As ListViewItem
Dim actualColumn As Integer = 0
'Load up the rows of actual data into the ListView
For row = 0 To rows - 1
For column = 0 To totalColumns - 1
If columnsWanted.Contains(dt.Columns(column).ColumnName) Then
If actualColumn = 0 Then
rowObjects(row) = New ListViewItem()
rowObjects(row).SubItems(actualColumn).Text = dt.Rows(row).Item(actualColumn)
Else
rowObjects(row).SubItems.Add(dt.Rows(row).Item(actualColumn))
End If
lstUsers.Columns.Item(actualColumn).Width = -2 'Set auto-width
actualColumn = actualColumn + 1
End If
Next
lstUsers.Items.Add(rowObjects(row))
Next
lstUsers.View = View.Details 'Causes each item to appear on a separate line arranged in columns
End Sub