【问题标题】:How to write condition in gridview column? Winform DevExpress如何在gridview列中写入条件? Winform DevExpress
【发布时间】:2013-11-22 13:24:56
【问题描述】:

当 GridView 中的列值 ==“折扣”时,我需要触发一个事件。我在第一列中使用了存储库查找编辑。因此,如果我选择任何项目,它会执行一些计算。如果我从存储库查找编辑中选择“折扣”特别项目,我需要启动另一个计算。 我尝试了这段代码,但它跳过了 if 条件。

private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{ 
    GridView view = sender as GridView; 
    if (gridView1.Columns["Type"] == gridView1.GetFocusedRowCellValue("Discount")) { 
        if (e.Column.FieldName == "Totalototal" && e.IsGetData)
           e.Value = getTotalValue(view, e.ListSourceRowIndex); 
    } 
}

if条件怎么写??

【问题讨论】:

  • 您在哪里获得该代码?即哪个事件处理方法?
  • 嗨,克里斯,我用过private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { GridView view = sender as GridView; if (gridView1.Columns["Type"] == gridView1.GetFocusedRowCellValue("Discount")) { if (e.Column.FieldName == "Totalototal" && e.IsGetData) e.Value = getTotalValue(view, e.ListSourceRowIndex); } }
  • 我使用了 CustomUnboundColumnData 事件
  • 您可以编辑您的问题以在您的评论中包含代码吗?
  • 嗨比尔伍德格` if (gridView1.Columns["Type"] == gridView1.GetFocusedRowCellValue("Discount")) { }`

标签: c# winforms gridview devexpress


【解决方案1】:

试试这个:

假设你的 RepositoryItemGridLookUpEdit 是这样绑定的:

repositoryItemGridLookUpEdit1.DisplayMember = "Description";
repositoryItemGridLookUpEdit1.ValueMember = "Id";
// here probably you will use a table from db
repositoryItemGridLookUpEdit1.DataSource = new [] 
{
     new  { Id=1, Description = "Normal" },
     new  { Id=2, Description = "Discount" },
};

然后你有你的 GridControl 并像这样设置 DataSource:

class GridViewDataSource
{
    // the id of the description type
    public int DescriptionId { get; set; } 
}

// again, this probably is taken from db
gridControl2.DataSource = new GridViewDataSource[] 
{
       new GridViewDataSource {  DescriptionId = 1 },
       new GridViewDataSource {  DescriptionId = 2 },
       new GridViewDataSource {  DescriptionId = 1 },
       new GridViewDataSource {  DescriptionId = 2 },
};

然后你必须创建 CustomUnboundColumnData 事件

void gridView2_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
    {
        GridView view = sender as GridView;
        if (e.Column.Name == "UnboundColumn" && e.IsGetData)
        {
            // here you get the Id of the type
            var value = (int)view.GetListSourceRowCellValue(e.ListSourceRowIndex, TypeColumn);
            // here you get the "Description", for example "Normal" or "Discount"
            var text = repositoryItemGridLookUpEdit1.Properties.GetDisplayText(value);

            // here you can whatever you want, for example set the text of an unbound column to something
            if(text == "Discount")
                e.Value = "!!!!";
        }
    }

您的 TypeColumn 应该具有以下属性:

this.TypeColumn.Caption = "TypeColumn";
this.TypeColumn.ColumnEdit = this.repositoryItemGridLookUpEdit1;
this.TypeColumn.FieldName = "DescriptionId";
this.TypeColumn.Name = "TypeColumn";

还有你 UnboundColumn 这些:

this.UnboundColumn.Caption = "UnboundColumn";
this.UnboundColumn.FieldName = "None";
this.UnboundColumn.Name = "UnboundColumn";
this.UnboundColumn.UnboundType = DevExpress.Data.UnboundColumnType.String;

【讨论】:

  • 您好 Tomek,感谢您的回答。我得到了一个想法,但在这一行var value = (int)view.GetListSourceRowCellValue(e.ListSourceRowIndex, TypeColumn); 中仍然出现错误Unable to cast object of type 'System.DBNull' to type 'System.String'.
  • 然后我再次修改为var valu = view.GetListSourceRowCellValue(e.ListSourceRowIndex, colType) == DBNull.Value ? 0 : Convert.ToInt32(view.GetListSourceRowCellValue(e.ListSourceRowIndex, colType)); 错误显示像这样Input string was not in a correct format. 我的错误是什么?帮帮我
  • 假设您从数据库中获取数据,这仅仅意味着您有一些 NULL 值。先检查一下。
猜你喜欢
  • 2013-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
  • 2012-05-22
  • 1970-01-01
  • 1970-01-01
  • 2014-05-18
相关资源
最近更新 更多