从数据表中的 sql 获取值后,我需要将 byte[] 转换为图像,并需要在数据网格中的视图之后添加到数据表中
我正在尝试在 DataGrid 中查看数据库中的值。我有一个名为签名的列,其中包含存储为字节数组的图像。但是,我不知道如何在 DataGrid 中查看这些图像。
这是我的代码:
DataTable dt = new DataTable("CustomerInformations");
string Cn = "Data Source=\\SQLEXPRESS;Initial Catalog=Export;Integrated Security=true;";
SqlConnection con = new SqlConnection(Cn);
con.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("select [Date],[CustomerName],[Amount],[Bank] ,[Signature] ,[IMEINumber] from PaymentReceipt where ReceiptID = 44", con);
sqlDa.Fill(dt);
con.Close();
this.dataGrid1.DataContext = dt;
数据库中有一个名为 signature
的列。它的数据类型是varbinary
。
在将数据表分配给 datagrid1
之前,我需要将签名(byte[]
)转换为图像。
这是我将 byte[]
转换为图像的代码:
foreach (DataRow dr in dt.Rows)
{
byte[] bytes = (byte[])dr[4];
MemoryStream stream = new MemoryStream(bytes);
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.EndInit();
return image;
}
我不知道如何在数据表的签名字段中添加返回图像。你能帮我吗?
这是我的 XAML 文件
<Grid>
<DataGrid AutoGenerateColumns="true" Name="dataGrid1" ItemsSource="{Binding}" Height="260" Width="705">
</DataGrid>
</Grid>
I am trying to view values from the database in a DataGrid. I have a column called signature, which contains images stored as byte arrays. However, I don't know how to view these images in the DataGrid.
Here is my code:
DataTable dt = new DataTable("CustomerInformations");
string Cn = "Data Source=\\SQLEXPRESS;Initial Catalog=Export;Integrated Security=true;";
SqlConnection con = new SqlConnection(Cn);
con.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("select [Date],[CustomerName],[Amount],[Bank] ,[Signature] ,[IMEINumber] from PaymentReceipt where ReceiptID = 44", con);
sqlDa.Fill(dt);
con.Close();
this.dataGrid1.DataContext = dt;
There is a column called signature
in the database. Its datatype is varbinary
.
Before I assigned the datatable to datagrid1
I need to convert the signature(byte[]
) to an image.
Here is my code for converting a byte[]
to an image:
foreach (DataRow dr in dt.Rows)
{
byte[] bytes = (byte[])dr[4];
MemoryStream stream = new MemoryStream(bytes);
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.EndInit();
return image;
}
I don't know how to add the return image in the signature field in datatable. Can you please help me?
Here is my XAML file
<Grid>
<DataGrid AutoGenerateColumns="true" Name="dataGrid1" ItemsSource="{Binding}" Height="260" Width="705">
</DataGrid>
</Grid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你不能用自动生成列来做到这一点。您需要将图像列绑定到 ValueConverter。这将从文件转换为图像,但它应该可以帮助您入门。
http://social.msdn.microsoft.com/Forums/en/wpf/thread/ea1fd63b-738c-40ca-850e-994eb21dccea
您可能需要一个模板化列来托管图像查看器。
I don't think you can do it with AutoGenerate columns. You need to bind the image column to a ValueConverter. This converts from file to an image but it should get you started.
http://social.msdn.microsoft.com/Forums/en/wpf/thread/ea1fd63b-738c-40ca-850e-994eb21dccea
You may need a templated column to host an image viewer.