1

我有一个xtragrid control on my devxpress form。我创建了columns of my grid at runtime when i load the form. 我正在为位于同一表单上的网格视图开发字段选择器。为此,我使用了repositoryItemCheckedComboBoxEdit控件并在该控件中添加了将出现在 xtragrid 中的列名。

基本上,我创建了 xtragrid 的列,并将 Visible 属性设置为 false。当用户使用repositoryItemCheckedComboBoxEdit 检查特定列名时,我将Visible 设置为true,如果用户取消选中列名,我再次将visible 设置为false。& 在创建列时,我设置了列的宽度。

Problem which i'm facing is that if user select the all fields from the repositoryItemCheckedComboBoxEdit then the grid control should show the horizontal scroll bar automatically when require.

And another problem is that with the columns is besides setting the width of the column, it is not showing the required width of that column . it shrinks that all column width .

我用于在运行时为 xtragridview 创建列的代码如下 -

 public void AddGridColumn(string fieldName, string caption, int nRowWidth, RepositoryItem Item, object oCollection, string DisplayMember, string ValueMember, string format, FormatType type)
        {
            DevExpress.XtraGrid.Columns.GridColumn column = ColumnView.Columns.AddField(fieldName);
            column.Caption = caption;
            column.ColumnEdit = Item;
            column.DisplayFormat.FormatType = type;
            column.DisplayFormat.FormatString = format;
            column.VisibleIndex =  ColumnView.VisibleColumns.Count;
            column.Width = nRowWidth;
        }

的代码field chooser如下 - 我使用这个函数来填充 repositoryItemCheckedComboBoxEdit 控件的项目

private void FieldCollection()
    {
        allFields = new ArrayList();
        columnNames = new Dictionary<string, string>();
        allFields.Clear();
        repositoryItemCheckedComboBoxEdit1.Items.Clear();
        for (int i = 0; i < gvBase.Columns.Count; i++)
        {
            allFields.Add(gvBase.Columns[i].Caption );
            if (gvBase.Columns[i].FieldName != "ContactID")
            {
                if (gvBase.Columns[i].Visible == true)
                {
                    if (gvBase.Columns[i].Caption != "Label1" && gvBase.Columns[i].Caption != "Label2" && gvBase.Columns[i].Caption != "Label3" && gvBase.Columns[i].Caption != "Label4" && gvBase.Columns[i].Caption != "Label5")
                        repositoryItemCheckedComboBoxEdit1.Items.Add(gvBase.Columns[i].Caption, CheckState.Checked);
                    if (!columnNames.ContainsKey(gvBase.Columns[i].Caption))
                        columnNames.Add(gvBase.Columns[i].Caption, gvBase.Columns[i].FieldName);
                }
                else
                {
                    if (gvBase.Columns[i].Caption != "Label1" && gvBase.Columns[i].Caption != "Label2" && gvBase.Columns[i].Caption != "Label3" && gvBase.Columns[i].Caption != "Label4" && gvBase.Columns[i].Caption != "Label5")

                        repositoryItemCheckedComboBoxEdit1.Items.Add(gvBase.Columns[i].Caption, CheckState.Unchecked);
                    if (!columnNames.ContainsKey(gvBase.Columns[i].FieldName))
                        columnNames.Add(gvBase.Columns[i].Caption, gvBase.Columns[i].FieldName);

                }
            }
        }
        cmbFieldChooser.EditValue = "";
    }

这用于 repositoryItemCheckedComboBoxEdit 控件事件 -

private void cmbFieldChooser_EditValueChanged(object sender, EventArgs e)
        {
            ArrayList temp = new ArrayList();
            temp.AddRange(allFields);

            string[] strFields = cmbFieldChooser.EditValue.ToString().Split(',');
            for (int i = 0; i < strFields.Length; i++)
            {
                if (temp.Contains(strFields[i].Trim()))
                    temp.Remove(strFields[i].Trim());
                if (strFields[i] != "")
                {
                    if (columnNames.ContainsKey(strFields[i].Trim()))
                    {
                        if (gvBase.Columns[columnNames[strFields[i].Trim()]].Visible == false)
                        {
                            gvBase.Columns[columnNames[strFields[i].Trim()]].Visible = true;
                            gvBase.Columns[columnNames[strFields[i].Trim()]].BestFit();

                        }
                    }
                }
            }
            if (temp.Count < 20)
            {
                for (int j = 0; j < temp.Count; j++)
                {
                    if (columnNames.ContainsKey(temp[j].ToString().Trim()))
                    {
                        gvBase.Columns[columnNames[temp[j].ToString().Trim()]].Visible = false;
                    }
                }
            }
            cmbFieldChooser.EditValue = repositoryItemCheckedComboBoxEdit1.GetCheckedItems();
            if ((cmbFieldChooser.EditValue.ToString()).Split(',').Length > 5)
            {
                gvBase.OptionsView.ColumnAutoWidth = false;
                gvBase.BestFitColumns();
                gvBase.HorzScrollVisibility = ScrollVisibility.Always;
            }
            else
            {
                gvBase.OptionsView.ColumnAutoWidth = true;
                gvBase.HorzScrollVisibility = ScrollVisibility.Never;
            }

        }

如何解决这个问题?

谢谢。

4

1 回答 1

0

您的网格中有多少列?

我看到您有代码可以在超过 5 列(即 6 列或更多列)后关闭 ColumnAutoWidth。您是否调试过这种情况以确保 ColumnAutoWidth 确实被关闭?

根据BestFitColumns 帮助文档,BestFitColumns 只会根据 BestFitMaxRowCount 属性计算前 n 行,除非它设置为 -1,这可能是原因吗?

如果您在 cmdFieldChooser_EditValueChanged 事件中设置 cmdFieldChooser 的 EditValue,另一件事似乎有点奇怪......为什么会这样?

于 2011-08-17T23:57:31.267 回答