如何在DataGridView中对图像单元使用数据绑定 [英] How to use Databinding with imagecells in DataGridView

查看:103
本文介绍了如何在DataGridView中对图像单元使用数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个关系数据库,我想在DataGridView中显示.
在该数据库中,我有2列,某种标志"(例如,一个进程标志,尚未启动-1,正在执行0,并且已完成1个进程)-我想要显示的这些标志DataGridView作为简单图像.


我目前的解决方案很难看.

由SQLDataReader捕获的每一行都通过一个委托放入一个方法中,该方法检查有关标志的行,并将该行(包括从图像列表中选择的图像)放入对象列表中.
然后,此对象列表将新行添加到数据库中.

这是我当前的SQLDataReader:

Hi there,

I have a relational database, that I want to display in a DataGridView.
In that db, I have 2 Columns, some kind of a ''flag'' (e.g. a processflag, -1 is not started yet, 0 is in progress, and 1 -process completed) - these flag I want do display in my DataGridView as an simple image.


My current solution is quite ugly.

Each Row, captured by a SQLDataReader is put through a Delegate into a method, which checks the row about the flag, and puts the row (incl. the image, selected of a imagelist) into object-List.
Afterwards, this object-List adds a new Row into the database.

This is my current SQLDataReader:

SqlCommand cmd = new SqlCommand(strQuery.ToString(), dbCls.sqlConnection);
                   SqlDataReader dr = cmd.ExecuteReader();
                   while (dr.Read())
                   {
                       valuesForDgV = new List<string>();
                       valuesForDgV.Add(dr[0].ToString());
                       valuesForDgV.Add(dr[1].ToString());
                       valuesForDgV.Add(dr[2].ToString());
                       valuesForDgV.Add(dr[3].ToString());
                       valuesForDgV.Add(dr[4].ToString());
                       valuesForDgV.Add(dr[5].ToString());
                       valuesForDgV.Add(dr[6].ToString());
                       valuesForDgV.Add(dr[7].ToString());
                       valuesForDgV.Add(dr[8].ToString());
                       //valuesForDgV.Add(dr[9].ToString());
                       //valuesForDgV.Add(dr[10].ToString());
                       fillDgVDelegate dgvDelegate = new fillDgVDelegate(addData2DgV);
                       dgv_monthView.Invoke(dgvDelegate, valuesForDgV);
                   }
                   dr.Close();





private void addData2DgV(List<string> valueArray)
{
object flgProcess, flgArc = null;

            //select the images for the column in the dgv
            if(valueArray[8] == "0" || valueArray[8] == short.MaxValue.ToString())
                flgProcess = imageList1.Images[0];
            else
                flgProcess = imageList1.Images[1];

            if (valueArray[7] == "0" || valueArray[7] == short.MaxValue.ToString())
                flgArc = imageList1.Images[0];
            else
                flgArc = imageList1.Images[1];

            dgv_monthView.Rows.Add(valueArray[0], betrag, valueArray[2], valueArray[3], valueArray[4], valueArray[5], flgWerbung, flgTermin, flgRechnung);




我不知道这不是计算机性能的问题,但是我猜想经过20行之后,性能会越来越差.

因此,我尝试使用SQLDataAdapter,DataTable和BindingSource.

但是我不能在DataGridView中使用Image.该列仅允许使用Int-Values


我现在需要的是提示,技巧,可能是代码片段,我如何才能更好地解决此问题.


很抱歉,我的英语不好,希望您能理解我的问题.

预先感谢,
麦克斯



我希望这些屏幕截图可以更好地显示我的问题:

这是我的数据库的样子:

数据库屏幕截图


这就是我要如何将突出显示的标志"显示为图像,被视为网格的最后三列

DataGridView

因此,我希望"0"值成为网格中的停车标志,而1-Value是选中的标志.




i don''t know if it''t an issue of the performance of my computer, but after, i guess 20 rows, the performance is going worse and worse.

So i''ve tried working with SQLDataAdapter, DataTable and BindingSource.

But I cannot use the Image in my DataGridView. the Column allows only Int-Values


What I need now, is a hint, a tip, may be a codesnippet, how i can solve this problem better.


I''m sorry, for my bad english and I hope you can understand my problem.

Thanks in advance,
Max



I hope these screenshots will better show my problem:

this is what my database looks like:

Databasescreenshot


and this is, how I want to display the highlighted "flags" as an image, seen as the last three columns in my grid

DataGridView

So, I want the ''0'' Value to be an stop sign in the grid, and the 1-Value is the checked-sign.

推荐答案

好,我找到了一个简单的解决方案,可能不是 perfekt 的完美方法,但至少又快又脏.

在收集数据库中的数据之前,我将数据网格的可见性值设置为false

Ok, i''ve found a simple solution, may be it''s not the perfekt perfect way, but at least quick and dirty.

Befor the data from the database is being collected, I set the visibility-value of my datagrid to false

DataGridView1.Visible = false;



在所有收集过程之后,我将该值重新设置为"true".

现在我的性能问题消失了..

但是非常感谢您的帮助,他们把我推到了正确的搜索方式!



after all collection processes I set the value back to ''true''.

Now my Performance problems are gone..

But thanks a lot for your help, they pushed me to the right way to search!


这篇关于如何在DataGridView中对图像单元使用数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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