Set dropdownlist in gridview in rowediting event - vb.net

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!

Related

Combobox update databinding value

I have searched a lot on this topic and cant seem to find what I did wrong so apologies if this has already been answered before.
So I have a combo box with a datasource of Account Types
So I set the properties to
DataSource = 'dscAccountTypeList'
DisplayMember = Name
ValueMember = Id
Now I bind this combobox to my datatable via code
cmdAccountType.DataBindings.Add("SelectedValue, dtMaster, "AccountTypeId", True, DataSourceUpdateMode.OnPropertyChanged)
My problem is the AccountTypeId on dtMaster does not update even if I changed the selected item on the combobox. The above method works on textboxes which updates the value on the datatable once the text has been changed. Sure I can set value via Code on change of the combobox but I am wondering why it doesn't automatically update like the textboxes. I already tried clicking on other field to fire the change event but it didn't work. I would appreciate it if anyone can point me in the right direction. Thanks!
P.S. the selected item on the combo box changes once the value on the dtMaster changed. It just doesnt work on the opposite.
Set your DataSource after you set your DisplayMember and ValueMember, not before.
DisplayMember = Name
ValueMember = Id
DataSource = 'dscAccountTypeList'

VB.NET Bound ComboBox SelectedValue does not display

I bind a datatable to the combobox.DataSource on load. I then give the combobox a DisplayMember and a ValueMember (2 different columns out of the datatable). In the SelectedIndexChanged of the combobox I would like to use the SelectedValue property of the combobox, just to test I MsgBox(combobox.SelectedValue) and I get "Argument 'Prompt' cannot be converted to type 'String'." Why isn't it displaying the value? :(
OnLoad
cbCISoftware.DataSource = dbMaps.Tables("maps")
cbCISoftware.ValueMember = "id"
cbCISoftware.DisplayMember = "name"
SelectedIndexChanged of cbCISoftware
MsgBox(cbCISoftware.SelectedValue)
SelectedValue.ToString outputs
System.Data.DataRowView
I believe the issue is that you need to bind the table's DefaultView:
cbCISoftware.DataSource = dbMaps.Tables("maps").DefaultView
First of all you have to be sure to have selected DropDownList as DropDownStyle for the Combobox and that the binding is working.
Then you have to replace MsgBox(cbCISoftware.SelectedValue)
with MsgBox(cbCISoftware.SelectedValue.ToString)
Otherwise to obtain the result, MsgBox(cbCISoftware.Text) will work, but it not probably what you are looking for :-)
I can provide you with complete code to do the binding if you need it.

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

Changing styling of DataGridViewComboBoxCell from DropDownList to DropDown?

(This question is narrower in scope than this question I asked earlier.)
Say I have a DataGridViewComboBoxColumn, and want to switch the style of the ComboBox control between DropDownList and DropDown (mainly for the text field editing capability). I'd like to do this on a row-by-row basis (at the DataGridViewComboBoxCell level).
How would I do that programatically?
Or maybe a different way of asking: How do I access the ComboBox control given an object of type DataGridViewComboBoxCell?
(I'm in VB 2005.)
Thanks as always.
Not sure if you still need an answer, but i have a question that covers similar ground: Detect which column is showing an editing control in a datagridview
i use something like this line to get the combobox out of the DataGridView (DGV) in my DataGridView's CellValidating event:
Dim comboBox As DataGridViewComboBoxCell = DGV.Item(e.ColumnIndex, e.RowIndex)
later i use this line to get change the DropDownList ComboBoxCell to a DropDown:
cb.DropDownStyle = ComboBoxStyle.DropDown
For mine to work i had to make sure the 'cb' was of the ComboBox type, i dont remember if i was able to get it working well if the combobox was of the DataGridViewComboBoxCell type.

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.