0

devexpress winforms グリッドを使用しています。

データセットのデータを 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
4

3 に答える 3

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 イベントで埋めることも可能です

于 2013-10-09T18:10:21.833 に答える
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
     }
 }
于 2013-10-09T18:11:09.447 に答える
1

特定の行に余分な列を条件付きで表示することはできません。問題の 1 つのアプローチは、グリッドにバインドする前に「メッセージ」列を DataSource に追加することです。行にエラーがある場合は、この列のテキストを変更します。

于 2013-10-09T18:07:06.323 に答える