如何通过选择另一个Combobox的项目来添加Combobox的项目如果两个Combobox都在C#的Datagridview中 [英] How Can I Add Items Of Combobox By Selcting Another Combobox's item If Both Combobox Are In Datagridview Of C#

查看:49
本文介绍了如何通过选择另一个Combobox的项目来添加Combobox的项目如果两个Combobox都在C#的Datagridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有DataGridView,它包含两个ComboBox列。我希望第二个ComboBox将根据第一个ComboBox中的选定值填充数据。



以下是我的代码。



谢谢



-Tushar



I have DataGridView which contains two ComboBox columns. I want that The second ComboBox will be filled with data depending on the selected value from first ComboBox.

Below is my code.

Thank you

-Tushar

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace t1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            logicload(2, 2);
        }
        public void logicload(int row, int col)
        {

            dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
            for (int i = 1; i <= col; i++)
            {
                dataGridView1.Columns.Add("Col" + i, "Stage" + i);
            }

            dataGridView1.Rows.Add(row);

            //make row 1 at all columns into combobox cell.
            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";

            loadComboboxesSampleItems();
        }
        DataGridViewComboBoxCell CellColumn1, CellColumn2;
        private void loadComboboxesSampleItems()
        {

            CellColumn1 = (DataGridViewComboBoxCell)this.dataGridView1.Rows[0].Cells[1];
             CellColumn2 = (DataGridViewComboBoxCell)this.dataGridView1.Rows[1].Cells[1];
            var list1 = new List<string>() { "IND", "PAK", "AUS", "USA" };
           // var list2 = new List<string>() { "AGRA", "DEHLI", "KANPUR" };
            var list3 = new List<string>() { "MOhali" };

            CellColumn1.DataSource = list1;// CellColumn2.DataSource = list2;

        }

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            ComboBox combo = e.Control as ComboBox;
            if (combo != null)
            {
                combo.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
                combo.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
            }


        }
        private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
             var list2 = new List<string>() { "AGRA", "DEHLI", "KANPUR" };
            if (CellColumn1.Selected.Equals(true))
            {
                ComboBox cb = (ComboBox)sender;
                string item = cb.Text;
                if (item != null)
                {
                    if (cb.SelectedIndex.Equals(0))
                    {
                        //MessageBox.Show(item);
                        CellColumn2.DataSource = list2;

                    }
                }
            }
           
        }
    }
}

推荐答案



  1. 您的陈述




          for(int i = 1; i< = col; i ++)




    使用意外的初始值和意外的条件测试。我原以为




         for(int i = 0;(i< col); i ++)。




    在C#中,数组索引从0开始。







  2. 片段




        Col+ i, 舞台+我




    是懒惰编程的标志。 i不是字符串,不应该以这种方式使用。你应该用过




        Col+ i.ToString(),Stage+ i.ToString()







  3. CellColumn1.Selected是一个布尔值。它返回true或false。所以你的if语句




         if(CellColumn1.Selected.Equals(true))




    应阅读




         if(CellColumn1.Selected)







  4. 你的if语句




         if(item!= null)




    应该读取

    < br>

         if(!String.IsNullOrEmpty(item))







  5. 你的if语句




         if(combo_box.SelectedIndex .Equals(0))




    应阅读




          if(combo_box.SelectedIndex == 0)







  6. 您没有包含所选国家/地区的数据



工作示例如下。


Working example follows.

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



希望有所帮助。


Hope that helps.


这篇关于如何通过选择另一个Combobox的项目来添加Combobox的项目如果两个Combobox都在C#的Datagridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆