2011-08-12 76 views
1

我有一個xtragrid control on my devxpress form。我創建了columns of my grid at runtime when i load the form。我正在爲我的網格視圖開發字段選擇器,它位於相同的表單上。爲此,我在該控件中使用了repositoryItemCheckedComboBoxEdit控件&,我添加了將出現在xtragrid中的列名稱。問題與DevExpress XtraGridView控件

基本上我創建了列的xtragrid與Visible屬性爲false。當用戶使用repositoryItemCheckedComboBoxEdit檢查特定的列名稱時,如果用戶未選中列名稱,我再次將Visible設置爲true &,然後再次將可見性設置爲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是如下 - 對於field chooser

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; 
     } 

代碼如下 - 我用這個函數用於填充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; 
      } 

     } 

如何解決這個問題呢?

謝謝。

+0

@ Priyanka:如果您在devExpress論壇http://www.devexpress.com/Support/Center/搜索或發佈此問題,可能您會找到解決方案。 – CharithJ

回答

0

您的網格中有多少列?

我看到你有代碼來關閉ColumnAutoWidth一旦你超過5列(即6列或更多)。您是否調試過這種情況以確保ColumnAutoWidth確實被關閉?

根據BestFitColumns Help Doc BestFitColumns將只根據BestFitMaxRowCount屬性計算前n行,除非它設置爲-1,這可能是原因嗎?

如果您在cmdFieldChooser_EditValueChanged事件中設置了cmdFieldChooser的EditValue,這似乎有點奇怪......爲什麼這麼說呢?