Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET SQL-Server WPF printing , +
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)
            {
 
<pre>
            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");
        }
    }</pre>
Posted 15-Aug-12 7:00am
Edited 15-Aug-12 7:01am
v2
Comments
Wes Aday at 15-Aug-12 12:04pm
   
You are not selecting your image based on any kind of identifier.
Mohammad Hossein Shojaei at 15-Aug-12 12:07pm
   
What type of identifier?
Wes Aday at 15-Aug-12 12:12pm
   
Any kind. Name, number, index. Whatever you are using in your database.
Mohammad Hossein Shojaei at 15-Aug-12 12:15pm
   
If I use Name or ID, have you guidance or link?
Wes Aday at 15-Aug-12 12:17pm
   
http://www.w3schools.com/sql/sql_where.asp
Mohammad Hossein Shojaei at 15-Aug-12 12:28pm
   
I used this:
cmd.CommandText = "SELECT Picture FROM Database Where Name=@Name";

But error: Must declare the scalar variable "@Name".
Wes Aday at 15-Aug-12 12:32pm
   
"SELECT Picture FROM Database Where Name='" + whereeveritisthatNameiscomingfrom + "'" and you might want to do some research on parameterized queries.
Mohammad Hossein Shojaei at 15-Aug-12 12:44pm
   
I found this:
...= "SELECT Picture FROM DataBase" + " where Name=" + (MyClass(grdPersonnel.SelectedItem)).Name;

but it's don't work!
Wes Aday at 15-Aug-12 12:45pm
   
You have to use single quotes for text items in SQL
Kenneth Haugland at 15-Aug-12 12:18pm
   
When I read the title it though ok, but when I saw the code I was lost... What are you trying to achive?
Mohammad Hossein Shojaei at 15-Aug-12 13:49pm
   
As i said: When i select one of DataGrid rows and print it's data, only first image of database will print while i only need to print selected DataGrid row image.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

  Permalink  
Comments
ArianDiba at 19-Aug-12 11:16am
   
This was good snippet for me,
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 325
2 ProgramFOX 265
3 Maciej Los 245
4 Andreas Gieriet 200
0 OriginalGriff 465
1 Sergey Alexandrovich Kryukov 275
2 ProgramFOX 265
3 Maciej Los 245
4 Andreas Gieriet 200


Advertise | Privacy | Mobile
Web03 | 2.8.150331.1 | Last Updated 19 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100