Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:07:31+00:00 2026-05-30T00:07:31+00:00

I have a datagridview which I’m using for data entry. I’ve done this before

  • 0

I have a datagridview which I’m using for data entry. I’ve done this before with all text columns, and it worked great. But now I want one of the columns to be a databound combobox so the user can select options. When I do this, the resulting gridview’s datasource ends up with empty rows (but the right quantity). What am I missing?

Here is code:

 DataGridViewComboBoxColumn cboCategory = new DataGridViewComboBoxColumn();
 {
     cboCategory.HeaderText = "Category";
     cboCategory.DataSource = downtimeCategories;
     cboCategory.DisplayMember = "Name";
     cboCategory.ValueMember = "CategoryID";
     cboCategory.DataPropertyName = "CategoryID";

     gridDowntime.Columns.Add(cboCategory);
 }

Then code to grab gridview’s datasource:

DataTable dt = (gridDowntime.DataSource as DataTable);

Everytime I get a table with the correct number of rows, but all the rows are empty (although they are long rows, the dataset visualizer has to scroll to show the entire cell). How can I find the error and correct?

EDIT: Is there some specific additional information I should provide here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:07:33+00:00Added an answer on May 30, 2026 at 12:07 am

    Here is a simple example project that I just cooked up.

    The key thing I think you are getting wrong is that the DataPropertyName property of the DataGridViewComboboxColumn needs to refer to the datasource of the DataGridView, not the column.

    public Form1()
    {
        InitializeComponent();
    
        DataTable dt = new DataTable("Customers");
        DataColumn dc;
    
        dc = new DataColumn();
        dc.DataType = typeof(int);
        dc.ColumnName = "CustomerID";
    
        dt.Columns.Add(dc);
        dt.Columns.Add(new DataColumn("LastName"));
        dt.Columns.Add(new DataColumn("FirstName"));
        // Concatenation of first and last names 
        dt.Columns.Add(new DataColumn("FullName"));
        dt.Columns.Add(new DataColumn("Address"));
        dt.Columns.Add(new DataColumn("City"));
        dt.Columns.Add(new DataColumn("State"));
        dt.Columns.Add(new DataColumn("Zip"));
        dt.Columns.Add(new DataColumn("Phone"));
    
        dc = new DataColumn();
        dc.DataType = typeof(DateTime);
        dc.ColumnName = "LastPurchaseDate";
        dt.Columns.Add(dc);
    
        dc = new DataColumn();
        dc.DataType = typeof(int);
        dc.ColumnName = "CustomerType";
        dt.Columns.Add(dc);
    
        // Populate the table 
        dt.Rows.Add(2, "Baggins", "Bilbo", "Baggins, Bilbo", "Bagshot Row #1", "Hobbiton", "SH", "00001", "555-2222", DateTime.Parse("24/9/2008"), 1);
        dt.Rows.Add(1, "Baggins", "Frodo", "Baggins, Frodo", "Bagshot Row #2", "Hobbiton", "SH", "00001", "555-1111", DateTime.Parse("14/9/2008"), 1);
        dt.Rows.Add(6, "Bolger", "Fatty", "Bolger, Fatty", "ProudFeet Creek", "Hobbiton", "SH", "00001", "555-1111", DateTime.Parse("14/9/2008"), 1); 
        dt.Rows.Add(4, "Elessar", "Aragorn", "Elessar, Aragorn", "Citadel", "Minas Tirith", "Gondor", "00000", "555-0000", DateTime.Parse("14/9/2008"), 4);
        dt.Rows.Add(5, "Evenstar", "Arwin", "Evenstar, Arwin", "Citadel", "Minas Tirith", "Gondor", "00000", "555-0001", DateTime.Parse("23/9/2008"), 4);
        dt.Rows.Add(3, "Greyhame", "Gandalf", "Grayhame, Gandalf", DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, 3);
    
        DataGridViewComboBoxColumn cboCategory = new DataGridViewComboBoxColumn();
    
        List<CustomerType> customerTypes = new List<CustomerType> { new CustomerType { Id = 1, Name = "Good" }, new CustomerType { Id = 4, Name = "Bad" }, new CustomerType { Id = 3, Name = "Ugly" } };
    
        cboCategory.HeaderText = "Customer Type";
        cboCategory.DataSource = customerTypes;
        cboCategory.DisplayMember = "Name";
        cboCategory.ValueMember = "Id";
        cboCategory.DataPropertyName = "CustomerType";
    
        dataGridView1.Columns.Add(cboCategory);
    
        dataGridView1.DataSource = dt;
    
    }
    

    Bit of fluff in there – grabbed that datatable code right off the interwebs. But the key part here is the setting of properties for the combobox column.

    My datasource is a list of customertype objects:

    public class CustomerType
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    

    So I need to set DisplayMember on the column to “Name” and ValueMember to “Id” since this references the columns datasource. However for the DataPropertyName was set the value to “CustomerType” which is the name of a column in the DataTable that we have bound to the DataGridView.


    So after a wee bit of discussion in chat it turns out that the above is true but that you also need to ensure that the types of your id columns match.

    The datatable used to provide data to the combobox column was generated from a sql query and had the id column of type in. Your backing datatable had the id column made like so:

    dt.Columns.Add(new DataColumn("Category")); 
    

    This defaults to a column of type string. Try instead something like:

    downtimeEntries.Columns.Add("Category", typeof(int));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is for winforms using c# I have a datagridview which lists the email
I have a DataGridView which I add data to programatically. I have the AutoSizeRowsMode
I have a datagridview where the users can select which subset of columns to
I have a DataGridView which has different rows and columns and it work perfectly
I'm using EntityFrameowrk 4 and WinForms. I have a DataGridView which shows 5 to
I have a DataGridView in which one column has data that the user needs
This is a Winform C# question. I have a customized datagridview which is bound
I have a datagridview which is bound to some data. One of the fields
I Loaded Data From Datatable to DatagridView which look like this: SerialNumber1 | Quantity
A relatively simple question. I have a datagridview, which all it does is displays

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.