|
-
Jul 22nd, 2011, 08:37 AM
#1
Thread Starter
Hyperactive Member
Combo boxes with DataGridView
Okay, I have a DataGridView in my winform app, and it loads the schema and data as follows:
Code:
DataSet dsClasses;
SqlDataAdapter daClasses;
// Load the Classes table in the adapter
dsClasses = new DataSet("Classes");
daClasses = new SqlDataAdapter("Select ClassID, StudentID From Classes ", Connection);
daClasses.FillSchema(dsClasses, SchemaType.Source, "Classes");
daClasses.Fill(dsClasses, "Classes");
dgvMain.DataSource = dsClasses.Tables["Classes"];
This sets up the DataGridView perfectly, except that the class and student display and have to be entered by number, i.e. their respective ID values. (It's just a basic intersection table, showing which students are enrolled in which courses.) What I WANT are combobox controls that bind to the ID fields in the Classes table, but DISPLAY the corresponding names from the student and course tables. Haven't yet determined how to do that, and would appreciate any help.
I use VS 2010 with .NET 4 under Windows 7. Thanks in advance for any help.
-
Jul 24th, 2011, 10:46 PM
#2
Re: Combo boxes with DataGridView
First up, there's no point calling FillSchema and Fill. You call FillSchema if you want to build the schema in the DataTable without actually getting any data. You do want data, so the FillSchema call is pointless. Fill will create the same schema before populating the DataTable with data.
As for the question, follow the CodeBank link in my signature and check out my thread on this exact topic: ComboBox columns in a DataGridView. Note that such columns cannot be created automatically. You have no choice but to set them up yourself, either in code or, more sensibly, in the designer.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|