DevExpress Xtra网格细胞图像一次又一次地重复

时间:2014-05-27 09:17:12

标签: c# devexpress xtragrid devexpress-windows-ui

我在某些情况下在特定的网格单元格中设置图像但是图像重复显示或显示为图块而不显示单个图像,我只想显示一个,我无法弄清楚为什么会发生这种情况。

屏幕截图:

enter image description here

这是我的代码:

private void PendingTask_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
{
    try
    {
        if (e.RowHandle < 0)
            return;

        int con = PendingTaskGridView.SortedColumns.Count;

        DataModel.TaskToConfirm task = this.PendingTaskGridView.GetRow(e.RowHandle) as DataModel.TaskToConfirm;

        if (task == null)
            return;

        if (e.Column.Name.Equals("gridColBilling", StringComparison.InvariantCultureIgnoreCase))
        {

            SetTaskStatusColor(e, task);
            //SetTimeDifferenceColor(e, task);
        }

        if (e.Column.Name.Equals("gridColPunctuality", StringComparison.InvariantCultureIgnoreCase))
        {

            SetPunctualityColor(e, task);
            //SetTimeDifferenceColor(e, task);
        }

        if (e.Column.Name.Equals("gridColAttendance", StringComparison.InvariantCultureIgnoreCase))
        {

            SetAttendanceColor(e, task);
            //SetTimeDifferenceColor(e, task);
        }


    }
    catch (Exception ex)
    {
        ApplicationExceptions.HandleAppExc(ex);
    }
}

这是内部方法:

private void SetTaskStatusColor(DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e, DataModel.TaskToConfirm task)
{
    e.Appearance.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");

    if (task.BillingConfirmed)
        e.Appearance.Image = imageListGrid.Images[0];
    else
        e.Appearance.Image = imageListGrid.Images[1];
}

private void SetPunctualityColor(DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e, DataModel.TaskToConfirm task)
{
    e.Appearance.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");

    if (task.PunctualityStatus == (int)ePunctuality.Extra)
        e.Appearance.Image = CareGiver.Properties.Resources.Cancelled_Image;
    else if (task.PunctualityStatus == (int)ePunctuality.Less)
        e.Appearance.Image = CareGiver.Properties.Resources.Update_CSD;
    else
        e.Appearance.Image = CareGiver.Properties.Resources.Category_Green;

}


private void SetAttendanceColor(DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e, DataModel.TaskToConfirm task)
{
    e.Appearance.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");

    if (task.AttendanceType == 2)
        e.Appearance.Image = imageList1.Images["auto"];
    else if (task.AttendanceType == 1)
        e.Appearance.Image = imageList1.Images["manual"];
    else if (task.AttendanceType == 0)
        e.Appearance.Image = imageList1.Images["default"];


}

2 个答案:

答案 0 :(得分:0)

在这种情况下,AppearanceObject.Image属性用于指定单元格背景的填充图案。因此,您可以看到所有这些图像重复 我建议你使用GridView.CustomDrawCell事件在单元格范围内绘制单个图像,或者使用GridColumn.ColumnEdit编辑器分配RepositoryItemImageComboBox属性,以根据单元格值显示不同的图像。

相关帮助文章:
Custom Painting
Assigning Editors to Individual Cells

答案 1 :(得分:-1)

我在高级同事的帮助下使用DataSourceChanged活动和CustomUnboundColumnData活动解决了这个问题。

这是DataSourceChanged事件,其中我将代码后面的图像列添加为未绑定列,并将其UnobundType设置为Object,并且必须将ColumnEdit属性设置为RespositotyItemPictureEdit对象:

private void PendingTask_SourceChanged(object sender, EventArgs e)
    {

        AddImageColumns();           

    }

<强> AddImageColumns():

private void AddImageColumns()
{
    RepositoryItemPictureEdit pictureEdit = this.PendingTaskGrid.RepositoryItems.Add("PictureEdit") as RepositoryItemPictureEdit;
    pictureEdit.SizeMode = PictureSizeMode.Zoom;
    PendingTaskGridView.OptionsView.AnimationType = GridAnimationType.AnimateAllContent;
    pictureEdit.NullText = " ";

    if (this.PendingTaskGridView.Columns.ColumnByName("StatusImage") == null)
    {
        this.PendingTaskGridView.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn()
        {
            Caption = " ",
            Name = "StatusImage",
            FieldName = "StatusImage",
            Visible = true,
            UnboundType = DevExpress.Data.UnboundColumnType.Object,
            VisibleIndex = 0,
            Width = 25,
            MaxWidth = 25,
            ToolTip = "Task Status",
            ColumnEdit = pictureEdit
        });
    }

    if (this.PendingTaskGridView.Columns.ColumnByName("PunctualityImage") == null)
    {
        this.PendingTaskGridView.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn()
        {
            Caption = " ",
            Name = "PunctualityImage",
            FieldName = "PunctualityImage",
            Visible = true,
            UnboundType = DevExpress.Data.UnboundColumnType.Object,
            VisibleIndex = 0,
            Width = 25,
            MaxWidth = 25,
            ToolTip = "Punctuality",
            ColumnEdit = pictureEdit
        });
    }

    if (this.PendingTaskGridView.Columns.ColumnByName("AttendanceImage") == null)
    {
        this.PendingTaskGridView.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn()
        {
            Caption = " ",
            Name = "AttendanceImage",
            FieldName = "AttendanceImage",
            Visible = true,
            UnboundType = DevExpress.Data.UnboundColumnType.Object,
            VisibleIndex = 0,
            Width = 25,
            MaxWidth = 25,
            ToolTip = "IVR Attendance",
            ColumnEdit = pictureEdit
        });
    }

}  

CustomUnboundColumnData事件:

private void PendingTaskGridView_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
    {
        if (e.Column.FieldName.Equals("StatusImage", StringComparison.InvariantCultureIgnoreCase))
        {
            SetStatusImage(e);
        }
        if (e.Column.FieldName.Equals("PunctualityImage", StringComparison.InvariantCultureIgnoreCase))
        {
            SetPunctualityImage(e);
        }
        if (e.Column.FieldName.Equals("AttendanceImage", StringComparison.InvariantCultureIgnoreCase))
        {
            SetAttendanceImage(e);
        }
    }

在某种条件下设置单元格的图像方法:

private void SetStatusImage(CustomColumnDataEventArgs e)
    {
        DataModel.TaskToConfirm task = e.Row as DataModel.TaskToConfirm;            

        if (task.BillingConfirmed)
            e.Value = imageListGrid.Images[0];

        else
            e.Value = imageListGrid.Images[1];
    }



private void SetPunctualityImage(CustomColumnDataEventArgs e)
    {
        DataModel.TaskToConfirm task = e.Row as DataModel.TaskToConfirm;

        if (task.PunctualityStatus == (int)ePunctuality.Extra)
            e.Value = CareGiver.Properties.Resources.Cancelled_Image;
        else if (task.PunctualityStatus == (int)ePunctuality.Less)
            e.Value = CareGiver.Properties.Resources.Update_CSD;
        else
            e.Value = CareGiver.Properties.Resources.Category_Green;

    }



private void SetAttendanceImage(CustomColumnDataEventArgs e)
    {
        DataModel.TaskToConfirm task = e.Row as DataModel.TaskToConfirm;

        if (task.AttendanceType == 2)
            e.Value = imageList1.Images["auto"];
        else if (task.AttendanceType == 1)
            e.Value = imageList1.Images["manual"];
        else if (task.AttendanceType == 0)
            e.Value = imageList1.Images["default"];


    }