471,089 Members | 1,130 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,089 software developers and data experts.

DataGridView ComboBoxCell, ArgumentException

Hello,

I'm designing a DataGridView that has a few combobox columns.

As the user selects a value from a ComboBoxCell in a row. I want to populate another ComboBoxCell in the same row with the corresponding dataset.

for eg:
step1: ComboBoxCell1 - Items: Categories
[Authors: "Ayn Rand", "George Orwell", "Joseph Heller" etc...]
step2: ComboBoxCell2 Items: Members of the selected Categories in step1.
[Books by "Ayn Rand": "Fountain Head","Atlas Shrugged", etc...]

Length of the Members varies in each Category.

ComboBoxCell doesn't allow me to assign values. I can ONLY add to the Items and cannot clear/edit them. If I do, I get System.ArgumentException.

I tried, to set a DataSource<a datatable> to this column and change the DisplayMembers each time the category changes for a row. I have two problems in this approach:
1. Table's Column length with non-null values are variable. So the combobox displays a few Items with null values as the case may be.
2. Everytime the category changes, I <again> get the System.ArgumentException.

The way am doing this is:
Approach 1:
---On cell value change:
DataGridViewComboBoxCell combocell = (DataGridViewComboBoxCell) MyDataGridView[column,row];
combocell.Items.Clear(); [throws argument exception]
combocell.AddRange(<values>);

or

Approach 2:
DataGridViewComboBoxColumn MyColumn = new DataGridViewComboBoxColumn();
MyColumn.DataSource = MyDataTable;
---On cell value change:
combocell.DisplayMember = "ColumnName"; (from MyDataTable)
(throws ArgumentException anytime DisplayMember is re-assigned)

I'd appreciate any help I can get.

Thanks,
Preethi
May 17 '07 #1
1 6286
To enable this, two versions of the filtered list (subcategory) need to be created.

Heres how I did it:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace DataGridProblem
  10. {
  11.   public partial class Form1 : Form
  12.   {
  13.     DataTable categoryDT, subCategoryDT;
  14.     BindingSource catBS, filteredSubCatBS, unfilteredSubCatBS;    
  15.  
  16.     public Form1()
  17.     {
  18.       categoryDT = new DataTable("category");
  19.       categoryDT.Columns.Add("ID", typeof(int));
  20.       categoryDT.Columns.Add("Name", typeof(string));
  21.  
  22.       subCategoryDT = new DataTable("subcategory");
  23.       subCategoryDT.Columns.Add("ID", typeof(int));
  24.       subCategoryDT.Columns.Add("subID", typeof(int));
  25.       subCategoryDT.Columns.Add("Name", typeof(string));
  26.  
  27.       categoryDT.Rows.Add(new object[] { 0, "cat0" });
  28.       categoryDT.Rows.Add(new object[] { 1, "cat1" });
  29.       categoryDT.Rows.Add(new object[] { 2, "cat2" });
  30.  
  31.       subCategoryDT.Rows.Add(new object[] { 0, 0, "SubCat0-Cat0" });
  32.       subCategoryDT.Rows.Add(new object[] { 1, 0, "SubCat1-Cat0" });
  33.       subCategoryDT.Rows.Add(new object[] { 2, 0, "SubCat2-Cat0" });
  34.       subCategoryDT.Rows.Add(new object[] { 3, 1, "SubCat3-Cat1" });
  35.       subCategoryDT.Rows.Add(new object[] { 4, 1, "SubCat4-Cat1" });
  36.       subCategoryDT.Rows.Add(new object[] { 5, 1, "SubCat5-Cat1" });
  37.       subCategoryDT.Rows.Add(new object[] { 6, 2, "SubCat6-Cat2" });
  38.       subCategoryDT.Rows.Add(new object[] { 7, 2, "SubCat7-Cat2" });
  39.       subCategoryDT.Rows.Add(new object[] { 8, 2, "SubCat8-Cat2" });
  40.  
  41.       InitializeComponent();
  42.  
  43.       catBS = new BindingSource();
  44.       catBS.DataSource = categoryDT;
  45.  
  46.       CategoryCol.DataSource = catBS;
  47.       CategoryCol.DisplayMember = "Name";
  48.       CategoryCol.ValueMember = "ID";
  49.  
  50.       // the ComboBox column is bound to the unfiltered DataView
  51.       unfilteredSubCatBS = new BindingSource();
  52.       DataView undv = new DataView(subCategoryDT);
  53.       unfilteredSubCatBS.DataSource = undv;
  54.  
  55.       SubCategoryCol.DataSource = unfilteredSubCatBS;
  56.       SubCategoryCol.DisplayMember = "Name";
  57.       SubCategoryCol.ValueMember = "ID";
  58.  
  59.       // this binding source is where I perform my filtered view
  60.       filteredSubCatBS = new BindingSource();
  61.       DataView dv = new DataView(subCategoryDT);
  62.       filteredSubCatBS.DataSource = dv;
  63.  
  64.     }
  65.  
  66.     private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
  67.     {
  68.       try 
  69.       {
  70.         if (e.ColumnIndex == SubCategoryCol.Index)
  71.         {
  72.           DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
  73.           dgcb.DataSource = filteredSubCatBS;
  74.           this.filteredSubCatBS.Filter = "subID = " + this.dataGridView1[e.ColumnIndex - 1, e.RowIndex].Value.ToString();          
  75.         }      
  76.       }
  77.       catch (Exception ex)
  78.       {
  79.         MessageBox.Show(ex.Message + ex.StackTrace);
  80.       }
  81.     }
  82.  
  83.     private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  84.     {
  85.       try
  86.       {
  87.         if (e.ColumnIndex == SubCategoryCol.Index)
  88.         {
  89.           DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
  90.           dgcb.DataSource = unfilteredSubCatBS;
  91.           this.filteredSubCatBS.RemoveFilter();
  92.         }
  93.       }
  94.  
  95.       catch (Exception ex)
  96.       {
  97.         MessageBox.Show(ex.Message + ex.StackTrace);
  98.       }      
  99.     }
  100.   }
  101. }
  102.  
May 18 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Nodir Gulyamov | last post: by
reply views Thread by Karen Hill | last post: by
2 posts views Thread by Michael Meckelein | last post: by
4 posts views Thread by =?Utf-8?B?anAybXNmdA==?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.