Click here to Skip to main content
15,744,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi in my C# windows form project, My datagridview have one ComboBox column. this combobox items read form database and each Row Combobox have different Items.
But all of the Row combobox always have same items. how can fix this?
please help.
my current code is:

What I have tried:

private void dataGridFill(IEnumerable<object> list)
            string address = "";
            List<String> categoryName = new List<string>();
            dataGridView1.DataSource = null;
            dataGridView1.RowTemplate.Height = 100;
            BLLCategory bLL = new BLLCategory();

            //id Column
            DataGridViewTextBoxColumn idColumn = new DataGridViewTextBoxColumn();
            idColumn.HeaderText = "کد";
            idColumn.Name = "id";
            idColumn.Visible = false;

            // image Column
            DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
            imageColumn.HeaderText = "تصویر";
            imageColumn.Name = "image";
            ((DataGridViewImageColumn)dataGridView1.Columns["image"]).ImageLayout = DataGridViewImageCellLayout.Stretch;
            dataGridView1.Columns["image"].Width = 100;
            //title Column
            DataGridViewTextBoxColumn titleColumn = new DataGridViewTextBoxColumn();
            titleColumn.HeaderText = "عنوان";
            titleColumn.Name = "title";
            dataGridView1.Columns["title"].Width = 100;

            //address Column
            DataGridViewTextBoxColumn addressColumn = new DataGridViewTextBoxColumn();
            addressColumn.HeaderText = "آدرس";
            addressColumn.Name = "Address";
            addressColumn.Visible = false;

            // category coulmn
            DataGridViewComboBoxColumn comboBoxColumn = new DataGridViewComboBoxColumn();
            comboBoxColumn.Name = "Category";
            comboBoxColumn.HeaderText = "دسته‌بندی‌ها";

            foreach (var item in list)
                int index = dataGridView1.Rows.Add();

                dataGridView1.Rows[index].Cells["id"].Value = item.GetType().GetProperty("id").GetValue(item, null).ToString();
                dataGridView1.Rows[index].Cells["image"].Value = (byte[])item.GetType().GetProperty("image").GetValue(item, null);
                dataGridView1.Rows[index].Cells["title"].Value = item.GetType().GetProperty("title").GetValue(item, null);

                address = item.GetType().GetProperty("address").GetValue(item, null).ToString();
                    dataGridView1.Rows[index].Cells["Address"].Value = address;

                foreach (var cat in bLL.GetCategoryName((Guid)item.GetType().GetProperty("id").GetValue(item, null)))
                    categoryName.Add((string)cat.GetType().GetProperty("name").GetValue(cat, null));
                    //dataGridViewX1.Rows[index].Cells["Category"].Value = (string)cat.GetType().GetProperty("name").GetValue(cat, null);
                DataGridViewComboBoxCell comboCell = dataGridView1["Category", index] as DataGridViewComboBoxCell; 
                comboCell.DataSource = new BindingSource(categoryName,null);

            //dataGridViewX1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightBlue;
Updated 18-Apr-23 21:30pm

You've declared the categoryName list outside of your loop. Every row is bound to the same list of strings, and you add the categories for all rows to that one list. Since you clear the list at the start of each iteration of the loop, the list will only contain the categories for the last row.

Move the list variable inside your loop, and each row will get its own separate list:
foreach (var item in list)
    List<String> categoryName = new List<string>();

NB: You're doing a lot of expensive reflection within your loop. There's almost certainly a better approach. At the very least, you should cache the item.GetType() call in a variable, and cache the value of any properties you read more than once:
foreach (var item in list)
    Type itemType = item.GetType();
    Guid id = (Guid)itemType.GetProperty("id").GetValue(item, null);
Share this answer
foreach (var item in list)

You are using the same list for each row of the DataGridView, so each combo will contain the same data, which is the last set that you process. You should create a new list at the start of the loop so each combo gets its own set of data.
foreach (var item in list)
    categoryName = new List<string>();
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900