在ReportViewer中显示富文本框格式

问题描述

我有一个名为rtb的富文本框,其数据插入到这样的sql表中:

private void insertdata(object sender,RoutedEventArgs e)
{       
    string name = nameofstudent.Text;
    string father = fatherofstudent.Text;
               
    string id = stud_id.Text;
    string gender;
    if (male.IsChecked == true)
    {
        gender = "M";
    }
    else
        gender = "F";
    
    var richTextBoxContent = new TextRange(this.rtb.Document.ContentStart,this.rtb.Document.ContentEnd);

    string Place = richTextBoxContent.Text;

    connect();
    con.open();

    string saved = "insert into student_details (Student_ID,Student_Name,Father_Name,Gender,Address)values('" 
    + id + "','" + name + "','" + father + "','" + gender + "','" + Place + "')";
               
    sqlCommand cmd = new sqlCommand(saved,con);
                                    
    cmd.ExecuteNonQuery();
    con.Close();

    MessageBox.Show("record is added");
}

要更改RTF格式的格式,我有一个按钮,可以将任何选定的单词更改为粗体。

private void changefont(object sender,RoutedEventArgs e)
{
    TextSelection text = rtb.Selection;
    if (!text.IsEmpty)
    {
        text.ApplyPropertyValue(RichTextBox.FontWeightProperty,FontWeights.Bold);
    }
    else
    {
        text.ApplyPropertyValue(RichTextBox.FontWeightProperty,FontWeights.normal);
    }     
}

我有一个在报告查看器中生成报告的按钮。

private void reportviewing(object sender,RoutedEventArgs e)
{
    ReportDataSource reportDataSource = new ReportDataSource();
    connect();
    con.open();

    sqlDataAdapter adp = new sqlDataAdapter("select * from student_details",con);

    DataTable newtab = new DataTable();
    adp.Fill(newtab);
    reportDataSource.Name = "DataSet1";

    reportDataSource.Value = newtab;
    studentreport.LocalReport.ReportPath = "C:\\Users\\Alice Luver\\Documents\\Visual Studio 2012\\Projects\\STDNT\\STDNT\\studentsreport.rdlc";

    studentreport.LocalReport.DataSources.Add(reportDataSource);

    studentreport.LocalReport.Refresh();
    studentreport.RefreshReport();
}

这也可以。

我的问题是我无法保存格式。

例如,我要将一个富文本字段另存为:“嗨,我是 alice

它被保存并显示为:“嗨,我是爱丽丝”,没有加粗字体。

我该如何解决?谢谢

解决方法

您可以使用TextRange.SaveTextRange.LoadFlowDocument保存/加载格式化的内容。将内容另存为RTF会保留格式信息。

private async Task SaveAndLoadRtfDocumentExampleAsync()
{
  // Select some text
  var textRange = new TextRange(this.RichTextBox.Document.ContentStart,this.RichTextBox.Document.ContentEnd);

  // Convert the FlowDocument selection to a RTF string
  string rtfText;
  using (var outputStream = new MemoryStream())
  {
    textRange.Save(outputStream,DataFormats.Rtf);
    outputStream.Position = 0;
    using (var streamReader = new StreamReader(outputStream))
    {
      rtfText = await streamReader.ReadToEndAsync();
    }
  }

  StoreInDatabase(rtfText);

  rtfText = LoadFromDatabase();

  // Convert the RTF string to a FlowDocument of an existing RichTextBox      
  var newTextRange = new TextRange(this.RichTextBox.Document.ContentStart,this.RichTextBox.Document.ContentEnd);
  using (var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(rtfText)))
  {
    newTextRange.Load(inputStream,DataFormats.Rtf);
  }
}