Setting the focus in a datagridview in windows form - vb.net

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.

Related

Extend the properties of datagridview and its cell

I am working on the datagridview now days. And I have to assign some custom properties to the datagridview which I am able to do. problem comes when I want to extend the properties of the cell. for example I already have my custom textbox control which user can set the behaviour like if its numeric or alphanumeric, allow negative, allow decimals etc etc. which works fine. Now I want to include that textbox control in my extended grid. So user can set all those properties while adding columns.
First of all is it possible? If yes then any tutorial or help please.
thanks in advance.
I'm not fully certain so you may want to keep looking, but as far as I know you have to manually set each property of the text box to what you want. There isn't a copy all cell style settings to textbox call you can make.
Public Sub funwithDGVs()
Dim DataGridView1 As New DataGridView
Dim DataGridViewCell1 As DataGridViewCell
DataGridViewCell1.ForeColor = Color.Aquamarine
DataGridView1.Item(0, 1).Style.ForeColor = DataGridViewCell1.ForeColor
TextBox1.ForeColor = DataGridViewCell1.ForeColor
End Sub

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.

vb.net / DataGridView / ComboBoxCell?

I am using vb.net 2010 and winforms and DataGridView.
The DataGridView has a DataGridViewComboBox column. When I show the form with the DGV it shows this and empty grid but the column that contains the ComboBox shows the first item on the dropdown list.
How can I have the ComboBox display nothing until it is clicked on and selected?
Try setting the combobox selectedindex property to -1 when you initialize it. That might fix your problem, but when I do the same thing that you described, mine doesn't show any values in the combobox until I click on it. Here are the steps I took:
1. create a datagridview control.
2. right click on control and add column.
3. add DataGridViewComboBoxColumn
4. right click on control and edit columns.
5. Click on the button for "Items (Collection)".
6. Add some items
Now your control should behave how you are asking. It works fine when I run it. If it doesn't it may be a VS2010 bug since I'm running VS2008.
Edit:
When you add your items in code, just set the combobox value to Nothing:
Dim cboBrand As New DataGridViewComboBoxColumn
With cboBrand
.HeaderText = "Brand"
.Name = "Brand"
.Width = 300
.Items.Add("item1")
.Items.Add("item2")
.Items.Add("item3")
End With
Me.DataGridView1.Columns.Insert(0, cboBrand)
DataGridView1.Rows.Insert(0, New Object() {Nothing})
or if you want to set an initial value, do it like this:
DataGridView1.Rows.Insert(0, New Object() {"item2"})

Set dropdownlist in gridview in rowediting event

Trying to get the findcontrol to work so I can set the default value of the dropdownlist I have in a template field, but I'm having no luck. Am I doing something wrong?
Dim drdList As DropDownList
For Each row As GridViewRow In gridviewComputer.Rows
drdList = gridviewComputer.Rows(e.NewEditIndex).FindControl("statusDropDown")
Next
Found an easier way.
Go into the gridview template editing, then under the EditItemTemplate, where I had the dropdownlist declared I selected Edit DataBindings. Refresh the Schema, then I was able to set the SelectedValue property to the corresponding field of my gridviews datasource. (Which really just fills in "Bind("columnName")" in the Code expression.) Problem solved!

DataGridView - how to hide the "new" row?

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