2012-08-16 76 views
1

我的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> 

回答

0

我解決了它:

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 [email protected]"; 
      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"); 
    } 
0

我不知道是什麼樣的控制grdPersonel1的,但你應該看看,如果是的SelectedItem一個字符串...如果不是,也許你需要的SelectedValue或SelectedItem.Text(東西,有你想要的字符串在名稱字段中)

+0

的SelectedItem或的SelectedValue和他們沒有不顯示。文本 – mhshojaee 2012-08-16 19:38:01

0

我假設grdPersonnel1是您的DataGrid,即項目是網格是Personnel類型並且該Personnel具有Name屬性。如果這是正確的,試試這個:

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

是grdPersonnel1是我的DataGrid,我用這個片段,但有錯誤:對象引用未設置爲對象的實例。 – mhshojaee 2012-08-17 15:02:09