Search Header Row and Go To The Specific Column That's Found - vb.net

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

Related

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.

Datagridview selections with vb.net

Help pls! I want to automatically select all the rows that has the same value in a particular cell of every rows when the user manually select or click on a particular row. am using vb.net. Thanks.
Although the question is not clear enough, you can try:
Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.RowIndex <> -1 Then
If DataGridView1.CurrentCell IsNot Nothing AndAlso DataGridView1.CurrentCell.Value IsNot Nothing Then
findOnGridview(DataGridView1, DataGridView1.CurrentCell.Value.ToString(), DataGridView1.CurrentCell.ColumnIndex)
End If
End If
End Sub
Private Sub findOnGridview(g As DataGridView, s As String, c As Integer)
Dim counter As Integer = 0
For i As Integer = 0 To g.Rows.Count - 1
If g.Rows(i).Cells(c).Value = s Then
g.Rows(i).Cells(c).Style.BackColor = Color.Yellow
Else
g.Rows(i).Cells(c).Style.BackColor = Color.White
End If
Next
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

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

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 !! :)