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
1 6550
To enable this, two versions of the filtered list (subcategory) need to be created.
Heres how I did it: -
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Text;
-
using System.Windows.Forms;
-
-
namespace DataGridProblem
-
{
-
public partial class Form1 : Form
-
{
-
DataTable categoryDT, subCategoryDT;
-
BindingSource catBS, filteredSubCatBS, unfilteredSubCatBS;
-
-
public Form1()
-
{
-
categoryDT = new DataTable("category");
-
categoryDT.Columns.Add("ID", typeof(int));
-
categoryDT.Columns.Add("Name", typeof(string));
-
-
subCategoryDT = new DataTable("subcategory");
-
subCategoryDT.Columns.Add("ID", typeof(int));
-
subCategoryDT.Columns.Add("subID", typeof(int));
-
subCategoryDT.Columns.Add("Name", typeof(string));
-
-
categoryDT.Rows.Add(new object[] { 0, "cat0" });
-
categoryDT.Rows.Add(new object[] { 1, "cat1" });
-
categoryDT.Rows.Add(new object[] { 2, "cat2" });
-
-
subCategoryDT.Rows.Add(new object[] { 0, 0, "SubCat0-Cat0" });
-
subCategoryDT.Rows.Add(new object[] { 1, 0, "SubCat1-Cat0" });
-
subCategoryDT.Rows.Add(new object[] { 2, 0, "SubCat2-Cat0" });
-
subCategoryDT.Rows.Add(new object[] { 3, 1, "SubCat3-Cat1" });
-
subCategoryDT.Rows.Add(new object[] { 4, 1, "SubCat4-Cat1" });
-
subCategoryDT.Rows.Add(new object[] { 5, 1, "SubCat5-Cat1" });
-
subCategoryDT.Rows.Add(new object[] { 6, 2, "SubCat6-Cat2" });
-
subCategoryDT.Rows.Add(new object[] { 7, 2, "SubCat7-Cat2" });
-
subCategoryDT.Rows.Add(new object[] { 8, 2, "SubCat8-Cat2" });
-
-
InitializeComponent();
-
-
catBS = new BindingSource();
-
catBS.DataSource = categoryDT;
-
-
CategoryCol.DataSource = catBS;
-
CategoryCol.DisplayMember = "Name";
-
CategoryCol.ValueMember = "ID";
-
-
// the ComboBox column is bound to the unfiltered DataView
-
unfilteredSubCatBS = new BindingSource();
-
DataView undv = new DataView(subCategoryDT);
-
unfilteredSubCatBS.DataSource = undv;
-
-
SubCategoryCol.DataSource = unfilteredSubCatBS;
-
SubCategoryCol.DisplayMember = "Name";
-
SubCategoryCol.ValueMember = "ID";
-
-
// this binding source is where I perform my filtered view
-
filteredSubCatBS = new BindingSource();
-
DataView dv = new DataView(subCategoryDT);
-
filteredSubCatBS.DataSource = dv;
-
-
}
-
-
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
-
{
-
try
-
{
-
if (e.ColumnIndex == SubCategoryCol.Index)
-
{
-
DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
-
dgcb.DataSource = filteredSubCatBS;
-
this.filteredSubCatBS.Filter = "subID = " + this.dataGridView1[e.ColumnIndex - 1, e.RowIndex].Value.ToString();
-
}
-
}
-
catch (Exception ex)
-
{
-
MessageBox.Show(ex.Message + ex.StackTrace);
-
}
-
}
-
-
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
-
{
-
try
-
{
-
if (e.ColumnIndex == SubCategoryCol.Index)
-
{
-
DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
-
dgcb.DataSource = unfilteredSubCatBS;
-
this.filteredSubCatBS.RemoveFilter();
-
}
-
}
-
-
catch (Exception ex)
-
{
-
MessageBox.Show(ex.Message + ex.StackTrace);
-
}
-
}
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
|
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();
|
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,
|
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.
| |
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
|
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:
|
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.
|
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")
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| | |