How to populate ObjectListView using Dataset - vb.net

Can someone please tell me what I'm doing wrong....
I am trying to populate an ObjectListView control by looping through a dataset (I don't want to bind it to my dataset). The first row populates but nothing after that.
My code looks like so:
If dsOverdueCalls.Tables.Count > 0 And dsOverdueCalls.Tables(0).Rows.Count > 0 Then
For x = 0 To (dsOverdueCalls.Tables(0).Rows.Count - 1)
'Calculate overdue amount
.....
Dim lstCalls = New List(Of [Call_Details])() From {New [Call_Details]() With {.Id = tempDT.Rows(x)("id").ToString, .Summary = tempDT.Rows(x)("summary").ToString, .First_Name = tempDT.Rows(x)("first_name").ToString, .Due_At = OverdueStr}}
lsvOverdueCalls.SetObjects(lstCalls)
Next
End If
I get no errors but only the first record will populate in my control.
Thanks

You're resetting the ObjectListView in each iteration. So, what you believe is the "first" row is actually the last row. The following code will fix your issue.
If ((dsOverdueCalls.Tables.Count > 0) AndAlso (dsOverdueCalls.Tables(0).Rows.Count > 0)) Then
Dim lstCalls = New List(Of [Call_Details])
For x As Integer = 0 To (dsOverdueCalls.Tables(0).Rows.Count - 1)
lstCalls.Add(New [Call_Details]() With {.Id = tempDT.Rows(x)("id").ToString, .Summary = tempDT.Rows(x)("summary").ToString, .First_Name = tempDT.Rows(x)("first_name").ToString, .Due_At = OverdueStr})
Next
lsvOverdueCalls.SetObjects(lstCalls)
End If

Related

vb.net how to clone listview item and add row with timestamp

I have a code that clones the listview item to the new listview (LSVCopyAdd), I want to add a timestamp to the item that was cloned in additional Column added for the time stamp (.Subitems(16)).
For i As Integer = LsvRead1.Items.Count - 1 To 0 Step -1
item1 = LsvRead1.Items(i)
For j As Integer = LsvCopyRemove.Items.Count - 1 To 0 Step -1
item2 = LsvCopyRemove.Items(j)
If item1.SubItems(1).Text = item2.SubItems(15).Text AndAlso
item1.SubItems(2).Text = LblGroup.Text Then
LsvCopyAdd.Items.Add(DirectCast(item2.Clone, ListViewItem))
End If
Next j
Next i
Any suggetions would be appriciated.
You can simply make this part of your code look like:
If item1.SubItems(1).Text = item2.SubItems(15).Text AndAlso
item1.SubItems(2).Text = LblGroup.Text Then
Dim newListViewItem As ListViewItem = DirectCast(item2.Clone, ListViewItem)
newListViewItem.SubItems(16).Text = "your time stamp value"
LsvCopyAdd.Items.Add(newListViewItem)
End If

Populate Treeview with data from string split with "\"

I have a large list which contains value like
List(0) = "Drive\First1\Folder2\Folder3"
List(1) = "Drive\Second2"
List(2) = "Drive\SubFolder1\ChildSubFolder"
Dim List = Split("Drive\First1\Folder2\Folder3", "\")
ParentNode = TreeView1.Nodes.Add(List(0))
For x = 1 To List.Count - 1
ParentNode.Nodes.Add(List(x))
Next
I am very confused about how to populate treeview control in vb.net
Can someone help me on this? Please. Thanks in advance.
You need two loops. One loop for the list, the second one to loop through the items that are separated by the slash. The tricky part is to differentiate between a "root node", which belongs to the TreeView control itself, and a "child node" that belongs to a parent node within that collection.
Once you have that figured out, you simply examine to see if the node already exists, and if it does, use that, otherwise, add it to the collection.
For Each item As String In List
Dim activeNode As TreeNode = Nothing
Dim nodeItems As TreeNodeCollection = Nothing
Dim subItems() As String = item.Split("\"c)
For i As Integer = 0 To subItems.Length - 1
nodeItems = If(i = 0, TreeView1.Nodes, activeNode.Nodes)
If nodeItems.ContainsKey(subItems(i)) Then
activeNode = nodeItems(subItems(i))
Else
activeNode = nodeItems.Add(subItems(i), subItems(i))
End If
Next
Next
I too find the answer but my code gives incorrect results... and #LarsTech code works perfectly. Thanks again LarsTech
Dim List(3) As String
List(0) = "Drive\First1\Folder2\Folder3"
List(1) = "Drive\Second2"
List(2) = "Drive\Second3\Folder4"
List(3) = "xDrive\Folder4\Folder5"
For Each ListItem In List
Dim Folders() = Split(ListItem, "\")
For i = 1 To Folders.Count - 1
Dim pNode = TreeView1.Nodes.Find(Folders(i - 1), True)
If pNode.Count = 0 Then
Dim pNode1 = TreeView1.Nodes.Add(Folders(i - 1), Folders(i - 1))
pNode1.Nodes.Add(Folders(i), Folders(i))
Else
If pNode(0).Nodes.Find(Folders(i), True).Count = 0 Then
pNode(0).Nodes.Add(Folders(i), Folders(i))
End If
End If
Next
Next

vb.net datagridview image column (change image - conditional)

I'm struggling with image column in my vb.net datagridview.
All what i want to do is that to change the image depending on a conditional time span.
Here is the full code :
con.Open()
da.SelectCommand = New OleDbCommand(Q, con)
da.Fill(ds)
da.Fill(dt)
DataGridView1.DataSource = dt
con.Close()
Dim receivedfrom As Date = Convert.ToDateTime(DateTimePicker1.Value)
Dim receivedto As Date = Convert.ToDateTime(DateTimePicker2.Value)
Dim difference As TimeSpan
Dim received As Date
Dim today As Date = today
Dim imgcol As New DataGridViewImageColumn()
Dim inImg As Image = My.Resources.red
imgcol.Image = inImg
DataGridView1.Columns.Add(imgcol)
imgcol.HeaderText = ""
imgcol.Name = "img"
imgcol.DataPropertyName = "img"
With DataGridView1
.Columns("img").DisplayIndex = 1
.Columns("img").Width = 28
.Columns("AC_RECEIVEDDT").DisplayIndex = 2
End With
For rowIndex = 0 To DataGridView1.RowCount - 1
received = DataGridView1.Rows(rowIndex).Cells("AC_RECEIVEDDT").Value
difference = today.Subtract(received)
If difference.Days < 2 Then
DataGridView1.Rows(rowIndex).Cells("img").Value = My.Resources.green
ElseIf difference.Days = 2 Then
DataGridView1.Rows(rowIndex).Cells("img").Value = My.Resources.yellow
Else
DataGridView1.Rows(rowIndex).Cells("img").Value = My.Resources.red
End If
Next
Now, when i open the application, all the images are coming in RED color only.
I have added the column name = "img", that's why i got the images already, but my problem is that all the images are not being changed based on the received date value, all the images are showing in red.
My plan was to have the case with a time span less than 2 days to appear with green image as an acceptable processing period. Equal to 2 = yellow image. More than 2 days = red.
I do have the red image now for all the rows, but its not getting changed based on the if statement, all rows have RED as i have assigned it in the image column.
Goal:
This is a DGV list with conditional status, that's exactly what i want to do
That's what appears to me now
Clearly, it is not applying the IF statement, is there anything wrong ? Am i missing something ?
Hope the idea is clear enough.
Any help would be appreciated.
Thanks !
The snapshot provided does not show the values for the column ac_receiveddt. Anyway assuming it contains range of dates and you want to display the color based on the difference. Try below. Use Days instead of TotalDays.
If difference.Days < 2 Then
DataGridView1.Rows(rowIndex).Cells("img").Value = My.Resources.Resource1.green
ElseIf difference.Days = 2 Then
DataGridView1.Rows(rowIndex).Cells("img").Value = My.Resources.Resource1.yellow
End If
I have added check for DateTime conversion
For rowIndex = 0 To DataGridView1.RowCount - 1
Dim rValue = DataGridView1.Rows(rowIndex).Cells("AC_RECEIVEDDT").Value
If DateTime.TryParse(rValue, received) Then
difference = today.Subtract(received)
If difference.Days < 2 Then
DataGridView1.Rows(rowIndex).Cells("img").Value = My.Resources.green
ElseIf difference.Days = 2 Then
DataGridView1.Rows(rowIndex).Cells("img").Value = My.Resources.yellow
Else
DataGridView1.Rows(rowIndex).Cells("img").Value = My.Resources.red
End If
Else
MessageBox.Show("not a DateTime")
End If
Next

Order DataTable after the population

I have a DataTable that I use to add, modify and delete data locally.
The DataTable is also used to populate a Grid.
I need to order the grid according to an integer value (importance).
I wanted to implement two buttons to increase or decrease the importance of each record.
For this reason I wrote this code:
Dim rowNumber As Integer = 0
For i As Integer = 0 To dataTable.Rows.Count - 1
'search for the selected row in the DataGridView by ID
If dataTable.Rows(i)("ID") = ID Then
rowNumber= i
End If
Next
If rowNumber <> 0 Then
dataTable.Rows(rowNumber )("Priority") -= 1
dataTable.Rows(rowNumber - 1)("Priority") += 1
End If
It works fine the first time, it changes the selected record and makes changes to all the others to adjust the order. The second time it generates an error.
This because the DataTable is not automatically sorted according to the new Priority Order.
I would like to know if there is a way to sort the DataTable after changing the original order of the records (field Priority)
Try using a DataView for that:
Dim dv As New DataView(dataTable)
dv.Sort = "Priority"
Then use the dv as the DataSource for the grid.
Based on your comments, I think you need to find the row that has the other priority number:
Dim rowNumber As Integer = 0
Dim priorityNumber As Integer = 0
For i As Integer = 0 To dt.Rows.Count - 1
If dt.Rows(i)("ID") = ID Then
rowNumber = i
priorityNumber = dt.Rows(i)("Priority")
dt.Rows(i)("Priority") -= 1
End If
Next
For i As Integer = 0 To dt.Rows.Count - 1
If dt.Rows(i)("ID") <> ID AndAlso dt.Rows(i)("Priority") = priorityNumber Then
dt.Rows(i)("Priority") += 1
End If
Next

populate combo box by consuming web service returning a dataset

I am trying to populate a combo list box with the results of a web service. The web service returns a dataset with two columns. I want to display column one to the user and capture column two with the user selection of an item from the combox.
right now I can display the first column and capture the selection.
Not sure how to add the connection of column two, to the combo box and capture the selection.
any help would be greatly appreciated!!
Code so far....
myDataSet1 = proxy3.listSuppLang()
Dim x As Integer
Dim dt As DataTable = myDataSet1.Tables(0)
Dim myString As String = ""
If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
'some code
For x = 0 To dt.Rows.Count - 1
myString = dt.Rows(x).Item(0)
ComboBox1.Items.Add(myString)
Next
ComboBox1.Visible = True
End If
I found this to work adquately....
If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
'some code
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "textFieldcolumnName"
ComboBox1.ValueMember = "valueFieldcolumnName"
ComboBox1.Visible = True
End If