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

  • SEARCH
  • Home
  • 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 8126277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:05:58+00:00 2026-06-06T07:05:58+00:00

I’m writing a small application in C# (.NET 4.0). I have a datagridview, each

  • 0

I’m writing a small application in C# (.NET 4.0). I have a datagridview, each row represents one object. I want a combobox column that allows to choose a specific property of that object.

Example:

public class Car
{
   public String Make {get; set;}
   public BindingList<String> AllColors {get; set;}
   public int SelectedColorIndex {get; set;}
}

Each row represents one Car object. Each (different) car object has it’s own selection of possible colors (AllColors). I want to have a column where you can set SelectedColorIndex by choosing one of the colors from AllColors (AllColors is specific to each Car object).

Note: I made up this example but it describes what I want to accomplish.

How can I accomplish this? The only solution that I found was to have a specific combobox outside of the datagridview using which you can change the selected row. And in row enter event I changed the datasource of the bindingsource to the current “AllColors”.

Thank you for your time and answers.

  • 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-06-06T07:05:59+00:00Added an answer on June 6, 2026 at 7:05 am

    Here is the code behind of a working example where the colors available are filtered down to the colors provided in the bound object’s AllColors list.

    The magic happens in the CellBeginEdit and CellEndEdit event handlers – there we provide each combo box cell with the list from the bound row and then reset it on exit.

    One thing this relies upon is having a master list which contains all colours – there is no way around this.

    Also I’ve added handling for the case where a new row needs default values. All I do is set the selected index by default to one. Of course this wouldn’t work in the real world, you would need something a bit smarter! The DefaultValuedNeeded event is describe here on MSDN.

    public partial class Form1 : Form
    {
    
        private BindingSource cars;
        private BindingSource masterColors;
    
        public Form1()
        {
            InitializeComponent();
    
            masterColors = new BindingSource();
            masterColors.Add(new CarColor{Name = "Blue", Index = 1});
            masterColors.Add(new CarColor{Name = "Red", Index = 2});
            masterColors.Add(new CarColor { Name = "Green", Index = 3 });
            masterColors.Add(new CarColor { Name = "White", Index = 4 });
    
            BindingList<CarColor> fordColors = new BindingList<CarColor>();
            fordColors.Add(new CarColor{Name = "Blue", Index = 1});
            fordColors.Add(new CarColor{Name = "Red", Index = 2});
    
            BindingList<CarColor> toyotaColors = new BindingList<CarColor>();
            toyotaColors.Add(new CarColor { Name = "Green", Index = 3 });
            toyotaColors.Add(new CarColor { Name = "White", Index = 4 });
    
            cars = new BindingSource();
            cars.Add(new Car { Make = "Ford", SelectedColorIndex = 1, AllColors = fordColors });
            cars.Add(new Car { Make = "Toyota", SelectedColorIndex = 3, AllColors = toyotaColors });
    
            dataGridView1.DataSource = cars;
            dataGridView1.Columns["SelectedColorIndex"].Visible = false;
            //dataGridView1.Columns["AllColors"].Visible = false;
    
            DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
            col.Name = "AvailableColors";
            col.DataSource = masterColors;
            col.DisplayMember = "Name";
            col.DataPropertyName = "SelectedColorIndex";
            col.ValueMember = "Index";
            dataGridView1.Columns.Add(col);
    
            dataGridView1.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);
            dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
            dataGridView1.DefaultValuesNeeded += new DataGridViewRowEventHandler(dataGridView1_DefaultValuesNeeded);
        }
    
        void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns["AvailableColors"].Index)
            {
                if (e.RowIndex != dataGridView1.NewRowIndex)
                {
                    // Set the combobox cell datasource to the filtered BindingSource
                    DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1
                                    [e.ColumnIndex, e.RowIndex];
                    Car rowCar = dataGridView1.Rows[e.RowIndex].DataBoundItem as Car;
                    dgcb.DataSource = rowCar.AllColors;
                }
    
            }
        }
    
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns["AvailableColors"].Index)
            {
                // Reset combobox cell to the unfiltered BindingSource
                DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1
                                [e.ColumnIndex, e.RowIndex];
                dgcb.DataSource = masterColors; //unfiltered
            }
        }
    
        void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
            e.Row.Cells["SelectedColorIndex"].Value = 1;
        }
    
    }
    
    public class Car
    {
        public String Make { get; set; }
        public BindingList<CarColor> AllColors { get; set; }
        public int SelectedColorIndex { get; set; }
    }
    
    public class CarColor
    {
        public String Name { get; set; }
        public int Index { get; set; }
    }
    

    1 I first learned how to do this from the DataGridView FAQ, a great resource written by Mark Rideout, the program manager at the time for the DataGridView at Microsoft. The example in the FAQ filters based upon another combobox, and uses DataTables but the principle is the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
Thanks in advance for your help. I have a need within an application to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.