DataGrid Selection_Changed从SQL检索图像 [英] DataGrid Selection_Changed retrieve image from SQL
本文介绍了DataGrid Selection_Changed从SQL检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我保存的图像转换为SQL但是当选择DataGrid行,图像不retreve到图像框中.
I saved image into sql but when select DataGrid row, image does not retreve in to Image box.
private void grdPersonnel1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\programing\prOjects\WPF\Data\Data.mdf;Integrated Security=True;User Instance=True";
con.Open();
SqlCommand sc = new SqlCommand("select Count(*) from Table where Name=@Name", con);
sc.Parameters.AddWithValue("@Name", txtName.Text);
int count = (int)sc.ExecuteScalar();
sc.CommandText = " Select Picture from Table Where Name=@Name ";
byte[] data = (byte[])sc.ExecuteScalar();
string strfn = Convert.ToString(DateTime.Now.ToFileTime());
FileStream fs = new FileStream(strfn, FileMode.CreateNew, FileAccess.Write);
fs.Write(data, 0, data.Length);
fs.Flush();
fs.Close();
//Photo = (grdPersonnel1[i, j].Value as DataGridViewImageColumn).Image;
con.Close();
}
<DataGrid Margin="3,33,3,358" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" HeadersVisibility="All" HorizontalGridLinesBrush="Black"
Name="grdPersonnel1" AlternatingRowremoved="LightPink" VerticalGridLinesBrush="LightBlue" HorizontalScrollBarVisibility="Auto" FontFamily="Arial" FontSize="12" FontWeight="Bold" IsTextSearchEnabled="True" SelectionChanged="grdPersonnel1_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=No}" Header="No" Width="Auto"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="Auto"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Path=Picture}" Header="Picture" Width="Auto"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<Image Grid.Column="6" Margin="49,6,44,0" Grid.RowSpan="7" Name="Picture" DataContext="{Binding Path=SelectedItem, ElementName=grdPersonnel1}"></Image>
推荐答案
您的共享代码段在您的工作流程中不太清楚,但是可以肯定的是:
Your shared snippet is not too clear on your workflow, but this for sure:
<DataGridTextColumn Binding="{Binding Path=Picture}" Header="Picture" Width="Auto"></DataGridTextColumn>
您正在使用特定于文本的列来显示图像,这可能是这里的问题.
You are using a Text specific column to display image which might be the issue here.
我使用了此代码段,但是当我选择datagrid行时,图片不会显示在imageBox中:
I used this snippet but when i select datagrid row, picture does not display in the imageBox:
<DataGridTemplateColumn Header="Picture" Width="45" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<Image Source="{Binding Path=Picture}" Width="100" Height="50" Stretch="Uniform" ">
</Image>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
关于此代码段,当我单击datagrid的每一行时,仅在图片框中显示datagrid的第一行的第一张图片:
有谁回答我吗?
谢谢,
About This snippet, when i click on every row of datagrid, only displays first picture of first row of datagrid in the picture box:
Is there anyone answer to me?
thanks,
private void grdPersonnel1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGridViewBand dgr = (grdPersonnel1.SelectedItem as DataGridViewBand);
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\programing\prOjects\WPF\Data\Data.mdf;Integrated Security=True;User Instance=True";
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter sqa = new SqlDataAdapter("Select Picture from Personnels", con);
sqa.Fill(ds);
con.Close();
byte[] data = (byte[])ds.Tables[0].Rows[0][0];
MemoryStream strm = new MemoryStream();
strm.Write(data, 0, data.Length);
strm.Position = 0;
System.Drawing.Image img = System.Drawing.Image.FromStream(strm);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
bi.StreamSource = ms;
bi.EndInit();
PictureBox.Source = bi;
}
这篇关于DataGrid Selection_Changed从SQL检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文