Prevent user from changing programmatical selection in DataGridView - vb.net

I have a DataGridView whose rows are selected/unselected based on filters set through other controls. This selection is ment to be only programmatical. How can I prevent user from changing these selections?

I thought of answering by telling you to keep DataGridView1.Enabled = False, which may work as per your need, but you might lose some visual effects(like greyed-out DataGridView).
Setting DataGridView1.ReadOnly = True also won't help.
So the below code is not a perfect solution but more like a workaround to get exactly what you want (tested now & completely working):
Dim SelectedRows As List(Of DataGridViewRow) = New List(Of DataGridViewRow)
Private Sub DataGridView1_MouseDown(sender As Object, e As MouseEventArgs) Handles DataGridView1.MouseDown
For Each Rows As DataGridViewRow In DataGridView1.SelectedRows
SelectedRows.Add(Rows) 'Add all the rows pre-defined by your code
Next
End Sub
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
DataGridView1.ClearSelection() 'clear the selection
For Each rs As DataGridViewRow In SelectedRows
rs.Selected = True 'restore the previous selected rows
Next
End Sub
Private Sub DataGridView1_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseClick
DataGridView1.ClearSelection() 'clear the selection
For Each rs As DataGridViewRow In SelectedRows
rs.Selected = True 'restore the previous selected rows
Next
End Sub

I'm not sure if it is possible (without customizing DataGridView) to keep it enabled (so to see selection) and avoid user to be allowed to select rows.
However, you can store your selection i.e. in a list(of int16) and handle events like CellClick and re-read and reset the selection from the list every time the user clicks inside DataGridView. Hopefully it won't be even noticeable to the user, but either way it will not allow him to modify the selection.

Related

Not allow selection of a certain column in a DataGridView control

I have this datagridview
I was wondering if there is a way to disable the abillity of the user to select cells of the first column (the one with an arrow), but i still need the user to be able to select all other cells,exept the ones on the first column.
If the selection is moved to the target column, using the DataGridView1.SelectionChanged event you can prevent the selection focus, setting the DataGridView1.CurrentCell to next column cell in the same row.
This works for selection events generated by both Mouse clicks and cursor movement.
Private blockedColumn As Integer = 0
Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
If DataGridView1.CurrentCell.ColumnIndex = blockedColumn Then
DataGridView1.CurrentCell =
DataGridView1(blockedColumn + 1, DataGridView1.CurrentCell.RowIndex)
End If
End Sub
You could set dataGridView1.Columns(0).Frozen = True anyway, for other uses. But it's not necessary.

Hide rows if column contains particular text/ string

I have a datagridview wich I have no bound datasource too I also have a button and three rows in my datagridview. If my column named STATUS contains the word CLOSED I would like to hide that entire row but i dont want to delete it just hide it.
If anyone woyuld like to know I am ussing VB.net
How can I do this?
If you are using a bound datasource you want to capture the DataGridView.DataSourceChanged event.
Would look like this.
Private Sub DataGridView1_DataSourceChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.DataSourceChanged
For Each row As DataGridViewRow In DirectCast(sender, DataGridView).Rows
If row.Cells("status").Value.ToString.ToLower.Contains("Closed") Then
row.Visible = False
End If
Next
End Sub
If you are not using a datasource you would want to capture the DataGridView.RowsAdded event.
Would look like this.
Private Sub DataGridView1_RowsAdded(sender As Object, e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
Dim dg As DataGridView = sender
If dg.Columns.Count > 0 And e.RowIndex <> 0 Then
Dim theRow As DataGridViewRow = dg.Rows(e.RowIndex)
If theRow.Cells("status").Value.ToString.ToLower.Contains("closed") Then
theRow.Visible = False
End If
End If
End Sub

Hide a row in DataGridView

I am a new user to vb.net and need to hide a row when a user right clicks on a contextmenu and selects hide. I have googled this but have yet to find a way to do it.
At the moment, when a user clicks on an entry in the grid, the value is entered into a text box which is fine. What I need to do is hide the entry the user right clicked on and hide the selection. As I am new I am finding it hard going to code this as I have just finished my first course which entailed the basics. Any help would be appreciated or if you need anymore code, then please ask.
Dim value As Object = UserDataGridView.Rows(e.RowIndex).Cells(0).Value
txtCustomerActive.Text = CType(value, String)
Private Sub HideToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles pnlContextMenuStrip1.ItemClicked
'Get the text of the item that was clicked on.
'Dim text As String = txtCustomerActive.Text
Try
'txtCustomerActive.Visible = False
pnlContextMenuStrip1.Visible = False
MessageBox.Show(txtCustomerActive.Text)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
You could use Rows.Item() to hide specific DataGridViewRow, like:
If (UserDataGridView.Rows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
End If
I am assuming you are using FullRowSelect here.
If you are not using FullRowSelect you could have this alternative code which could catch both Cell being Selected or Row being Selected:
If (UserDataGridView.SelectedRows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
ElseIf (UserDataGridView.SelectedCells.Count > 0) Then
For Each cell As DataGridViewTextBoxCell In UserDataGridView.SelectedCells
UserDataGridView.Rows.Item(cell.RowIndex).Visible = False
Next
End If
To Unhide everything let's say from a Button Click you could have this:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For Each row As DataGridViewRow In UserDataGridView.Rows
If (row.Visible = False) Then
UserDataGridView.Rows.Item(row.Index).Visible = True
End If
Next
End Sub
As far as I know, you can not make a server-side handler for right mouse click (as you did for HideToolStripMenuItem_Click, which works as part of .NET postback mechanism).
However, I believe that such feature could be done with some client-side javascript progamming.
Hope this helps!

DataGridView retrieve checked row

The thing is that I have DataGridView1, when I click on it I store some values and after clicking on *edit* button new window opens and I retrieve all info from that selected row cells.
But, when I turn off that window I set the code to automatically refresh my DGV1, what I need is after refreshing to retrieve that previously checked row and get it again so my client can easily continue working since they have like 10.000 rows so they don't want to search for next row everytime when they edit something.
I don't know how you are retrieving the current Row/Column indices, but I am writing a code triggered from the CellClick Event which you shouldn't find any problem to adapt to your specific requirements:
Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim selectedRow As Integer = e.RowIndex
Dim selectedCol As Integer = e.ColumnIndex
fillDataGridView()
DataGridView1.FirstDisplayedCell = DataGridView1(selectedCol, selectedRow)
End Sub
Where fillDataGridView() performs the refilling.

datagridview row selection; not updating to new selection

Almost completed my first VB.Net program. Works great, with one problem. My datagridview displays info on selection of a row. However, it does not consistently change when user selects a different row. I have been selecting a third row, then going back to second row to get updated info.
here's my code so far. Any suggestions to update upon new selection??
Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
Me.txtCJISCode.Text = DataGridView1.Item(5, i).Value
Me.txtShortDescript.Text = DataGridView1.Item(10, i).Value
Me.txtDescription.Text = DataGridView1.Item(23, i).Value
End Sub
Depending on the Selection mode of your datagrid, the CurrentRow might not be the selected one. Since you're doing it with the CellContentClick it would be easier to use the event args :
Replace
i = DataGridView1.CurrentRow.Index
by
i = e.RowIndex