VB.net datagridview to chart control devexpress - vb.net

I have a study about passing DataGridView values to DevExpress ChartControl. I have X and Y values in my DataGridView (It can have different row count). I would like to use for next loop due to different point counts (needs to stop after last value). Sometimes I have 5 values, sometimes 8, 12, ... etc. I have use code below:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To DataGridView1.Rows.Count - 1
ChartControl1.Series("Series 1").Points.Add(New SeriesPoint(DataGridView1.Item(0, i).Value, DataGridView1.Item(1, i).Value))
Next
End Sub
See also picture:

There is the new row in your DataGridView. You need to check for new row before adding the values from current row to your chart. To do this you can use the DataGridView.NewRowIndex property.
Here is example:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To DataGridView1.Rows.Count - 1
If i <> DataGridView1.NewRowIndex Then
ChartControl1.Series("Series 1").Points.Add(New SeriesPoint(DataGridView1.Item(0, i).Value, DataGridView1.Item(1, i).Value))
End If
Next
End Sub

Related

VB.NET: How to copy selected cell data to the first selected cell in other form?

My question may be confusing but here is the step by step process of the output I want to accomplish.
first when I click a cell in the fist form, another form pop's up. see images..
Image 1 Image 2
then if I click the cell data of the second form it should copy the selected row data of Code and Short Name to the first datagridview cell in form 1
Here is my code so far:
Private Sub SearchJournalGrid_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles SearchJournalGrid.CellContentDoubleClick
With GeneralJournal
.GridJournal.Rows(.GridJournal.SelectedRows(0).Index).Cells("Code").Value = SearchJournalGrid.Rows(SearchJournalGrid.SelectedRows(0).Index).Cells("CODE").Value.ToString
.GridJournal.Rows(.GridJournal.SelectedRows(0).Index).Cells("Account Name").Value = SearchJournalGrid.Rows(SearchJournalGrid.SelectedRows(0).Index).Cells("Short Name").Value.ToString
End With
End Sub
Let me know if I got it right!
Start a new project with only two forms in it, Form1 & Form2, add a DataGridView in each with their original name, which will DataGridView1 by default.
I have created 3 columns for each of them.
The code which has placed on the only Form1 is in the following:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i As Integer = 0 To 5
DataGridView1.Rows.Add(New String() {i + 1, "ITEM " & i + 1, "Description " & i + 1})
Next
End Sub
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim ROW_ID As Integer = DataGridView1.CurrentCell.RowIndex
Dim DGV_Row As DataGridViewRow = DataGridView1.Rows(ROW_ID).Clone
For J As Integer = 0 To DGV_Row.Cells.Count - 1
DGV_Row.Cells(J).Value = DataGridView1.Rows(ROW_ID).Cells(J).Value
Next
With Form2
.DataGridView1.Rows.Clear()
.DataGridView1.Rows.Add(DGV_Row)
End With
Form2.Show()
End Sub

Adding Multiple PictureBoxes To Array

So I have a total of 55 PictureBoxes that I am trying to add to an array. The naming of them looks like the Following:
Row1_Brick1, Row1_Brick2, up to Row1_Brick10
There is a total of 10 rows and there is 1 less brick in each row.
This is what I have thought of so far to make this work:
Dim bricks(0 To 54) As PictureBox 'Total of 55 Bricks Spread Out
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Start of Loading 55 Bricks into the bricks() Array
For i = 0 To 54
For a = 1 To 10
For am = 10 To 1 Step -1
bricks(i) = ("Row" & a & "_Brick" & am)
Next
Next
Next
End Sub
Any ideas on how to do this would be great.
I recommend a jagged array, which would look like this (note that this is 0-indexed rather than 1-indexed, as with your control names):
Dim bricks(10)() As PictureBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Set up child arrays
For i As Integer = 0 To 9
bricks(i) = New PictureBox(9-i)
'Set up each element in the array
For j As Integer = 0 To 9 - i
bricks(i)(j) = Me.Controls("Row" & i + 1 & "_Brick" & j + 1)
Next j
Next
End Sub
But if you really only want a single array, it is at least easier to set up (you might be able to get it down to a single line):
Dim bricks() As PictureBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
bricks = Me.Controls.OfType(Of PictureBox)().ToArray()
End Sub
If you need to, you can put in a Where() call to limit to pictureboxes where the name matches your patter, though it would be better to put these controls into a common Panel or GroupBox you can use as the parent rather than the form. You can also use an Orderby() call to make sure the PictureBoxes are returned in the proper sequence:
Dim bricks() As PictureBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
bricks = BrickPanel.Controls.
OfType(Of PictureBox)().
OrderBy(Function(pb) pb.Name). 'Naive OrderBy... 10 is before 2. I leave it to the OP to fix that part
ToArray()
End Sub
If you are unconformtalbe with the Linq functions, the trick is to increment your result array index as part of your inner loop, rather than having a separate loop by itself:
Dim bricks(54) As PictureBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim i As Integer = 0
For r As Integer = 1 To 10
For c As Integer = 1 to 11 - r
bricks(i) = Me.Controls("Row" & r & "_Brick" & c)
i+=1
Next c
Next r
End Sub
This is completely untested, because I was too lazy to create a new project and set up a form the same way you have, but it should be close.
Dim arr(54) As PictureBox
Dim index As Integer = 0
For row As Integer = 1 To 10
For col As Integer = 1 To 10 - row + 1 'Max column is based on the inverted value of the current row
arr(index) = Ctype(f.Controls("Row" & row & "_Brick" & col), PictureBox)
index += 1
Next
Next
This method of getting the control by the name explicitly will avoid any errors due to the order that the controls were added to the form.

Dividing listbox items into 2 parts

I have a listbox with random items, I wanted to divide it into 2 part's and put each part in one listbox. I was able to do it, but i got a very messy code, couldn't found any help online, so I just wanna to ask if there is another way of doing it. Here's my code.
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'items count
Dim count_listbox1 As Integer = ListBox1.Items.Count - 1
'half the count - 1 (im going to use it on the for loop)
Dim metade_listbox1_1 As Integer = (count_listbox1 / 2) - 1
'half the count
Dim metade_listbox1_2 As Integer = (count_listbox1 / 2)
' ( first part of the listbox)
For i = 0 To metade_listbox1_1
list1.Items.Add(ListBox1.Items.Item(i)) 'list1 - listbox that contains 1 half
Next
' (second part of the listbox items)
For i = metade_listbox1_2 To count_listbox1
list2.Items.Add(ListBox1.Items.Item(i)) 'list2 - listbox that contains 2 half
Next
'check if number of items its even or odd, if odd, deletes the first item of the list2,
'because its the same as the last item from list1
If eo = False Then
list2.Items.Remove(list2.Items.Item(0))
End If
End Sub
Well there are many ways to split the list. The following would distribute them evenly one by one
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
For i = 0 To ListBox1.Items.Count - 1
Dim lstbx As ListBox = If(i Mod 2 = 0, list1, list2)
lstbx.Items.Add(ListBox1.Items.Item(i))
Next
End Sub
An approach with a result more like yours would be this (requires importing System.Linq).
Note that I first set size2 to the half of the count so that it will have the extra item in case the number of items is odd.
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim size2 as Integer = ListBox1.Items.Count / 2
Dim size1 as Integer = ListBox1.Items.Count - size2
list1.Items.AddRange(ListBox1.Items.GetRange(0, size1))
list2.Items.AddRange(ListBox1.Items.GetRange(size1, size2))
End Sub

Merge two datagridviews

can anyone please tell me how to merge two datagridviews in vb.net? I have following code but data doesn't align in one row. (see attached pdf).
Code:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
For Each col As DataGridViewColumn In DataGridView2.Columns
DataGridView3.Columns.Add(DirectCast(col.Clone, DataGridViewColumn))
For Each col2 As DataGridViewColumn In DataGridView1.Columns
DataGridView3.Columns.Add(DirectCast(col2.Clone, DataGridViewColumn))
Next
Next
For j As Integer = 0 To (DataGridView2.Rows.Count - 1)
For k As Integer = 0 To (DataGridView1.Rows.Count - 1)
DataGridView3.Rows.Add(DataGridView2.Rows(j).Cells.Cast(Of DataGridViewCell).Select(Function(c) c.Value).ToArray())
DataGridView3.Rows.Add(DataGridView1.Rows(k).Cells.Cast(Of DataGridViewCell).Select(Function(c) c.Value).ToArray())
Next
Next
End Sub

Search Header Row and Go To The Specific Column That's Found

So the following code grabs the header row and populates each field in a combobox.
I then want to select a field name from the combobox and go to that column in the datagridview, is that possible?
Dim c As Integer
For cn = 0 To DataGridView1.Columns.Count - 1
c = c + cn
'Debug.Print(cn)
'Debug.Print(DataGridView1.Columns(cn).Name)
ComboBox1.Items.Add(DataGridView1.Columns(cn).Name)
Next cn
You'd want to check that the DGV has data and perhaps that there is a CurrentRow:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object,
e As EventArgs) Handles ComboBox1.SelectedIndexChanged
' ToDo: check that the selectedIndex is valid vs dgv.Columns.Count
If dgv1.CurrentRow IsNot Nothing Then
dgv1.CurrentCell = dgv1.CurrentRow.Cells(ComboBox1.SelectedIndex)
Else
dgv1.CurrentCell = dgv1.Rows(0).Cells(ComboBox1.SelectedIndex)
End If
End Sub