gpt4 book ai didi

c# - 在DataGridView中使用DataTable显示小图(图标)

转载 作者:行者123 更新时间:2023-11-30 23:22:22 27 4
gpt4 key购买 nike

[编辑]

我想使用 DataTable 在 Datagridview 中使用图像。

RadioButton 只是这篇文章的一种简单问题格式。

让我为此澄清一下。

如何使用绑定(bind)样式在 datagridview 上添加这个“图像”或那个“图像”?

(因为,

我想,它比正常方式更快。我做了500行图文——都是16*16的,都是短字。如果我重画它,我花了 20 秒。二十!!!!这是无稽之谈。我已经尝试过“双缓冲区”解决方法,但没有比这更好的了。)


我编写了代码。

它只是 DataGridView 上 RadioButton 的起点。

到目前为止一切顺利。

我在上面制作了五张图片。

像这样。

enter image description here

        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 绑定(bind)样式”得到相同的结果?

我只有错误“需要正确的行数据类型”。

我第二次尝试的代码是;

        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[] 数据类型的 DataColumnDataGridViewImageColumnDataGridVide 中。因此,您首先需要将图像转换为字节数组,然后将它们设置为具有字节数组数据类型的数据列的值:

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 Column 绘制 RadioButton 而不是 CheckBox

使用图像类型不适合单选按钮。适合这种DataColumn 的数据类型是bool,适合DataGridViewColumn 的列是DataGridViewCheckBoxColumn。然后,您可以处理 DataGridViewCellPainting 事件,并使用 RadioButtonRenderer 以这种方式绘制单选按钮:

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

关于c# - 在DataGridView中使用DataTable显示小图(图标),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38804630/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com