【问题标题】:DataGrid row selection and printing its dataDataGrid 行选择并打印其数据
【发布时间】:2012-08-16 19:17:38
【问题描述】:

我的dataGrid中有一些行,当我选择其中一个并打印它的数据时,只会打印数据库的第一个图像而我只需要打印所选行的图像,这个sn-p在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");
        }

<Image VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="Fill" Name="PictureBox"
                   Source="{Binding Picture}" DataContext="{Binding Path=SelectedItem, ElementName=grdPersonnel1}" Opacity="2">
</Image>

【问题讨论】:

    标签: c# sql wpf


    【解决方案1】:

    我不知道 grdPersonel1 是什么类型的控件,但是您应该查看 SelectedItem 是否为字符串...如果不是,您可能需要 SelectedValue 或 SelectedItem.Text(在名称字段中包含您想要的字符串的东西)

    【讨论】:

    • SelectedItem 或 SelectedValue 并且没有一个不显示 .Text
    【解决方案2】:

    我假设 grdPersonnel1 是您的 DataGrid,项目是网格属于 Personnel 类型,并且 Personnel 具有 Name 属性。如果这是正确的,试试这个:

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

    【讨论】:

    • 是的 grdPersonnel1 是我的 DataGrid,我使用了这个 sn-p 但出现错误:对象引用未设置为对象的实例。
    【解决方案3】:

    我解决了:

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

    【讨论】:

      猜你喜欢
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      • 1970-01-01
      相关资源
      最近更新 更多