Excel VSTO功能区DropDownItem索引为-1

时间:2018-01-30 17:43:50

标签: c# excel vsto

我在自定义Exel功能区中有一些VSTO功能区组合框。我遇到困难的两个组合框是CBO.Division和CBO.Category。我有Division_ItemsLoading方法执行数据库查询并使用结果填充除法组合框。

CBO_Division_TextChanged有一个返回所有类别项的查询,并使用除法组合框中的选定文本作为其查询的一部分

并非所有部门都有类别,我需要知道在选择新部门时如何从类别组合框中清除当前选定的项目。这是如何完成的?

到目前为止,这是我的代码:

 #region ComboBox: Division 
    private void cbo_Division_ItemsLoading(object sender, RibbonControlEventArgs e) //cbo_Division
    {
        cbo_Division.Items.Clear();

        try
        {
            using (con = new SqlConnection(CS))
            {
                using (cmd = new SqlCommand(@"SELECT DISTINCT SUBSTRING(groupname, CHARINDEX('_', Groupname) + 1,
                                              CHARINDEX('/', groupname) - 1 - CHARINDEX('_', Groupname))
                                              FROM groups where Groupname like @division;", con))
                {
                    con.Open();
                    cmd.Parameters.AddWithValue("@division", "Approvers.CCB%");
                    using (rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            var rddi = this.Factory.CreateRibbonDropDownItem();
                            rddi.Label = rdr[0].ToString();
                            cbo_Division.Items.Add(rddi);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            rdr.Close();
            con.Close();
        }
    }

    private void cbo_Division_TextChanged(object sender, RibbonControlEventArgs e)
    {
        cbo_Category.Items.Clear();

        try
        {
            //Clear all approvers
            XLS_RemoveStakeHolders.RemoveApprovers();

            using (con = new SqlConnection(CS))
            {
                using (cmd = new SqlCommand(@"select RIGHT(groupname, len(groupname) - charindex('/',Groupname))  
                                              FROM groups where Groupname like @division ORDER BY groupName;", con))
                {
                    con.Open();
                    cmd.Parameters.AddWithValue("@division", "Approvers.CCB_" + cbo_Division.Text + "%");
                    using (rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            var rddi = this.Factory.CreateRibbonDropDownItem();
                            rddi.Label = rdr[0].ToString();
                            cbo_Category.Items.Add(rddi);
                        }
                    }
                }
            }

            XLS_RangeData division = new XLS_RangeData(cbo_Division.Text);
            division.SetDivision();

            XLS_RangeData category = new XLS_RangeData(cbo_Category.Text);
            category.SetCategory();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            rdr.Close();
            con.Close();
        }

    }
    #endregion

1 个答案:

答案 0 :(得分:0)

您需要在分组组合框的更改事件中使类别组合框无效。此方法将触发重新加载类别combox框的源。

    public void OnChange(Office.IRibbonControl control, string text)
    {
        try
        {
            switch (control.Id)
            {
                case "cbo_Division":
                    ribbon.InvalidateControl("cbo_Category");
                    break;

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

以下是on change事件的XML示例。

                <comboBox
                        id="cbo_Division"
                        label="Division: "
                        onChange="OnChange"
                        getText="GetLabelText"
                        getItemCount="GetItemCount"
                        getItemLabel="GetItemLabel"
                        sizeString= "XXXXXXXXXXXXXXXXXXXX"
                        screentip="Division Name"
                        supertip="This is the Division name."
                        />