1

dataGridにいくつかの行があり、そのうちの1つを選択してそのデータを印刷すると、データベースの最初の画像のみが印刷されますが、選択した行の画像のみを印刷する必要があります。このスニペットにはcmd.CommandText ...、How改善できますか?

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>
4

3 に答える 3

0

grdPersonel1の種類はわかりませんが、SelectedItemが文字列であるかどうかを確認する必要があります。そうでない場合は、SelectedValueまたはSelectedItem.Text([名前]フィールドに必要な文字列が含まれているもの)が必要です。

于 2012-08-16T19:28:54.583 に答える
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 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");
    }
于 2012-08-19T00:20:31.580 に答える
0

grdPersonnel1 は DataGrid であり、項目はグリッドが Personnel タイプであり、Personnel には Name プロパティがあると仮定します。これが正しい場合は、次のことを試してください。

cmd.CommandText = "Select Picture from Personnels where Name= " + (grdPersonnel1.SelectedItem as Personnel).Name;
于 2012-08-17T07:24:22.083 に答える