How to remove a row from datagridview and reset a particular column (vb.net 2005) - vb.net

I am using VS 2005 edition. I am developing a winform desktop application. Let's say I have a unbound datagridview, with table like below:
Original datagrid before delete:
If I removed Stuff D (Item No 5), the "Item No" column supposed to reset itself accordingly. The expected output should be:
After delete row:
The "Item No" column is not an autonumber, it's just the number I assigned incrementally as the user add in a new row(new Stuff). I tried using the following code in rowremoved event but failed to achieve the expected output. Please help. Thanks.

If the goal is just showing the record index in cell, then you don't need to assign a value to cell and it's enough to set e.Value = e.RowIndex + 1 in CellFormatting event.
But based on your comment it seems you need those cells have values as well. So you need to handle RowsRemoved and RowsAdded event of DataGridView and refresh the cell value with row number:
Public Sub RefreshRowNumbers(g As DataGridView)
For Each r As DataGridViewRow In g.Rows
r.Cells(1).Value = r.Index + 1
Next
End Sub
Private Sub DataGridView1_RowsRemoved(sender As Object, _
e As DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
RefreshRowNumbers(DirectCast(sender, DataGridView))
End Sub
Private Sub DataGridView1_RowsAdded(sender As Object, _
e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
RefreshRowNumbers(DirectCast(sender, DataGridView))
End Sub

Related

How to update a Text cell using a ComboBox cell VB.NET DatagridView

I am using Friend WithEvents PersonName As DataGridViewComboBoxColumn as a column in DataGridView and PersonName DataSoucre is bounded to a database. I would like for when the ComboBox Selection is changed it uses that value to query my database Business Object to find the right value then I want to set the value from the query to the column to the right of the one with the ComboBox which is named Colum3.
But am not use how to access each individual event handler for the ComboBox cell, I tried dgvTable.CellBeginEdit but it triggers too early.
Private Sub dgvTable_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles dgvTable.CellBeginEdit
If e.RowIndex <> -1 And e.ColumnIndex <> -1 Then
dgvTable.Rows(e.RowIndex).Cells("Colum3").Value = dgvTable.Rows(e.RowIndex).Cells(e.ColumnIndex).Value & "Somevalue from my databse"
End If
End Sub

How do I set a Combox with a default value when the Drop Down Style is set to "Dropdownlist

I am using Visual Studio 2019, and I've been trying to get my combo box to set a default value using a record from a Database. I have the Drop Down Style set to "Drop Down List" and this is the code I am Using.
First I am loading the listbox from the state data table into the combo box
Private Sub frmABS3_Shown(sender As Object, e As EventArgs) Handles Me.Shown
modABS.clsABS.ExecQuery("SELECT * FROM tblABSStates")
If Not String.IsNullOrEmpty(modABS.clsABS.Exception) Then MsgBox(modABS.clsABS.Exception) : Exit Sub
For Each r As DataRow In modABS.clsABS.DBDT.Rows
cboState.Items.Add(r("StateAbbrev"))
Next
End Sub
Next I am populating the fields on the Form. This is the part I am having trouble.
Private Sub GetRecord()
' Fail if No Records Found or position is out of Range
If modABS.clsABS.DBDT.Rows.Count < 1 OrElse CurrentRecord > modABS.clsABS.DBDT.Rows.Count - 1 Then Exit Sub
'Return first user found
Dim r As DataRow = modABS.clsABS.DBDT.Rows(CurrentRecord)
'Populate Fields
cboState.Text = r("StateAbbrev").ToString
txtCity.Text = r("City").ToString
End Sub
When I run the code the combo box doesn't show the state abbrev and the item isn't selected on the list. It works the way its suppose to when I reset the Drop Down Style set to "Drop Down", however I don't want the user to be able to add a value that is not in the combo listbox.
This code needed to be in the form load event not the form shown event:
Private Sub frmABS3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
modABS.clsABS.ExecQuery("SELECT * FROM tblABSStates")
If Not String.IsNullOrEmpty(modABS.clsABS.Exception) Then MsgBox(modABS.clsABS.Exception) : Exit Sub
For Each r As DataRow In modABS.clsABS.DBDT.Rows
cboState.Items.Add(r("StateAbbrev"))
Next
txtZipCode.Text = modABS.clsABS.ZipCode
End Sub

vb.net datagrid to datagridview winforms

We work with vb.net - Visual Studio 2013 (WinForms)
We are changing datagrids to datagridviews in our code.
At the moment I'm trying to convert a column that was described in the datagrid as follows:
Dim grdcolstyle5 As New PTSoft.FrameWork.DataBrowserTextColumnColorDecimal
With grdcolstyle5
.HeaderText = "text"
.MappingName = "vrd_199"
.Width = 80
.FormatString = "###,###,##0.00"
.[Operator] = "<"
.ParameterCol = 2
.ForeBrush = New SolidBrush(Color.Red)
End With
What is does is compare the value of one column with another and color the text when it's smaller (in this case).
I know how to do it in a loop after the grid has been filled, but this slows things somewhat down.
Does anyone know how this can be done "on the fly", on the moment the row is filled?
In short I am looking for the equivalent for the datagridview...
You can use RowPrePaint event, where you have large number of rows to paint.
Private Sub dataGridView1_RowPrePaint(ByVal sender As Object, ByVal e As DataGridViewRowPrePaintEventArgs)
If Convert.ToInt32(dataGridView1.Rows(e.RowIndex).Cells(2).Text) < Convert.ToInt32(dataGridView1.Rows(e.RowIndex).Cells(5).Text) Then
dataGridView1.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Red
End If
End Sub
The above code is comparing Cells(2) against Cells(5). You can customize that as per your need.
Another option is using CellFormattingEvent
Private Sub dataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs)
For Each Myrow As DataGridViewRow In dataGridView1.Rows
If Convert.ToInt32(Myrow.Cells(2).Value) < Convert.ToInt32(Myrow.Cells(1).Value) Then
Myrow.DefaultCellStyle.ForeColor = Color.Red
End If
Next
End Sub

One row selector in all Datagridview

Based on what you see below is the sample of a datagridview that an item has been selected.
Here is my question what if I have a more than 1 Datagridview? I mean 5 Datagridview like this.
All of them contains 1 column only. Based on the 1st image, the row selector or the blue one select an item.
My question is how can I make all of the datagridview only have one row selector?
What happens is when i selected each of them all of it has a row selected 5 selections.
How can I make 1 row selector for all of them.
Thinking of changing the Selection Color but I think that's not applicable.
TYSM for future help.
If you're looking for an alternative, you can also try this approach:
Private Sub DataGridView_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles _
DataGridView1.CellEnter, DataGridView2.CellEnter, DataGridView3.CellEnter, DataGridView4.CellEnter, DataGridView5.CellEnter
Dim MyDataGrids() As DataGridView = {DataGridView1, DataGridView2, DataGridView3, DataGridView4, DataGridView5}
For i = 0 To MyDataGrids.Count - 1
If MyDataGrids(i).Name = sender.Name Then
Continue For
Else
MyDataGrids(i).ClearSelection()
End If
Next
End Sub
MyDataGrids() is an array of DataGridViews. If for example, the controls you need to check increases, just add the name of the DataGridView in this array and it will be included in the checking and clearing of selections. Don't also forget the Handles event. As you can see here, all of the five grids .CellEnter event are included so you don't have to copy-paste it to five separate events.
Try this maybe it is more easy to edit if you add more grid
Private Sub ClearSelectedCells(ByVal Identifier As Integer)
If Identifier = 1 Then 'for datagrid 1
dg2.ClearSelection()
dg3.ClearSelection()
ElseIf Identifier = 2 Then 'for datagrid 2
dg1.ClearSelection()
dg3.ClearSelection()
'and so on
.
.
End If
End Sub
Private Sub dg1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dg1.CellClick
ClearSelectedCells(1)
End Sub
'and other gridcellclick
.
.

Silverlight Datagrid New Row Goes to Bottom and Screws Up Multi-Delete Logic

I have a Silverlight 4 application I've been tasked with making some changes to. I'd rather redo it in something supported, but that's not an option at the moment. I have a datagrid and I've added a button to insert a new row into the datagrid and database table using the values from two comboboxes:
Private Sub btnAddAccountGroupLink_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles btnAddAccountGroupLink.Click
Dim sv_ag As Com_AccountGroup = cbACCT_GROUP.SelectedValue
Dim sv_cf As CS_FUND = cbACCT_CD.SelectedValue
AccountGroupLinkData.DataView.Add(New Com_AccountGroupLink() With {.ACCT_GROUP = sv_ag.ACCT_GROUP, .ACCT_CD = sv_cf.ACCT_CD})
AccountGroupLinkData.SubmitChanges()
End Sub
When the row is added, it gets added to the bottom of the datagrid instead of it's properly sorted location. This also breaks my code that deletes selected indexes from the datagrid:
Private Sub btnRemoveSelected_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnRemoveSelected.Click
If MessageBox.Show("Are you sure you want to delete the selected records?", "Confirmation", MessageBoxButton.OKCancel) = MessageBoxResult.OK Then
Dim indexesToDelete As New List(Of Integer)
For i As Integer = 0 To AccountGroupLinkData.DataView.Count - 1
If AccountGroupLinkData.DataView(i).IsSelected Then
indexesToDelete.Add(i)
End If
Next i
For d As Integer = 0 To indexesToDelete.Count - 1
AccountGroupLinkData.DataView.RemoveAt(0)
Next d
AccountGroupLinkData.SubmitChanges()
End If
End Sub
So if I delete the bottom row, it actually deletes the row that's located where this row should be.
How can I force the datagrid to update or show the row in its correct position?
I solved this by adding this code after the SubmitChanges:
NavigationService.Refresh()
This refreshes the Silverlight page and the row pops up to the proper position.