473,787 Members | 2,934 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 6549
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
6417
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
1086
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
6065
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
2375
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
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
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
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.