DataGrid行选择并打印其数据

我的dataGrid中有一些行,当我选择其中一行并打印它的数据时,只打印第一张数据库图像,而我只需要打印所选行的图像,这段代码在cmd.CommandText上有错误…,如何我能改进吗?

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.Connection = con; con.Open(); cmd.CommandText = "Select Picture from Personnels where Name= " + grdPersonnel1.SelectedItem; 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"); }   

我不知道grdPersonel1是什么类型的控件,但是如果SelectedItem是一个字符串,你应该看看…如果不是,你可能需要SelectedValue或SelectedItem.Text(你希望在Name字段中包含字符串的东西)

我假设grdPersonnel1是您的DataGrid,项目是该网格属于Personnel类型,Personnel具有Name属性。 如果这是正确的,请尝试这样做:

 cmd.CommandText = "Select Picture from Personnels where Name= " + (grdPersonnel1.SelectedItem as Personnel).Name; 

我解决了它:

 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"); }