依赖组合框填充的问题

时间:2011-06-27 09:34:43

标签: c# sql combobox

我有一个Windows窗体和用户从组合框中选择布料参数,例如country-state-city 方法。我的参数是:

typeName - typeNo - pattern - variant
R - 125 - 254978 - 1
R - 125 - 254979 - 1
R - 125 - 254979 - 2
Ri- 54 - 54658 - 1
Ri- 54 - 54658 - 2... etc.

我在formLoad上填写typeNameCombobox 比typeNoCombobox填充typeNameCombobox_SelectedIndexChanged()事件 并且喜欢这个patternCombobox填写typeNoCombobox_SelectedIndexChanged()事件......

但是在typeNameCombobox_SelectedIndexChanged()中存在问题 错误消息说:

无法绑定多部分标识符“System.Data.DataRowView”。

并使用这种荒谬的方法

if (typeNameCombobox .SelectedValue.ToString() != "System.Data.DataRowView")
{
 fill -> typeNoCombobox
}

这四个组合框是依赖的,下面的组合框不会填充第一个组合框。

我使用第二种荒谬的方法

typeName.selectedIndex = 1;
typeName.selectedIndex = 0;
typeName.selectedIndex = 1;
typeName.selectedIndex = 0; 

(我必须至少做4次,因为有四个依赖的组合框)

在所有组合框填充后,如果用户更改typeName没有问题,所有相关值都是changin

有没有办法让这段代码清楚?

回复后编辑:

蒂普图尔=类型名 tip = typeNo desen =模式 varyant = variant

这是我的原始代码

 ConnectionClassOleDb cc = new ConnectionClassOleDb();


    private void SiparisDetay_Load(object sender, EventArgs e) //formload
        {            

            sorgu = "Select distinct tipTurId, tipTur from View_Tip order by tipTur";
            cc.cbDoldur(sorgu, cbTipTur, "tipTur", "tipTurId"); 

            cbTipTur.SelectedIndex = 1;
            cbTipTur.SelectedIndex = 0;
            cbTipTur.SelectedIndex = 1;
            cbTipTur.SelectedIndex = 0;       

        }



    private void cbTipTur_SelectedIndexChanged(object sender, EventArgs e) 
        {
            if (cbTipTur.SelectedValue.ToString() != "System.Data.DataRowView")
            {
                string sorgu = "SELECT distinct tipNo FROM tblTip where " +
                    " tipTurId = '" + cbTipTur.SelectedValue.ToString() + "' ORDER BY tipNo";
                cc.cbDoldur(sorgu, cbTip, "tipNo", "tipNo");

            }
        }

        private void cbTip_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbTip.SelectedValue.ToString() != "System.Data.DataRowView")
            {
                string sorgu = "SELECT desen FROM tblTip WHERE " +
                    " tipTurId = '" + cbTipTur.SelectedValue.ToString() +
                    "' AND tipNo = '" + cbTip.SelectedValue.ToString() + "' order by desen";
                cc.cbDoldur(sorgu, cbDesen, "desen", "desen");
            }
        }

        private void cbDesen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbDesen.SelectedValue.ToString() != "System.Data.DataRowView")
            {
                string sorgu = "SELECT varyant FROM tblTip WHERE " +
                               " desen = '" + cbDesen.SelectedValue.ToString() +
                               "' AND tipTurId = '" + cbTipTur.SelectedValue.ToString() +
                               "' AND tipNo = '" + cbTip.SelectedValue.ToString() + "' ORDER BY varyant";
                cc.cbDoldur(sorgu, cbVaryant, "varyant", "varyant");
            }
        }

和connectionclass中的代码如下:

public void cbDoldur(string sorgu, ComboBox cb, string text, string value) //fill combobox
        {        
            DataTable dtDdl = Dt(sorgu);
            cb.DataSource = dtDdl;
            cb.DisplayMember = text;
            cb.ValueMember = value;            

            if (cb.Items.Count > 0)
            {
                cb.SelectedIndex = 0;
            }

        }


 public DataTable Dt(string query)
        {

            DataTable dt = new DataTable();
            if (Open())
            {
                OleDbDataAdapter da = new OleDbDataAdapter(query, connection);
                try
                {
                    da.Fill(dt);

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Sistem Mesajı:" + ex.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            //connection.Close();
            return dt;
        }

2 个答案:

答案 0 :(得分:1)

您应该尝试将Alias添加到查询中的字段名

Select distinct tipTurId, tipTur from View_Tip

Select distinct tipTurId as tipTurId , tipTur from View_Tip

在这种情况下SELECT distinct tipNo FROM tblTip

SELECT distinct tipNo as tipNo FROM tblTip

因为它无法在DataSource中找到tipTurId字段,因为它会返回Distinct,与tipNo

的情况相同

答案 1 :(得分:0)

我解决了我的问题,就像在这个

中更改我的comboboxpopulate函数
cb.DisplayMember = text;
cb.ValueMember = value;
cb.DataSource = dtDdl;