我有DataGridView,它包含两个ComboBox列。第二个ComboBox将根据第一个ComboBox中的选定值填充数据。 [英] I have DataGridView which contains two ComboBox columns. The second ComboBox will be filled with data depending on the selected value from first ComboBox.

查看:67
本文介绍了我有DataGridView,它包含两个ComboBox列。第二个ComboBox将根据第一个ComboBox中的选定值填充数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Friends !!!!



我有DataGridView,其中包含两个ComboBox列。第二个ComboBox将填充数据,具体取决于第一个ComboBox上的选定值C#



以下是我的代码





 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;

命名空间 t1
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
logicload( 2 2 );
}
public void logicload( int 行, int col)
{

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

dataGridView1.Rows.Add(row);

// 将所有列的第1行放入组合框单元格。
dataGridView1.Rows [ 0 ]。单元格[ 1 ] = new DataGridViewComboBoxCell();
dataGridView1.Rows [ 1 ]。单元格[ 1 ] = new DataGridViewComboBoxCell();
dataGridView1.Rows [ 0 ]。单元格[ 0 ]。值= 国家;
dataGridView1.Rows [ 1 ]。单元格[ 0 ]。值= 城市;

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

CellColumn1 =(DataGridViewComboBoxCell) this .dataGridView1.Rows [ 0 ]。单元格[ 1 ];
CellColumn2 =(DataGridViewComboBoxCell) this .dataGridView1.Rows [ 1 ]。单元格[ 1 ];
var list1 = new 列表< string>(){ IND PAK AUS USA};
var list2 = new 列表< string>(){ AGRA DEHLI KANPUR};
var list3 = new 列表< string>(){ MOhali};

CellColumn1.DataSource = list1; // CellColumn2.DataSource = list2;
}
私有 void ComboBox_SelectedIndexChanged(对象发​​件人,EventArgs e)
{

}
}
}

解决方案

您需要做的是捕获第一个组合框的CellValueChanged事件,然后在其中重新填充第二个组合框中的值。



退房:



http://stackoverflow.com/questions/11950189/how-to-handle-selectedindexchanged-event-for-a-combobox




我这里有一个代码片段,但数据不是来自datagrid。这个有点简单。希望它能帮助你提出良好的逻辑。

  private   void  comboBox1_SelectedIndexChanged( object  sender,EventArgs e)
{
if (cboCountry.Text == 菲律宾
{
cboProvince.Items.Clear();
cboProvince.Items.Add( 八打雁);
cboProvince.Items.Add( 马尼拉);
}
else if (cboCountry.Text == Japan
{
cboProvince.Items.Clear();
cboProvince.Items.Add( Kyoto);
cboProvince.Items.Add( Tokyo);
}
else if (cboCountry.Text == 韩国
{
cboProvince.Items.Clear();
cboProvince.Items.Add( 首尔);
cboProvince.Items.Add( 釜山);
}
}





这种代码用于较大的数据并不明智。但我只是给你逻辑。这取决于你如何提出你的解决方案。

你知道,在其他人的帮助下编写代码并提出更好的解决方案是一种非常棒的感觉。希望能帮助到你。 :)


Hello Friends !!!!

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

Below is my code


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 ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

解决方案

What you need to do is catch the CellValueChanged event for the first combo box, and in it you repopulate the values in second combo box.

Check out:

http://stackoverflow.com/questions/11950189/how-to-handle-selectedindexchanged-event-for-a-combobox


Hi,
I have a code snippet here, but the data isn't from the datagrid. This one is kinda simple. Hope it will help you to come up with the good logic.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboCountry.Text == "Philippines")
            {
                cboProvince.Items.Clear();
                cboProvince.Items.Add("Batangas");
                cboProvince.Items.Add("Manila");
            }
            else if (cboCountry.Text == "Japan")
            {
                cboProvince.Items.Clear();
                cboProvince.Items.Add("Kyoto");
                cboProvince.Items.Add("Tokyo");
            }
            else if (cboCountry.Text == "South Korea")
            {
                cboProvince.Items.Clear();
                cboProvince.Items.Add("Seoul");
                cboProvince.Items.Add("Busan");
            }
        }



This kind of code isn't wise to use for larger data. But I'm just giving you the logic. It's up to you on how you will come up with your solution.
You know, coding with a little help from others and you coming up with more good solution is such a great feeling. Hope it helps. :)


这篇关于我有DataGridView,它包含两个ComboBox列。第二个ComboBox将根据第一个ComboBox中的选定值填充数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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