本文介绍了如何在C#帮助上更改DataGridViewComboBoxCell的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将DataGridViewComboBoxCell的样式从Drop-Down更改为Simple
请帮帮我,我真的需要它。
这是我的代码
I want to change the style of DataGridViewComboBoxCell from Drop-Down to "Simple"
please help me i really need it.
here is my Code
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace DataGridViewComboBoxes
{
// *************************************************** class Form1
public partial class Form1 : Form
{
// *************************************** enums and constants
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;
// ************************************************* variables
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" };
// ***************************************************** Form1
public Form1 ( )
{
InitializeComponent ( );
initialize_datagridview ( NUMBER_ROWS, NUMBER_COLUMNS );
load_sample_data ( );
dataGridView1.DataError +=
new DataGridViewDataErrorEventHandler (
dataGridView1_DataError );
}
// *********************************** dataGridView1_DataError
void dataGridView1_DataError (
object sender,
DataGridViewDataErrorEventArgs e )
{
}
// *********************************** initialize_datagridview
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 );
// make first row columns into
// DataGridViewComboBoxCell
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 );
}
// ****************************************** load_sample_data
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;
}
// *********************** dataGridView1_EditingControlShowing
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 );
}
}
}
// ******************************** 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" );
}
// avoid DataError by rebuild
// of cities_cell
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;
}
}
}
} // class Form1
} // namespace DataGridViewComboBoxes
推荐答案
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is ComboBox)
{
ComboBox box = (ComboBox)e.Control;
box.DropDownStyle = ComboBoxStyle.Simple;
}
}
这篇关于如何在C#帮助上更改DataGridViewComboBoxCell的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!