Solved

Change datagridview combobox column to listindex = -1

Posted on 2010-11-16
19
995 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:PSCTECH
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 8
19 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34150212
What is the datatype of the combobox column in datatable? Is it text (display member) or numeric(value member)?
0
 

Author Comment

by:PSCTECH
ID: 34150367
display member is text
value member is numeric
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34150608
Do you have 2 columns in DataTable for the combobox?
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

Author Comment

by:PSCTECH
ID: 34151233
yes
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34153442
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.
0
 

Author Comment

by:PSCTECH
ID: 34155231
it's bound to the value member (numeric)
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34155257
Yeah then try setting the value to -1 which would probably make the column empty.
0
 

Author Comment

by:PSCTECH
ID: 34155282
tried that it has no effect
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34155288
Can you show the code you are using to load and bind the grid?
0
 

Author Comment

by:PSCTECH
ID: 34155391
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

0
 

Author Comment

by:PSCTECH
ID: 34155422
Where it says "NAME_UIC" in the for loop, it should actually read "CITY_ID".  Sorry for the confusion
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34155430
0
 

Author Comment

by:PSCTECH
ID: 34155453
it's set to: colCity.DataPropertyName = "CITY_ID"
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34155611
Try setting it to City
0
 

Author Comment

by:PSCTECH
ID: 34155669
Invalid value error.   Doesn't the dataproperty name have to match the value member??
0
 

Accepted Solution

by:
PSCTECH earned 0 total points
ID: 34157308
I got it to work by setting the displaymember and valuemember but NOT setting the datapropertyname.  This left the combobox with nothing being displayed, but since the valuemember and displaymember are set, I can then choose the appropriate city from the combobox.  
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34158198
>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?
0
 

Author Comment

by:PSCTECH
ID: 34158285
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.  
0
 

Author Closing Comment

by:PSCTECH
ID: 34186392
solved in-house
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question