473,799 Members | 3,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataGridView ComboBoxCell, ArgumentExcepti on

3 New Member
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.Argument Exception.

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.Argument Exception.

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

or

Approach 2:
DataGridViewCom boBoxColumn MyColumn = new DataGridViewCom boBoxColumn();
MyColumn.DataSo urce = MyDataTable;
---On cell value change:
combocell.Displ ayMember = "ColumnName "; (from MyDataTable)
(throws ArgumentExcepti on anytime DisplayMember is re-assigned)

I'd appreciate any help I can get.

Thanks,
Preethi
May 17 '07 #1
1 6550
preeethi
3 New Member
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

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

Similar topics

6
6418
by: dbuchanan | last post by:
Hello, Is this a bug? Is there some kind of work around? I want to add default values for a few columns in my datagridview I found the "DefaultValuesNeeded" event for the datagridview I gave it a try using the example given in
3
4906
by: Danny Nielsen | last post by:
Hi I am having trouble figuring out how to use combobox in a datagridview. What seemed to be a simple task turned out not to arrghh. I have a dataGridView with 4 columns, one of them is column ID (double). I want to be able to lookup a textual value using a combobox and - when choosed - replace this textual value with the corresponding ID ... the pulldownlist has a dataSource pointing to a table. It shows a text
0
1715
by: Nodir Gulyamov | last post by:
Hello All, First of all thanks in advance to everyone who will try to help me. I have Form with TreeView control and DataGridView control. Each TreeNode has its own rows and due to I am placing rows to ArrayList and store it in tag of TreeNode like this: // dgv - DataGridView visible within whole class private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { dgv.Rows.Clear();
0
1088
by: Karen Hill | last post by:
Hello .NET guru, I'm successfully adding rows to my datagrid view with the Add method, except for my comboboxcell. How does one supply a value for it with the row add method?!? I get an exception every time I hover over it with my mouse as is. regards,
0
1256
by: Rick | last post by:
Hi guys!!!, once i used a property that do it in a datagridview but i dont remember what is its name, anybody knows about it? this is for select the comboboxcell with one click and not two as default Regards.
2
4199
by: Michael Meckelein | last post by:
I get "Value of '3720' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum' exception if I remove rows in a dataGridView while scrolling from the top to the end of the grid in one fell swoop. You can reproduce this using the code listed under . .NET 2.0 is used. The only description of the exception found at Microsoft was not helpful. As the exception occurs in
0
2064
by: Andrus | last post by:
I tried to use modeless picklist for DataGridView custom ComboBoxColumn without success. Steps to reproduce issue: 1. Run code 2. Enter some character 3. Press Tab Observed:
18
6067
by: Andrus | last post by:
Marc, Thank you very much. I have issue on implementing add row properly using this. User presses down arrow in last row in grid starting adding new row. Then user changes its mind desiding that new row should not added and presses up arrow. DataGridView does not show this unfinished row anymore.
4
2378
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I use unbound DataGridView controls in my form (simply because I have to manually message my data before populating the DataGridView with it). Say I clear all the info from the DataGridView: DataGridView1.Columns.Clear() Now I manually add new columns to fill: DataGridView1.Columns.Add("Date_Time", "Date_Time")
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10243
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.