DataGridView - how to hide the "new" row? - vb.net

My DataGridView is read-only - the user cannot enter data into it, so that empty row at the end (the "new" row?) just looks ugly.
How can I prevent it from displaying?

On the form designer view, click the little arrow to the right on the DataGridView. Adjust the value for Enable Adding. That will remove the row at the bottom of your grid.
Programmatically, you can adjust the AllowUserToAddRows property.
myGrid.AllowUserToAddRows = False

The DataGridView has a boolean property AllowUserToAddRows. Set this to false and you should no longer see the empty row at the end of the grid.

Just a clarification for other people who are looking at this for WPF the datagrid property has been renamed to CanUserAddRows

Related

Button doesn't work after DataGridView Validating Event

I have a Form with a TabControl on it. On one of the TabPages is a DataGridView with a New- and Delete-Button below it. I also have a Validating-Event for the DataGridView. Inside the Event I do some checking. If the conditions are not met I do a simple:
e.Cancel = True
Return
But after that my Delete-Button isn't working anymore. Even if I set a Breakpoint right at the start of the Click-Event nothing is happening what could that be? I have set the CausesValidation to false on the Button.
Update
I need to handle both Events, because the Sum of this DataGridView is compared to another value. The Sum can't be smaller or bigger than the other Value. But the User can enter what he wants as long as at the End the Sum is correct. I can't do that in the CellValidating because as soon as I change a Value the Sum doesn't match anymore
Update
I have observerd the following Scenarios where it doesn't work:
I change Data in the DataGridView and then trigger the Validating-Event. It starts working again when I input correct Data and switch Tabs
I create a new Row, trigger the CellValidating-Event and after that the DGV Validating-Event

Combo box's last value stays activated even if .removeitem has removed it

I have a situation where combobox's last value stays in the screen even if it has been removed via VBA.
I was using the method
cboBox.Removeitem "Drafter"
but here's what I would see
i've try cboBox.Requery afterwards, but no luck.
any help would be appreciated. thanks.
UPDATE:
I tried changing the combo box's source type from Value list to Table/Query, and just .requery after the values are updated. The same problem presists. Access has seemed to set "Drafter" as the default value now.
The Combo Box retains its .Value even after the underlying Item has been removed from the list. That's why it still shows up in the (top) text box portion of the control even after it has been removed from the drop-down list portion. You could try this:
Dim itemToRemove As String
itemToRemove = "Drafter"
Me.cboBox.RemoveItem itemToRemove
If Me.cboBox.Value = itemToRemove Then
Me.cboBox.Value = Null
End If

vb datagridview columntype change on edit

is there a method of setting up my datagridview to show me textboxcolumns until editmode is entered? At which time I will swap some textboxcolumns for bound comboboxcolumns. And at the end of validation/exit edit mode I can swap back?
If there's a method of doing this please share some links to examples.
I wouldn't try to switch DataGridView ColumnTypes like that. Sounds like a bad time.
Is your goal to have a DataGridViewComboBoxColumn that does not display the ComboBox dropdown button when it's not being edited? If so, there are two options:
Set the column's DataGridViewComboBoxColumn.DisplayStyleForCurrentCellOnly property to True.
Create your own DGV column based on the ComboBox by extending DataGridViewTextBoxCell. MSDN has a great article for doing this with the Calendar control here.
Of course you can. In the properties of the datagridview you can drill down to the column proprieties and switch the datatype to combo box. Very straight forward and easy.

Duplicate a DataGridView on a second windows form

I have a bound DGV that took a bit of work to get its columns set up. I'd like to show a 1-row version of this identical DGV on a second windows form. Is there a way to programatically place a copy on the second form. I would adjust the height and position of the 1-row version, and create a new binding source on the second form so that I could filter the data.
MyForm.Controls.Add(myDataGridView)
So further explanation:
In your first for you will need to make a variable or property that contains a reference to the DataGridView that you want to access.
I'd suggest doing something like this.
Public Shared Property myDataGridView As DataGridView
then after you get it set up in the form the way you want it set up
myDataGridView = originalDataGridView
Then in the second form
SecondForm.Controls.Add(FirstForm.myDataGridView)
Will add the DataGridView exactly as it is on the first form.
Edit
If you are creating it in a designer, you can just either copy and past it from the original for to the second form.
Or just on the Form.Shown or in the New() of the first form set the myDataGridView to the DataGridView that you created.

Setting the focus in a datagridview in windows form

I have a datagridview that accepts a list(of myObject) as a datasource. I want to add a new row to the datagrid to add to the database. I get this done by getting the list... adding a blank myObject to the list and then reseting the datasource. I now want to set the focus to the second cell in the new row.
To CLARIFY i am trying to set the focus
You can set the focus to a specific cell in a row but only if the SelectionMode on the DataGridView is set to CellSelect. If it is, simply do the following:
dataGridView.Rows[rowNumber].Cells[columnNumber].Selected = true;
In WinForms, you should be able to set the
Me.dataEvidence.SelectedRows
property to the row you want selected.
In Visual Studio 2012 (vb.NET Framework 4.50), you can set the focus on any desired cell of a DataGridView control.
Try This:
Sub Whatever()
' all above code
DataGridView1.Focus()
DataGridView1.CurrentCell = DataGridView1.Rows(x).Cells(y) 'x is your desired row number, y is your desired column number
' all below code
End Sub
Okay, that works for me. I hope that it works for you, too.