Avatar of PSCTECH
PSCTECH
 asked on

Change datagridview combobox column to listindex = -1

I have a form with a datagridview populated from a data table.  1 of the columns is a combobox.
Let's say for example there are only 2 columns (the first is a textbox that displays "Name" and the second is a combobox which displays "City").  Right now it shows:

Joe Johnson     Miami
Bob Smith         Boston

My question is how can I set the "City" combobox to listindex -1 after the grid loads so that the city is blank and the user is forced to make a selection.   Again, this is a bound dgv
Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
PSCTECH

8/22/2022 - Mon
Nasir Razzaq

What is the datatype of the combobox column in datatable? Is it text (display member) or numeric(value member)?
PSCTECH

ASKER
display member is text
value member is numeric
Nasir Razzaq

Do you have 2 columns in DataTable for the combobox?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
PSCTECH

ASKER
yes
Nasir Razzaq

Which one do you bind it to? The reason I am asking this is that if you are binding it to the value member then try setting the value member to -1.
PSCTECH

ASKER
it's bound to the value member (numeric)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Nasir Razzaq

Yeah then try setting the value to -1 which would probably make the column empty.
PSCTECH

ASKER
tried that it has no effect
Nasir Razzaq

Can you show the code you are using to load and bind the grid?
Your help has saved me hundreds of hours of internet surfing.
fblack61
PSCTECH

ASKER
here's a stripped down version
Private Sub frmInvRelocate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim objBllSite As New BLLCity
Dim dsCity As DataSet = objBllSite.GetAllSites()

Dim colCity As New DataGridViewComboBoxColumn

'fill grid from dataset to set dg structure
dgInvRelocate.DataSource = objBllInv.GetInvRelocate(strWhere).Tables(0)
        dgInvRelocate.AutoResizeColumns()

'fill combobox list from city datasource
colName_UIC.DisplayMember = "CITY"
colName_UIC.ValueMember = "CITY_ID"
colName_UIC.DataSource = dsSiteCol.Tables(0).DefaultView

dgInvRelocate.Columns.Add(colCity)

End Sub



Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

dgInvRelocate.DataSource = objBllInv.GetInvRelocate(strWhere).Tables(0)  

'refresh grid and reset comboboxes
For x = 0 To dgInvRelocate.Rows.Count - 1
   dgInvRelocate.Rows(x).Cells("NAME_UIC").Value = -1
Next
End Sub

Open in new window

PSCTECH

ASKER
Where it says "NAME_UIC" in the for loop, it should actually read "CITY_ID".  Sorry for the confusion
Nasir Razzaq

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
PSCTECH

ASKER
it's set to: colCity.DataPropertyName = "CITY_ID"
Nasir Razzaq

Try setting it to City
PSCTECH

ASKER
Invalid value error.   Doesn't the dataproperty name have to match the value member??
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
PSCTECH

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Nasir Razzaq

>I can then choose the appropriate city from the combobox.  
But that selection would not be saved to the datatable. How would you save then?
PSCTECH

ASKER
we are not saving the datatable.  we are, on a row by row basis (if changes were made) saving directly from the datagridview to the database.  
PSCTECH

ASKER
solved in-house
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.