简体   繁体   English

如何在C#中设置组合框的选定值

[英]how to set selected value of combo box in C#

I have two combo box ( say cbo_zone & cbo_floor ) which are having table as the data source 我有两个组合框(例如cbo_zone和cbo_floor),它们都具有表作为数据源

private void load_cbo_zone() 
    { 
        clz_Common_References ccr = new clz_Common_References ();
        DataTable dt_zone = ccr.get_zone_detail();
        cbo_zone.DataSource = dt_zone;
        cbo_zone.ValueMember = "ID";
        cbo_zone.DisplayMember = "zone";
        cbo_zone.SelectedIndex = -1;
    }

` `

    private void load_cbo_floor()
    {
        int ID_zone_ref = Convert.ToInt16(cbo_zone.SelectedValue);
        clz_Common_References ccr = new clz_Common_References();
        DataTable dt_flr = ccr.get_floor_data_fr_Ref_IDZone(ID_zone_ref);
        cbo_floor.DataSource = dt_flr;
        cbo_floor.DisplayMember = "Floor";
        cbo_floor.ValueMember = "ID";
        cbo_floor.SelectedIndex = -1;
    }

` . `。 I wrote the code to update the cbo_floor as following . 我编写了如下代码来更新cbo_floor。

private void cbo_zone_SelectionChangeCommitted(object sender, EventArgs e)
    {
        load_cbo_floor();
    }

Now I need to update the cbo_zone & cbo_floor when I click a data row of datagridview . 现在,当我单击datagridview的数据行时,需要更新cbo_zone和cbo_floor。

int ref_area_id, ref_floor_id, ref_zone_id;

        int.TryParse(dt_issued_mat.Rows[0][11].ToString(), out ref_area_id);
        DataTable dt_area_detail = ccr.get_area_data_fr_area_id(ref_area_id);
        int.TryParse(dt_area_detail.Rows[0][2].ToString(), out ref_floor_id);
        DataTable dt_floor_detail = ccr.get_floor_data_fr_floor_id(ref_floor_id);
        int.TryParse(dt_floor_detail.Rows[0][2].ToString(), out ref_zone_id);
        DataTable dt_zone_detail = ccr.get_zone_data_fr_zone_id(ref_zone_id);

after that using 之后使用

cbo_zone.Text  = dt_zone_detail.Rows[0][1].ToString();
cbo_floor.Text  = dt_floor_detail.Rows[0][1].ToString();

I was able to display the values on the combo boxes but once I tried to get the cbo_floor.SelectedValue code doesn't work . 我能够在组合框上显示这些值,但是一旦尝试获取cbo_floor.SelectedValue代码就无法使用。

Then I was able to get the relevant SelectedIndex by using , 然后,我可以使用来获取相关的SelectedIndex

int index = cbo_zone.FindString(dt_zone_detail.Rows[0][1].ToString());
        cbo_zone.SelectedIndex  = index ;

still the combo box shows nothing & "cbo_zone.SelectedValue" doesn't show a value.my target is to get the cbo_floor.SelectedValue . 组合框仍然不显示任何内容,“ cbo_zone.SelectedValue”也不显示值。我的目标是获取cbo_floor.SelectedValue。 Please help . 请帮忙 。

Instead of cbo_zone.Text = dt_zone_detail.Rows[0][1].ToString(); cbo_floor.Text = dt_floor_detail.Rows[0][1].ToString(); 代替cbo_zone.Text = dt_zone_detail.Rows[0][1].ToString(); cbo_floor.Text = dt_floor_detail.Rows[0][1].ToString(); cbo_zone.Text = dt_zone_detail.Rows[0][1].ToString(); cbo_floor.Text = dt_floor_detail.Rows[0][1].ToString();

Use 采用

cbo_zone.Items.Add( dt_zone_detail.Rows[0][1].ToString());

cbo_floor.Items.Add ( dt_floor_detail.Rows[0][1].ToString());

First working with indexes is not a good idea. 首先使用索引不是一个好主意。 You have the value, use them. 您拥有价值,使用它们。 Using the index will cause you trouble. 使用索引会给您带来麻烦。

cbo_zone.SelectedValue = myValue.ToString();//find the value from your data

Secondly for selecting the floor, you need to fill it with the relevant values first, since it already has the data for the last selection. 其次,选择地板时,您需要先用相关值填充地板,因为它已经具有最后选择的数据。

  load_cbo_floor();
  cbo_floor.SelectedValue = myValue.ToString();//find the value from your data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM