Results 1 to 2 of 2

Thread: Combo boxes with DataGridView

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Question 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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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
  •  



Click Here to Expand Forum to Full Width