Change editted cell in datagridview on enter (vb.net) - 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

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

Make Checkbox Column Checked when Row Selected in Datagridview VB.NET

Hello Everyone Good Afternoon.
I have a question,
I have Checkbox Column in Datagridview and the Column is in 0.
How can i make the Checkbox Column Checked when a Specific Row is Selected in Datagridview? Make the checkbox column checked when a row is Selected.
Here is my code
Code connected to another Code when populating Data from Database to Datagridview
Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = "Tag"
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
DataGridView1.Columns.Insert(0, checkBoxColumn)
Code when selecting row.
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.ColumnIndex = DataGridView1.Columns(0).Index Then
DataGridViewCheckBoxColumn_Uncheck()
Dim cell As DataGridViewCheckBoxCell = DataGridView1.Rows(e.RowIndex).Cells(0)
cell.Value = cell.TrueValue
End If
End Sub
Private Sub DataGridViewCheckBoxColumn_Uncheck()
For Each row As DataGridViewRow In DataGridView1.Rows
Dim cell As DataGridViewCheckBoxCell = row.Cells(0)
cell.Value = cell.FalseValue
Next
End Sub
My code has no error but the prob. here is I really need to Select the Checkbox Column and Checked on it and when the Row Selection Changed the Last Selected is Unchecked.
I hope you get me.
TYSM for future Help
Try to change your code to :
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim cell As DataGridViewCheckBoxCell = DataGridView1.Rows(e.RowIndex).Cells(0)
DataGridViewCheckBoxColumn_Uncheck()
cell.Value = True
End Sub
Private Sub DataGridViewCheckBoxColumn_Uncheck()
For Each row As DataGridViewRow In DataGridView1.Rows
Dim cell As DataGridViewCheckBoxCell = row.Cells(0)
cell.Value = False
Next
End Sub

vb.net DexExpress winform GridControl click column header select cell

I use GridControl with Gridview to display the data. I realized I click the column header is sorting the data instead of select all cell under the column header. May I know how to handle this? I have a sample code (below) but it use BandedGridview.
Private Sub bandedGridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles bandedGridView1.MouseDown
If Control.ModifierKeys <> (Keys.Shift Or Keys.Control) Then
Return
End If
Dim view As BandedGridView = CType(sender, BandedGridView)
Dim hInfo As BandedGridHitInfo = view.CalcHitInfo(e.Location)
If hInfo.InColumn Then
view.ClearSelection()
SelectCells(hInfo.Column)
ElseIf hInfo.InBandPanel AndAlso hInfo.Band IsNot Nothing Then
view.ClearSelection()
SelectCells(hInfo.Band)
Else
Return
End If
CType(e, DXMouseEventArgs).Handled = True
End Sub
Private Sub SelectCells(ByVal column As BandedGridColumn)
For i As Integer = 0 To column.View.RowCount - 1
column.View.SelectCell(i, column)
Next i
End Sub
Private Sub SelectCells(ByVal band As GridBand)
For Each column As BandedGridColumn In band.Columns
SelectCells(column)
Next column
End Sub
I need GridView only, anyone can help?
If you want to use this code for GridView then you can just remove the word Banded from everywhere and remove anything that belongs to GridBand. For SelectedCells method you need to convert column.View to GridView. Also I suggest you to add GridView.BeginSelection and GridView.EndSelection methods into SelectedCells method.
Here is example:
Imports DevExpress.Utils
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
'...
Private Sub gridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles gridView1.MouseDown
If Control.ModifierKeys <> (Keys.Shift Or Keys.Control) Then
Return
End If
Dim view As GridView = CType(sender, GridView)
Dim hInfo As GridHitInfo = view.CalcHitInfo(e.Location)
If hInfo.InColumn Then
view.ClearSelection()
SelectCells(hInfo.Column)
Else
Return
End If
CType(e, DXMouseEventArgs).Handled = True
End Sub
Private Sub SelectCells(ByVal column As GridColumn)
Dim view As GridView = CType(column.View, GridView)
view.BeginSelection()
For i As Integer = 0 To view.RowCount - 1
view.SelectCell(i, column)
Next i
view.EndSelection()
End Sub

How to use CellEndEdit event in datagridview using VB.Net?

In the data grid,I have two columns A and B.
I need only one column to be filled in a row.
In this,after filling column A,if i try to fill column B or vice-verse, I need the other column to be empty.
Note: I am filling the grid manually.
Please help me out of this and thanks in advance.
The DataGridViewCellEventArgs parameter you get from that event tells you which column and row you are currently editing, so you can easily wipe out the other column with that information:
Private Sub dgv_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) _
Handles dgv.CellEndEdit
If e.ColumnIndex = 0 Then
dgv.Rows(e.RowIndex).Cells(1).Value = String.Empty
ElseIf e.ColumnIndex = 1 Then
dgv.Rows(e.RowIndex).Cells(0).Value = String.Empty
End If
End Sub
To do this when the user starts to edit the cell, simply use the CellBeginEdit event instead:
Private Sub dgv_CellBeginEdit(sender As Object, _
e As DataGridViewCellCancelEventArgs) _
Handles dgv.CellBeginEdit
If e.ColumnIndex = 0 Then
dgv.Rows(e.RowIndex).Cells(1).Value = String.Empty
ElseIf e.ColumnIndex = 1 Then
dgv.Rows(e.RowIndex).Cells(0).Value = String.Empty
End If
End Sub

checkbox from datagridview to textbox

A new issue !!
I have a checkbox column in the datagridview and also a column named "partqty" which displays the quantity of materails available.. I would want that when the user checks on the checkbox, the quantities of checked rows should be added and displayed in the textbox..
I tired something like this.. But it displays 0(zero) in the textbox.. Please help..
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'For Each _rw As DataGridViewRow In dataGridView1.Rows
' If _rw.Cells(0).Value = True Then
Dim totalSum As Integer
For i As Integer = 0 To HemDatabase1DataSet5.Tables(0).Rows.Count - 1
totalSum += HemDatabase1DataSet5.Tables(0).Rows(i).Item("partqty")
Next
textBox5.Text = totalSum.ToString()
' End If
'Next
End Sub
Hey !! This is something i could think upon trying !! however, there is no error during compile time, but there is an error at runtime, which says :
Operator '+' is not defined for type 'DBNull' and type 'Boolean'
Here is the code i tried :
For Each _rw As DataGridViewRow In dataGridView1.Rows
If _rw.Cells(0).Value = True Then
Dim totalSum As Integer
For i As Integer = 0 To dataGridView1.Rows.Count - 1
totalSum += dataGridView1.Rows(i).Cells(5).Value
Next
textBox5.Text = totalSum.ToString()
End If
Next
It gives error on this line :
totalSum += dataGridView1.Rows(i).Cells(5).Value
You first problem is that you are trying to add apples and oranges together.
In the line:
totalSum += dataGridView1.Rows(i).Cells(5).Value
totalSum is an integer while the Value property of a DataGridView cell is of type object.
This is a similar problem to what you see when trying to use the DataTable except there your Item property is givin you a DBNull rather than an object back.
Here is the code that you want (I'm mainly a C# dev so the VB.Net might be lacking elegance but it works).
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim sum As Integer
sum = 0
For Each row As DataGridViewRow In DataGridView1.Rows
Dim current As Integer
If Integer.TryParse(row.Cells("OrderQty").Value.ToString, current) Then
sum += current
End If
Next
TextBox1.Text = sum.ToString
End Sub
Some things worth noting about that code:
I use a For Each rather than a for. The two perform basically identically but I find the foreach more readable
I use Integer.TryParse on the cell value before using it. This is safer than just casting the cell value directly.
Rather then using column indexes which is a little fragile I use column names.
With your check on whether or not the checkbox is checked you can do something similar:
Boolean.TryParse(row.Cells("IsChecked").Value.ToString, cb)
One final point to note is that if you try to execute this code in the cellclick method of the DataGridView you will run into problems - the checkbox state is not committed until after the cellclick, so the most recently checked cell will not get added to your count. Instead you will want to handle the CellDirtyStateChanged and commit the edits, then doing the sum in the CellValueChanged handler.
To illustrate that try this code:
Private Sub dgv_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim sum As Integer
sum = 0
Dim cb As Boolean
cb = False
If DataGridView1.Columns(e.ColumnIndex).Name <> "IsChecked" Then
Return
End If
For Each row As DataGridViewRow In DataGridView1.Rows
If Boolean.TryParse(row.Cells("IsChecked").Value.ToString, cb) Then
If cb Then
Dim current As Integer
If Integer.TryParse(row.Cells("OrderQty").Value.ToString, current) Then
sum += current
End If
End If
End If
Next
TextBox1.Text = sum.ToString
End Sub
The sum will always lag one click behind (actually in VB.Net it appears to just nor really work - but I'm more a c# dev so that could be me missing something).
Instead you want:
Sub dataGridView1_CurrentCellDirtyStateChanged( _
ByVal sender As Object, ByVal e As EventArgs) _
Handles DataGridView1.CurrentCellDirtyStateChanged
If DataGridView1.IsCurrentCellDirty Then
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
' If a check box cell is clicked, this event handler disables
' or enables the button in the same row as the clicked cell.
Public Sub dataGridView1_CellValueChanged(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellValueChanged
If DataGridView1.Columns(e.ColumnIndex).Name = "IsChecked" Then
Dim sum As Integer
sum = 0
Dim cb As Boolean
For Each row As DataGridViewRow In DataGridView1.Rows
If Boolean.TryParse(row.Cells("IsChecked").Value.ToString, cb) Then
If cb Then
Dim current As Integer
If Integer.TryParse(row.Cells("OrderQty").Value.ToString, current) Then
sum += current
End If
End If
End If
Next
TextBox1.Text = sum.ToString
End If
End Sub
This is the code that worked..
Dim iSselected As Boolean
Dim CurrentCellValue As String
Dim CumulativeSummer As Integer
Dim i As Integer
With Me.dataGridView1
For i = 0 To .RowCount - 1
iSselected = .Rows(i).Cells(0).Value
CurrentCellValue = .Rows(i).Cells(5).Value
If CurrentCellValue = "" Then CurrentCellValue = 0
If iSselected = True Then
CumulativeSummer += Convert.ToInt32(CurrentCellValue)
End If
Next
End With
Me.textBox5.Text = CumulativeSummer
Thank u for your help !! :)