且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将base64string转换为图像并显示在datagridview上

更新时间:2023-11-14 19:39:10

我尝试过",
dgvProducts.Columns.Add("Column","image");


这只是为我提供了一个新的新列,但行中没有数据?"


好吧...是的...会的.它会添加一列,但不会向正在显示的行中添加任何数据-因此该列为空.
确切地说,如何添加数据将取决于DVG的数据源是什么,以及它来自何处.例如,如果要通过DataTable从数据库加载它,则需要向DT中添加一列,而不是DGV-然后为新列填充行数据.同样,如果您使用的是一个类实例的Collection,则需要向该类添加一个public属性,并将每个实例设置为Image本身.然后,将数据绑定到DVG时,所有这些都会自动发生.
如果要在代码中手动将行添加到DGV,则按照显示的方式添加列,并在相应的列名称下将图像添加到每一行.


"this just gives me new new column with not data in the rows?"


Well...yes...it will. It adds a column, but it doesn''t add any data to the rows that are being displayed - so the column is empty.
Exactly how you would add the data is going to depend on what your data source for thr DVG is, and where it comes from. If you are loading it from a DB via a DataTable for example, then you would need to add a column to the DT, rather than the DGV - and then populate the rows data for the new column. Similarly, if you are using a Collection of instances of a class, then you need to add a public property to the class and set each instance to the Image itself. Then it will all happen automatically when you bind the data to the DVG.
If you are manually adding rows to the DGV in your code, then add the column as you have shown, and add the Image to each row under the appropriate column name.


您并没有真正尝试任何事情.要显示图像,您需要一个能够显示图像的单元格:
DataGridViewImageCell类(System.Windows.Forms) [ DataGridViewImageColumn类(System.Windows.Forms) [ 魔术字符串-***,免费的百科全书 [魔术数(编程)—***,免费的百科全书 [反模式-***,免费的百科全书 [
You did not really try anything. To show an image, you need a cell of the type capable of showing an image:
DataGridViewImageCell Class (System.Windows.Forms)[^].

You can have a whole column of such cells: DataGridViewImageColumn Class (System.Windows.Forms)[^].

Your major problem is hard-coding everything around: "Picture", "Columns". I hope it''s only for illustration. But if you do it in a real application, you cannot get well maintainable code. Please see:
Magic string — Wikipedia, the free encyclopedia[^],
Magic number (programming) — Wikipedia, the free encyclopedia[^],
Anti-pattern — Wikipedia, the free encyclopedia[^].

—SA