내 DataGrid에 일부 행이 있는데 그 중 하나를 선택하여 데이터를 인쇄하면 선택한 행의 이미지 만 인쇄해야하는 반면 데이터베이스의 첫 번째 이미지 만 인쇄됩니다. cmd.CommandText ...에 오류가 발생했습니다. 어떻게 개선 할 수 있습니까?DataGrid 행 선택 및 데이터 인쇄
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");
}
<Image VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="Fill" Name="PictureBox"
Source="{Binding Picture}" DataContext="{Binding Path=SelectedItem, ElementName=grdPersonnel1}" Opacity="2">
</Image>
의 selectedItem 또는 SelectedValue를하고 그들 중 누구도는 .text – mhshojaee