【问题标题】:Image and value into a GridView cell将图像和值放入 GridView 单元格
【发布时间】:2017-04-06 15:01:24
【问题描述】:

我有一个通过 Linq 绑定到数据源的 XtraGridview,当我选中某个复选框时,我需要将图像设置到单元格中以及它们已经拥有的值。 现在,当我选中复选框时,我将图像设置在单元格中就好了,但消除了单元格值(数据)。 在 CustomDrawCell 事件中我会这样做

private void gridView_GD_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
        {            
                GridView view = sender as GridView;
                string evento1 = Convert.ToString(view.GetRowCellValue(e.RowHandle, "Eve1"));

            if (CVariables.Ficon_estado == 1)
            {
                if (evento1 == "06" || evento1 == "15")
                {
                    if (e.Column.FieldName == "G1")
                    {
                        e.Handled = true;
                        Point pos = CalcPosition(e, imageCollection_16.Images[1]);
                        e.Graphics.DrawImage(imageCollection_16.Images[1], pos);
                        view.Columns["G1"].AppearanceCell.BackColor = Color.Transparent;
                        view.Columns["G1"].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;                        
                    }
                }
            }
            else
            {
                view.Columns["G1"].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            }
}

 private Point CalcPosition(RowCellCustomDrawEventArgs e, Image img)
        {
            Point p = new Point();
            p.X = e.Bounds.Location.X + (e.Bounds.Width - (img.Width * 3)) / 2;
            p.Y = e.Bounds.Location.Y + (e.Bounds.Height - img.Height) / 2;
            return p;
        }

I post an image to illustrate what i want

【问题讨论】:

    标签: c# winforms gridview devexpress


    【解决方案1】:

    由于您将 e.Handled 属性设置为 true,因此单元格文本会消失。设置此选项时,不会调用默认绘制机制。您仍然可以使用 e.Appearance.DrawString 方法手动绘制单元格文本。

    e.Appearance.DrawString(e.Cache, e.DisplayText, e.Bounds);
    

    另一种解决方案是使用RepositoryItemTextEdit.ContextImage 显示图像。也就是说,您可以创建两个具有不同上下文图像的RepositoryItemTextEdits,并在GridView.CustomRowCellEdit 事件处理程序中有条件地将它们分配给网格单元。

    【讨论】:

      猜你喜欢
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多