Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > combobox does not dropdown in datagridview
 

combobox does not dropdown in datagridview

Hi,

I have a datagridview binded to a business object. in the Edit Columns dialog, I chose ColumnType = DataGridViewComboBoxColumn, and DisplyStyle = ComboBox,

in Items, typed 3 lines of text string,

the problem: when I run the app and click the comboBox, it does not dropdown.

I also tried DisplyStyle = DropDownButton, not working either.

Question:

1. what is wrong,

2.how to use the Edit Columns dialog to get it work.

thanks in advance

Belinda

the wizard generated the following codes:

this.run_track_code.DataPropertyName = "run_track_code";

this.run_track_code.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox;

this.run_track_code.HeaderText = "Track";

this.run_track_code.Items.AddRange(new object[] {

"track 0",

"track 1",

"track 2"});

this.run_track_code.Name = "run_track_code";

this.run_track_code.ReadOnly = true;

this.run_track_code.Resizable = System.Windows.Forms.DataGridViewTriState.True;

this.run_track_code.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;

this.run_track_code.Width = 50;

BLiu  Wednesday, July 25, 2007 6:09 PM

You have set the ReadOnly property to true, this is the cause of your problem. Set it to false.

Zhi-Xin Ye  Thursday, July 26, 2007 8:59 AM

You have set the ReadOnly property to true, this is the cause of your problem. Set it to false.

Zhi-Xin Ye  Thursday, July 26, 2007 8:59 AM

After I set to false, it sort of works but seem has another complicated issue.

behavier:

the first time the combobox get focus, nothing happens except highlight.

click on it again with the focus on, an exception happened as follow:

System.Threading.ThreadStateException: Current thread must be set to single thread apartment (STA)mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.

at System.Windows.Forms.ComboBox.set_AutoCompleteSource

at System.Windows.Forms.DataGridViewComboBoxCell.InitializeEditingControl

at System.Windows.Forms.DataGridView.InitializeEditingControlValue

click on it again with the focus on, it drops down and I can choose any item I want.

the above sequence happens everytime the combobox loses focus and gains focus.

background about my application:

a large legacy application developed in C, using Runtime library: Multi-threaded Debug DLL (/MDd), I developed a middle piece so C code can call C# form in a DLL, and also C# form can call C functions in the legacy application.

so far in the C#datagridview (using business object), I can use checkbox, icon, textbox, but currentlyhas beenstruggling onthe combobox for a few days

thanks

Belinda

BLiu  Thursday, July 26, 2007 4:24 PM

Try insert the statement:

Thread.CurrentThread.ApartmentState = System.Threading.ApartmentState.STA;

in Main right before the Application.Run statement, as

Code Snippet

STAThread]

void Main()

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

System.Threading.Thread.CurrentThread.ApartmentState =

System.Threading.ApartmentState.STA;

Application.Run(new Form1());

my main function is WinMain in C, and which is multi-threaded and I do not have the luxuary to change it at this moment.

I made a c# FormA as a class library, when I call it from a c# application, in static void Main(), the combo box in datagridview works without adding the line you mentioned.

static class Program

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new FormA());

}

}

but when I call it from .cpp managed code (which is the bridge between unmanaged legacy app to c# world), even I add the line, I still get the same error. (keep in mind the main app is a multi-tread)

System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState:Tongue TiedTA;

FormA^ form2 = gcnew FormA();

form2->ShowDialog();

thanks

Belinda Liu

BLiu  Friday, July 27, 2007 7:25 PM

You can use google to search for other answers

Custom Search

More Threads

• DatePicker in DataGridView
• delete datagridviewrows
• Multiple combobox bindings
• Copying tables in DataSet
• Binding Navigator Delete Item problem
• Binding a List with a Nested List to a DataGrid
• Help> Searching/.Find in DataGridView using Virtual Mode
• How to fix child form insted of moving out of screen
• how to populate a DGV cell with text from a textbox (C#)
• Delete Columns (DataGridViewTextBoxColumn) from DataSet after DataSet load