如何在gridview列中写入条件? Winform DevExpress

时间:2013-11-22 13:24:56

标签: c# winforms gridview devexpress

我需要在GridView中列值==“折扣”时触发事件。我在第一列中使用了Repository Lookup Edit。因此,如果我选择任何项目,它会执行一些计算。如果我从Repository Lookup Edit中选择“Discount”particulat Item,我需要触发另一个计算。 我尝试了这个代码,但如果条件,它会跳过。

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

如果条件如何写?

1 个答案:

答案 0 :(得分: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;