选择dataGrid行并打印行数据 [英] Selecting dataGrid row and print row data
本文介绍了选择dataGrid行并打印行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的dataGrid中有一些行,当我选择其中的一行并打印其数据时,将仅打印数据库的第一张图像,而我只需要打印选定的行图像.
There are some rows in my dataGrid, when i select one of them and print it''s data, only first image of database will print while i only need to print selected row image.
<Image VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="Fill" Name="PictureBox"
Source="{Binding Picture}" DataContext="{Binding Path=SelectedItem, ElementName=grdPersonnel1}" Opacity="2">
</Image>
private void Print_Click(object sender, RoutedEventArgs e)
{
System.Windows.Controls.PrintDialog printDialog = new System.Windows.Controls.PrintDialog();
if (printDialog.ShowDialog() == true)
{
DrawingVisual dv = new DrawingVisual();
var dc = dv.RenderOpen();
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Database\Data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlCommand cmd = new SqlCommand();
BitmapImage bmp = new BitmapImage();
cmd.CommandText = "SELECT Picture FROM DataBase";
cmd.Connection = con;
con.Open();
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.BeginInit();
bmp.StreamSource = new System.IO.MemoryStream((Byte[])cmd.ExecuteScalar());
bmp.EndInit();
dc.DrawImage(bmp, new Rect(140, 170, 150, 150));
-
dc.DrawText(new FormattedText("Name:", CultureInfo.GetCultureInfo("en-us"), FlowDirection,
new Typeface(new System.Windows.Media.FontFamily("Courier New"), FontStyles.Normal, FontWeights.Bold,
FontStretches.Normal), 12, System.Windows.Media.Brushes.Black), new System.Windows.Point(700, 180));
dc.DrawText(new FormattedText(txtName.Text, CultureInfo.GetCultureInfo("en-us"), FlowDirection,
new Typeface(new System.Windows.Media.FontFamily("Courier New"), FontStyles.Normal, FontWeights.Normal,
FontStretches.Normal), 11, System.Windows.Media.Brushes.Black), new System.Windows.Point(550, 180));
dc.Close();
printDialog.PrintVisual(dv, "Print");
}
}
推荐答案
我解决了.
I solved it.
private void Print_Click(object sender, RoutedEventArgs e)
{
System.Windows.Controls.PrintDialog printDialog = new System.Windows.Controls.PrintDialog();
if (printDialog.ShowDialog() == true)
{
DrawingVisual dv = new DrawingVisual();
var dc = dv.RenderOpen();
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Database\Database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlCommand cmd = new SqlCommand();
BitmapImage bmp = new BitmapImage();
cmd.CommandText = "Select Picture from Database where Code=@Code";
cmd.Parameters.Add("@Code", SqlDbType.NVarChar, 30);
cmd.Parameters["@Code"].Value = this.txtCode.Text;
cmd.Connection = con;
con.Open();
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.BeginInit();
bmp.StreamSource = new System.IO.MemoryStream((Byte[])cmd.ExecuteScalar());
bmp.EndInit();
dc.DrawImage(bmp, new Rect(140, 170, 150, 150));
dc.DrawText(new FormattedText("Name:", CultureInfo.GetCultureInfo("en-us"), FlowDirection,
new Typeface(new System.Windows.Media.FontFamily("Courier New"), FontStyles.Normal, FontWeights.Bold,
FontStretches.Normal), 12, System.Windows.Media.Brushes.Black), new System.Windows.Point(700, 180));
dc.DrawText(new FormattedText(txtName.Text, CultureInfo.GetCultureInfo("en-us"), FlowDirection,
new Typeface(new System.Windows.Media.FontFamily("Courier New"), FontStyles.Normal, FontWeights.Normal,
FontStretches.Normal), 11, System.Windows.Media.Brushes.Black), new System.Windows.Point(550, 180));
dc.Close();
printDialog.PrintVisual(dv, "Print");
}
这篇关于选择dataGrid行并打印行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文