在DataGridView上使用DataTable显示小图像(图标) [英] Using DataTable at DataGridView to display small images(Icon)

查看:79
本文介绍了在DataGridView上使用DataTable显示小图像(图标)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用DataTable在Datagridview上使用图像.

I'd like to use image at Datagridview using DataTable.

RadioButton只是本文的一种简单问题格式.

RadioButton is just a simple question format for this post.

让我为此澄清一下.

如何使用绑定样式在datagridview上添加此图像"或该图像"?

(因为

我认为,这比常规方法要快.我制作了500行图像和文字-它们都是16 * 16和简短的单词. 如果我重画它,我花了 20秒.二十!这是胡说八道.我尝试了双缓冲区"解决方法,但没有更好的方法.)

I thought, it is faster then the normal ways. I made 500 lines of images and texts -both of them are 16*16 and short words. If I redraw it, it took me 20 seconds. Twenty !!!! This is a nonsense. I have tried "Double buffer" workarounds, but nothing was better.)

我写了密码.

这只是DataGridView上RadioButton的起点.

It is just a start point for RadioButton on DataGridView.

到目前为止一切都很好.

So far so good.

我在上面制作了五张图像.

I made five images on it.

喜欢这个.

        dataGridView1.RowCount = 5;
        dataGridView1.Rows[0].Cells[0].Value = Properties.Resources.WhiteBall;
        dataGridView1.Rows[1].Cells[0].Value = Properties.Resources.BlueBall;
        dataGridView1.Rows[2].Cells[0].Value = Properties.Resources.WhiteBall;
        dataGridView1.Rows[3].Cells[0].Value = Properties.Resources.WhiteBall;
        dataGridView1.Rows[4].Cells[0].Value = Properties.Resources.WhiteBall;

问题.

如何使用"DataTable绑定样式"获得相同的结果?

How can I make the same result using "DataTable binding style" ?

我只有错误行中需要正确的数据类型".

I had only error "Needs correct DataType at Rows".

我第二次尝试的代码是;

My codes for 2nd try is;

        DataTable myTable = new DataTable();
        DataColumn myCoulmn = new DataColumn();
        myCoulmn.DataType = Type.GetType("System.Drawing.Bitmap");
        myTable.Columns.Add(myCoulmn);
        DataRow myRow = myTable.NewRow();
        myRow[0] = Properties.Resources.WhiteBall;
        myRow[1] = Properties.Resources.BlueBall;
        myRow[2] = Properties.Resources.WhiteBall;
        myRow[3] = Properties.Resources.WhiteBall;
        myRow[4] = Properties.Resources.WhiteBall;
        myTable.Rows.Add(myRow);
        dataGridView1.DataSource = myTable;

请帮助.

推荐答案

使用DataTable在DataGridView中显示图像

要使图像列使用DataTable,应使用具有byte[]数据类型的DataColumnDataGridVide中的DataGridViewImageColumn.因此,您首先需要将图像转换为字节数组,然后将它们设置为具有字节数组数据类型的数据列的值:

To have an image column using a DataTable you should use a DataColumn with byte[] data type and a DataGridViewImageColumn in DataGridVide. So you first need to convert images to byte array then set them as value of a data column with byte array data type:

var imageConverter = new ImageConverter();
var b1 = (byte[])imageConverter.ConvertTo(Properties.Resources.Image1, typeof(byte[]));
var b2 = (byte[])imageConverter.ConvertTo(Properties.Resources.Image2, typeof(byte[]));
var dt = new DataTable();
dt.Columns.Add("Column1", typeof(byte[]));
dt.Rows.Add(b1);
dt.Rows.Add(b2);
this.dataGridView1.DataSource = dt;


为bool列而不是CheckBox绘制RadioButton

使用图像类型不适用于单选按钮.此类DataColumn的合适数据类型是bool,列的合适DataGridViewColumnDataGridViewCheckBoxColumn.然后,您可以处理DataGridViewCellPainting事件,并使用RadioButtonRenderer这样绘制单选按钮:

Using image type is not suitable for a radio button. A suitable data type for such DataColumn is bool and a suitable DataGridViewColumn for column is DataGridViewCheckBoxColumn. Then you can handle CellPainting event of DataGridView and draw a radio button using RadioButtonRenderer this way:

private void Form1_Load(object sender, EventArgs e)
{
    var dt = new DataTable();
    dt.Columns.Add("Column1", typeof(bool));
    dt.Rows.Add(false);
    dt.Rows.Add(true);
    this.dataGridView1.DataSource = dt;
    this.dataGridView1.CellPainting += dataGridView1_CellPainting;
}

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex == 0 && e.RowIndex >= 0)
    {
        var value = (bool?)e.FormattedValue;
        e.Paint(e.CellBounds, DataGridViewPaintParts.All &
                                ~DataGridViewPaintParts.ContentForeground);
        var state = value.HasValue && value.Value ?
            RadioButtonState.CheckedNormal : RadioButtonState.UncheckedNormal;
        var size = RadioButtonRenderer.GetGlyphSize(e.Graphics, state);
        var location = new Point((e.CellBounds.Width - size.Width) / 2,
                                    (e.CellBounds.Height - size.Height) / 2);
        location.Offset(e.CellBounds.Location);
        RadioButtonRenderer.DrawRadioButton(e.Graphics, location, state);
        e.Handled = true;
    }
}

这篇关于在DataGridView上使用DataTable显示小图像(图标)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆