- Your for statement
for (int i = 1; i <= col; i++)
uses an unexpected initial value and an unexpected condition test. I would have expected
for ( int i = 0; ( i < col ); i++).
In C#, arrays indices start at 0.
- The fragment
"Col" + i, "Stage" + i
is a sign of lazy programming. "i" is not a string and should not be used that way. You should have used
"Col" + i.ToString ( ), "Stage" + i.ToString ( )
- CellColumn1.Selected is a boolean. It returns true or false. So your if statement
if ( CellColumn1.Selected.Equals ( true ) )
should read
if ( CellColumn1.Selected )
- Your if statement
if ( item != null )
should read
if ( !String.IsNullOrEmpty ( item ) )
- Your if statement
if ( combo_box.SelectedIndex.Equals ( 0 ) )
should read
if ( combo_box.SelectedIndex == 0 )
- You did not include data for the chosen country
Working example follows.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace DataGridViewComboBoxes
{
public partial class Form1 : Form
{
enum ControlTypes
{
COMBO,
TEXT
}
const int CITIES_ROW = 1;
const int CITIES_CELL = 1;
const int COUNTRIES_ROW = 0;
const int COUNTRIES_CELL = 1;
const int NUMBER_COLUMNS = 2;
const int NUMBER_ROWS = 2;
List < string > Australian_cities =
new List < string > ( ) {
"Sydney",
"Melbourne",
"Brisbane",
"Perth",
"Adelaide" };
DataGridViewComboBoxCell countries_cell;
DataGridViewComboBoxCell cities_cell;
List < string > cities;
List < string > Indian_cities =
new List < string > ( ) {
"Mumbai",
"Delhi",
"Bangalore",
"Karnataka",
"Hyderabad" };
List < string > Pakistani_cities =
new List < string > ( ) {
"Karachi",
"Lahore",
"Faisalabad",
"Rawalpindi",
"Punjab" };
List < string > USA_cities =
new List < string > ( ) {
"New York",
"Los Angeles",
"Chicago",
"Houston",
"Philadelphia" };
List < string > countries =
new List < string > ( ) {
"IND",
"PAK",
"AUS",
"USA" };
public Form1 ( )
{
InitializeComponent ( );
initialize_datagridview ( NUMBER_ROWS, NUMBER_COLUMNS );
load_sample_data ( );
dataGridView1.DataError +=
new DataGridViewDataErrorEventHandler (
dataGridView1_DataError );
}
void dataGridView1_DataError (
object sender,
DataGridViewDataErrorEventArgs e )
{
}
void initialize_datagridview ( int rows,
int columns )
{
dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
for ( int i = 0; ( i < columns ); i++ )
{
string i_plus_one = ( i + 1 ).ToString ( );
dataGridView1.Columns.Add ( "Col" + i_plus_one,
"Stage" + i_plus_one );
}
dataGridView1.Rows.Add ( rows );
dataGridView1.Rows [ 0 ].Cells [ 1 ] =
new DataGridViewComboBoxCell ( );
dataGridView1.Rows [ 1 ].Cells [ 1 ] =
new DataGridViewComboBoxCell ( );
dataGridView1.Rows [ 0 ].Cells [ 0 ].Value = "Country";
dataGridView1.Rows [ 1 ].Cells [ 0 ].Value = "City";
dataGridView1.EditingControlShowing +=
new DataGridViewEditingControlShowingEventHandler (
dataGridView1_EditingControlShowing );
}
private void load_sample_data ( )
{
countries_cell = ( DataGridViewComboBoxCell )
dataGridView1.Rows [ COUNTRIES_ROW ].
Cells [ COUNTRIES_CELL ];
cities_cell = ( DataGridViewComboBoxCell )
dataGridView1.Rows [ CITIES_ROW ].
Cells [ CITIES_CELL ];
countries_cell.DataSource = null;
countries_cell.DataSource = countries;
}
void dataGridView1_EditingControlShowing (
object sender,
DataGridViewEditingControlShowingEventArgs e )
{
ControlTypes control_type;
Object editing_control;
try
{
editing_control =
( DataGridViewTextBoxEditingControl ) e.Control;
control_type = ControlTypes.TEXT;
}
catch
{
editing_control =
( DataGridViewComboBoxEditingControl ) e.Control;
control_type = ControlTypes.COMBO;
}
if ( control_type == ControlTypes.COMBO )
{
ComboBox combo_box = ( ComboBox ) editing_control;
if ( combo_box != null )
{
combo_box.TextChanged -=
new EventHandler (
editingcontrol_TextChanged );
combo_box.TextChanged +=
new EventHandler (
editingcontrol_TextChanged );
}
}
}
void editingcontrol_TextChanged ( object sender,
EventArgs e )
{
if ( countries_cell.Selected )
{
ComboBox combo_box = ( ComboBox ) sender;
string text = combo_box.Text;
if ( !String.IsNullOrEmpty ( text ) )
{
switch ( text )
{
case "IND":
cities = Indian_cities;
break;
case "PAK":
cities = Pakistani_cities;
break;
case "AUS":
cities = Australian_cities;
break;
case "USA":
cities = USA_cities;
break;
default:
throw new ApplicationException (
"Unrecognized country" );
}
cities_cell.Dispose ( );
cities_cell = ( DataGridViewComboBoxCell )
dataGridView1.
Rows [ CITIES_ROW ].
Cells [ CITIES_CELL ];
combo_box.TextChanged -=
new EventHandler (
editingcontrol_TextChanged );
combo_box.TextChanged +=
new EventHandler (
editingcontrol_TextChanged );
cities_cell.DataSource = null;
cities_cell.DataSource = cities;
}
}
}
}
}
Hope that helps.