FullColumnSelect not working in DataGridView - vb.net

Actually i'm trying to hightlight whole the column when a user click on a cell of the DataGridView.
The source is load dynamically from the user so initially DataGridView is null.
The issue is that if i change SelectionMode to FullColumnSelect or anyother SelectionMode options nothing change, the DataGridView will still have the SelectionMode as FullRowSelect even if i change it programmatically.
How can i solve that problem and hightlight entire column on user click?

Actually i had to set SortMode to NotSortable for each column of the DataGrid but only after i've called .DataSource
So the code is the following
Grid.DataSource = dt
For Each c In Grid.Columns
c.SortMode = DataGridViewColumnSortMode.NotSortable
Next
Grid.SelectionMode = DataGridViewSelectionMode.FullColumnSelect

Related

DataGridView: programmatically select/highlight entire column on button click

I want to programmatically select/highlight entire column on button click to let users know what they searched.
This is what currently happens on button click ("GO" button)
but this is what I need to happen.
I have tried these so far to no avail:
DataGridView2.SelectionMode = DataGridViewSelectionMode.FullColumnSelect
DataGridView2.Columns(2).Selected = True
Doing so gave me this error: DataGridView control's SelectionMode cannot be set to FullColumnSelect while it has a column with SortMode set to DataGridViewColumnSortMode.Automatic.
I also tried to simply select the whole column. No error, but it didn't work.
DataGridView2.Columns(2).Selected = True
Just set the Selected property of each cell in the column.
Found this thread. I modified the code like so:
Dim row As DataGridViewRow
For Each row In DataGridView2.Rows
row.Cells(1).Selected = True
Next
and placed it inside Private Sub Button1_Click
I noticed that the first cell is automatically selected. This solved that problem.

VB.NET - Focus first row and first cell of DataGridView

Good morning,
I have a form with two textbox and a DataGridView.
I'm used to fill my controls then i press Tab to switch to the next one.
However, when i switch from my textbox to my DataGridView, the selected cell is the second one of the first row => DTG.Rows(0).Cells(1).
I need to enter on the DTG.Rows(0).Cells(0) to fill my DTG without using my mouse.
I tried the code bellow :
Public Sub txtBoxTest_Leave() Handles txtBoxTest.Leave
DTG.Focus()
DTG.CurrentCell = DTG(0, 0)
DTG.BeginEdit(True)
End Sub
The cell seems to be selected, but it is not in editmode.
Modify the edit mode to EditOnEnter does not resolve my problem :
DTG.EditMode = DataGridViewEditMode.EditOnEnter
Can someone help me please ?
EDIT :
My datagridview cells are in ReadOnly = False.
Try this:
Dim cell as Windows.Forms.DataGridViewCell= dataGridView1.Rows(0).Cells(0)
DataGridView1.CurrentCell = cell
DataGridView1.BeginEdit(True)

Change a Cell into a Button in DataGridView

I have a databound DataGridView. I want the cells of the two columns to contain a button instead of a regular cell.
Edit: Solved, now I need to figure out how to disable the button when a Cell contains NULL.
To enable adding of Columns to a specified position to a DataBound Datagridview, we have to set the Datagridviews AutoGenerateColumns Property to False before adding the Column.
Without doing so, all Columns that will be added will be placed on the right most end of the Datagridview and it will affect the first Column too.
But remember that the Colummn.Index Property will be affected.
dgv_dt is a DataTable
dgv is the DataGridView
dgv.AutoGenerateColumns=True
dgv.DataSource=dgv_dt
dgv.ClearSelection
dgv.Columns(0).HeaderText="FirstName"
dgv.Columns(1).HeaderText="Company ID"
dgv.Columns(2).HeaderText="Recent Picture"
dgv.Columns(2).Visible=False
dgv.Columns(3).HeaderText="Address"
dgv.Columns(4).HeaderText="Alive"
dgv.AutoGenerateColumns=False
Dim btn As New DataGridViewButtonColumn()
btn.HeaderText = "Click Data"
btn.Text = "Click Here"
btn.Name = "btn"
dgv.Columns.Insert(2,btn)
The code above will display the Datagridview and its Column.Index like this:
dgv.Columns(1) First Name
dgv.Columns(2) Company ID
dgv.Columns(3) Recent Picture 'This column is hidden, if this Contains NULL, ButtonCell is enabled/clickable
dgv.Columns(0) ButtonCell
dgv.Columns(4) Address
dgv.Columns(5) Alive
The Columns Company ID and Recent Picture contains a BLOB or NULL, if its a BLOB, the cell will be an Enabled Button and a Disabled Button if its NULL.
Set existing column to hidden .Visible = false.
Then add DataGridViewButtonColumn and check value of CompanyID column
if BLOB then button column enabled
if NULL then button column disabled

Combobox Make First Row As Display

So my combobox is used to populate a datagridview when user clicks the customersID in the combobox it will popupate the grid. I have the code for populating but the problem is that they combobox does no reset itself. It still shows the current value that was Selected. I wanted to know it is posible to reset the combobox after selected index event. The code below is how i got my combobobox.
' after a command and connection was made
customerAdapt = New SqlDataAdapter(customerQuery,con)
customerSet = New DataSet
customerAdapt.Fill(customerSet)
customerCb.DataSource = adoAddtRs.tables(0)
customerCb.DisplayMember = "custID"
customerCb.ValueMember = "custID"
customerCb.Visible = False
customerCb.DropDownStyle = ComboBoxStyle.DropDownList
Try this piece of coding
customerCb.selectedindex = -1

Select first visible cell of new row in DataGridView

I'm trying to focus input and fire the editing event on each new row that I add to a DataGridView in my form.
This is the code I am trying to user to achieve this.
Private Sub grd_GoldAdders_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles grd_GoldAdders.RowsAdded
Dim grid As DataGridView = DirectCast(sender, DataGridView)
grid.ClearSelection()
If grid.Rows(e.RowIndex).Cells("grid_flag").FormattedValue = Constants.[New] Then
For Each cell As DataGridViewCell In grid.Rows(e.RowIndex).Cells
If Not cell.Visible Then Continue For
grid.CurrentCell = cell
grid.BeginEdit(False)
Exit For
Next
End If
End Sub
The "grid_flag" is a hidden cell which is used to store custom states for a row.
Prior to adding a row, this is what we see on the form:
This is what we see when we actually try and add a new row:
Notice that both the column 0,0 and the first visible column of the new row are selected, but the column 0,0 has the focus. I do not wish for 0,0 to either get selected or have the focus. I also see here that the row indicator is pointing at row 0 too...
This is how I would like to see things after clicking my Add button:
Does anyone know where I am going wrong with the code? I've searched SO for the most part of the day trying to solve this one.
Instead of using your DataGridView's RowAdded event to set the CurrentCell, add the following code wherever you're adding a new record to your DGV (in your Add button's Click event I assume):
''# Add the new record to your Data source/DGV.
For Each row As DataGridViewRow In grd_GoldAdders.Rows
If row.Cells("grid_flag").FormattedValue = Constants.[New] Then
grd_GoldAdders.CurrentCell = row.Cells("AssySiteColumn") ''# I'm calling the first column in your DGV 'AssySiteColumn'.
grd_GoldAdders.BeginEdit(False)
Exit For
End If
Next
This code simply loops through all the rows in your DGV and specifies as the CurrentCell the first cell in the first row with your Constants.[New] flag value.