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

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

Related

if the file not exits then return no error vb

I have start this application every things its work fine but i have a small bug but i can not find the solution to solve the error.
i have debug it and the error its because the file not exist
is there any way to me to populate my datagridview with all *.gif images From a directory and the check if its null or some thing like that.
What i mean is is there any way to my to populate all gif images found on the chose Directory?
in fact i have all ready try like this but i get one error "Provided column already belongs to the DataGridView control.
Well finaly i have found a solution to load all images from a directory to a datagridview programmatic
Here Is The Working Code
Public Class Form5
Private Sub addBtn_Click(sender As Object, e As EventArgs) Handles addBtn.Click
'Populate()
ShowImages()
End Sub
'CLEAR DATAGRIDVIEW
Private Sub clearBtn_Click(sender As Object, e As EventArgs) Handles clearBtn.Click
DataGridView1.Rows.Clear()
End Sub
'WHEN AN IMAGE IS CLICKED
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
MessageBox.Show("You Clicked Image At Col: " + e.ColumnIndex.ToString() + " Row: " + e.RowIndex.ToString())
End Sub
Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Public Sub ShowImages()
Dim directory As New System.IO.DirectoryInfo("C:\avitogifconverter\")
If directory.Exists Then
Dim pngFiles() As System.IO.FileInfo = directory.GetFiles("*.gif")
For Each pngFile As System.IO.FileInfo In pngFiles
If pngFile.Exists Then
Dim image = System.Drawing.Image.FromFile(pngFile.FullName)
Using image
Dim count = 1
' do something with the image like show in picture box
'CONSTRUCT IMG COLUMN
Dim imgCol As DataGridViewImageColumn = New DataGridViewImageColumn()
imgCol.HeaderText = "Photo"
imgCol.Name = "Col 1"
DataGridView1.Columns.Add(imgCol)
'CONSTRUCT ROWS
'FIRST ROW
Dim img As Image = System.Drawing.Image.FromFile(pngFile.FullName)
Dim row As Object() = New Object() {img, img, img}
DataGridView1.Rows.Add(row)
End Using
End If
Next
End If
End Sub
End Class
Using Directory.EnumerateFiles, you could do something like this:
Dim row = New List(Of Image)(3)
For Each filename In Directory.EnumerateFiles("C:\avitogifconverter", "*.gif")
row.Add(Image.FromFile(filename))
If row.Count = 3 Then
DataGridView1.Rows.Add(row.ToArray())
row.Clear()
End If
Next
If row.Count > 0 Then
DataGridView1.Rows.Add(row.ToArray())
row.Clear()
End If

Edit Selected Datagridview Row vb.Net

I have a Datagridview grid with four columns. When a cell is double-clicked, the data on the selected row is passed to four textboxes for the user to make changes if any.So how do i pass the changes made to the selected row instead of having the changes added as new row?
P.S. The Data isn't from a database
You can either "remember" the DataGridViewRow by setting a module-level variable, or you can find the row again by looking for its primary key.
Public Class Form1
'Add to form:
' DataGridView called DataGridView1
' 4 Textboxes called TextBox1, TextBox2, TextBox3, and TextBox4
' Button called btnEdit
' Button called btnSave
Private mintRowWeAreEditing As Integer = -1
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
If DataGridView1.DataSource Is Nothing Then
'set initial data
Dim dtb As New DataTable
dtb.Columns.Add("Col1")
dtb.Columns.Add("Col2")
dtb.Columns.Add("Col3")
dtb.Columns.Add("Col4")
dtb.Rows.Add("R1C1", "R1C2", "R1C3", "R1C4")
dtb.Rows.Add("R2C1", "R2C2", "R2C3", "R2C4")
dtb.Rows.Add("R3C1", "R3C2", "R3C3", "R3C4")
dtb.Rows.Add("R4C1", "R4C2", "R4C3", "R4C4")
DataGridView1.DataSource = dtb
End If
'copy data from grid to textboxes
mintRowWeAreEditing = DataGridView1.CurrentCell.RowIndex
Dim drw As DataRow = DirectCast(DataGridView1.Rows(mintRowWeAreEditing).DataBoundItem, DataRowView).Row
TextBox1.Text = drw("Col1").ToString
TextBox2.Text = drw("Col2").ToString
TextBox3.Text = drw("Col3").ToString
TextBox4.Text = drw("Col4").ToString
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
'copy data from textboxes to grid
If mintRowWeAreEditing = -1 Then Exit Sub 'haven't clicked Edit button yet
Dim drw As DataRow = DirectCast(DataGridView1.Rows(mintRowWeAreEditing).DataBoundItem, DataRowView).Row
drw("Col1") = TextBox1.Text
drw("Col2") = TextBox2.Text
drw("Col3") = TextBox3.Text
drw("Col4") = TextBox4.Text
End Sub
End Class
So, for example, you could do something like this on your button click event.
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Dim refNo As Integer = txtRefNo.Text ' Change DataType and TextBox name as appropriate
Dim firstName as String = txtFName.Text
' Repeat setting variables for each field in the row that you're updating
For Each dgr As DataGridViewRow in DataGridView1.Rows
If dgr.Item("RefNo") = refNo Then
dgr.Cells(0).Value = firstName 'Instead of using (0) you can use the column name
dgr.Cells(1).Value = newVar
End If
Next
' Commit the changes/refresh here
End Sub
I don't have the IDE to hand to test this but any problems let me know, I'll have a look into it.

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.

VB.net datagridview to chart control devexpress

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

Change editted cell in datagridview on enter (vb.net)

I have a datagridview table that is populated by using a datatable as a datasource. The datagridview has been set to edittable and I can change the values of cells but of course, the changes do not reflect in the original database table since the grid view is not directly bound. Is it possible to have the datagridview in such a way that when I press enter (when editting a cell), the focus shifts to the cell on the right (rather then selecting the row below)? This would need to keep on going until I reach the right most column, in which case the following edit cell would be the first cell in the following row.
Thanks in advance!
Try this:
define a flag flag_edited that will be raised when an edit occurs (CellEndEdit event)
define a function changeSelectionToNextCell that will undo default behavior's selection change (SelectionChanged event)
Here is a sample:
Private flag_edited As Boolean
Private Sub DataGridView1_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
flag_edited = True
End Sub
Private Sub DataGridView1_SelectionChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.SelectionChanged
changeSelectionToNextCell()
End Sub
Private Sub changeSelectionToNextCell()
If flag_edited Then
flag_edited = False
If DataGridView1.CurrentCell IsNot Nothing Then
Dim row_index As Integer = DataGridView1.CurrentCell.RowIndex - 1
Dim col_index As Integer = DataGridView1.CurrentCell.ColumnIndex + 1
If col_index = DataGridView1.ColumnCount Then
col_index = 0
row_index += 1
End If
DataGridView1.CurrentCell = DataGridView1.Rows(row_index).Cells(col_index)
End If
End If
End Sub