2013-10-09 74 views
0

我使用的DevExpress WinForms的網格。如何動態地添加列在網格的DevExpress數據網格

我從數據集到的DevExpress網格數據綁定。

dataGrid.MainView.GridControl.DataSource = ds; 
dataGrid.MainView = gridView; 
gridView.BestFitColumns(); 

顯示網格線會是這樣

FirstName LastName  
Sharp  Eye 

我打電話RowStyle事件,以顯示背景色基於狀態的網格行。

對於前:

private void gridViewExcel_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) 
{ 
    GridView View = sender as GridView; 
    string firstName = View.GetRowCellDisplayText(e.RowHandle, View.Columns["FirstName"]); 

     if (firstName="Sharp") 
     { 
     e.Appearance.BackColor = Color.Salmon; 
     e.Appearance.BackColor2 = Color.White; 
     } 
    else 
    { 
     //I want to append another column in the end to the dataset that is bound to the grid. 
     //With an error message...(see below) 
    } 
} 

輸出

FirstName LastName  Message 

Sharp  Eye  First name doesn't match 
+0

爲什麼不能多一個列添加到您的數據表?或者將'UnBoundGridColumn'添加到網格 –

回答

1

無法有條件地顯示在特定行一個額外的列。 一種方法對您的問題將是你把它綁定到網格之前,「消息」列添加到數據源。然後,如果某一行出現錯誤,請更改此列中的文本。

+0

另一種選擇是使用工具提示顯示單元格或行錯誤圖標。 – Bit

+0

有關更多信息,請參閱鏈接http://www.devexpress.com/Support/Center/Example/Details/E1933 – Bit

1

可以實例在運行時DevExpress.XtraGrid.Columns.GridColumn()。

例:

DevExpress.XtraGrid.Columns.GridColumn colMessage= newDevExpress.XtraGrid.Columns.GridColumn(); 

colMessage.Caption = "Message"; 
colMessage.FieldName = "<bound datafield>"; 
colMessage.Name = "colMessage"; 
colMessage.OptionsColumn.AllowEdit = false; 
colMessage.OptionsColumn.FixedWidth = true; 
colMessage.OptionsColumn.ReadOnly = true; 
colMessage.Visible = true; 
colMessage.VisibleIndex = 0; 
colMessage.Width = 80; 
View.Columns.AddRange(newDevExpress.XtraGrid.Columns.GridColumn[] { 
      this.colMessage}); 

也有可能宣佈一個綁定列,並與GridView控件事件

1

我不熟悉的DevExpress明確,但我認爲你會過得更好分析你加油吧數據綁定到網格之前。什麼時候調用gridViewExcel_RowStyle?如果頻繁調用,您希望避免沉重的邏輯。

如果你真的想在這裏的邏輯,你應該能夠得到從綁定的源數據集(或從場),並根據需要添加列。

GridView View = sender as GridView; 
    string firstName = View.GetRowCellDisplayText(e.RowHandle, View.Columns["FirstName"]); 

     if (firstName=="Sharp") 
     { 
     e.Appearance.BackColor = Color.Salmon; 
     e.Appearance.BackColor2 = Color.White; 
     } 
    else 
{ 

    DataSet ds = view.DataSource as DataSet; 
    if(ds != null) 
    { 
     //add the column to the table you want if it doesn't exist 
    } 
}