用图像替换DataGridView单词和数字

时间:2008-11-04 15:15:06

标签: c# datagridview

我正在创建一个应用程序,它在DataGridView中显示一些消息及其方向。我想用图片替换一些列内容。例如,我想用绿色箭头(某些.jpg图像)替换代表来电的数字0。

有谁知道如何实现这一目标?

谢谢!

3 个答案:

答案 0 :(得分:1)

我们将图像作为BMP文件存储在资源文件中。然后,我们在DataGridView中处理CellFormatting事件,如下所示:

    private void messageInfoDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        // Is this the correct column? (It's actually a DataGridViewImageColumn)
        if (messageInfoDataGridView.Columns[e.ColumnIndex] == messageSeverityDataGridViewTextBoxColumn)
        {
            // Get the row we're formatting
            DataGridViewRow row = messageInfoDataGridView.Rows[e.RowIndex];
            // Get the enum from the row.
            MessageSeverity severity = ((MessageInfo)row.DataBoundItem).MessageSeverity;
            Bitmap cellValueImage;
            // Map the enumerated type to an image...
            // SeverityImageMap is a Dictionary<MessageSeverity,Bitmap>.
            if (ReferenceTables.SeverityImageMap.ContainsKey(severity))
                cellValueImage = ReferenceTables.SeverityImageMap[severity];
            else
                cellValueImage = Resources.NoAction;

            // Set the event args.
            e.Value = cellValueImage;
            e.FormattingApplied = true;
        }
    }

答案 1 :(得分:0)

GridViews能够使用图像字段而不是数据绑定字段。这听起来好像可以解决问题。

答案 2 :(得分:0)

查看创建自定义datagridviewCell datagridviewColumn和/或dataGridViewEditingControl。