【问题标题】:Load data from database to richtextbox in c# windows form using Linq使用Linq将数据从数据库加载到c# windows窗体中的richtextbox
【发布时间】:2018-12-27 00:47:30
【问题描述】:

我正在尝试使用 Linq 在我的 c# windows 窗体项目中将数据从数据库加载到 RichTextBox(如图所示)。

我不知道我是否做得对,因为数据没有加载到 RichTextBox。请帮忙。

这就是我正在尝试的方式:

 string idNr = txtIdcardNr.Text.Trim();

 var CheckIfIdCardExist = (from u in db.Customer
                           where u.IdentityCardNr == idNr
                           select u).FirstOrDefault();
     if(CheckIfIdCardExist != null)
     {
       String template =
       @"Date\t\t{0}
       Notes\t\t{1}
       Staff\t\t{2}
       *********\t\t{3}";
              var notes = (from u in db.CustomerNotes
                           join em in db.Employee on u.StaffId equals em.EmployeeId
                           where u.CustomerId == CheckIfIdCardExist.CustomerId
                           select new {
                                         Date = u.NoteDate,
                                         notes = u.Notes,
                                         employee = em.FirstName + " " + em.LastName


                                     }).ToList();
                    foreach(var n in notes)
                    {
                        richTextBox1.Text = string.Format(template, n.Date, n.notes, n.employee);
                    }

【问题讨论】:

  • 你没有说你不喜欢这是做什么的。但是,我建议在答案中尝试一下。
  • richTextBox1.Text = 显然这条线会让你摆脱困境,不管其他任何问题,你只需添加最后一个注释
  • 图片是你想看的吗?
  • @Ann L. 感谢您的回复,是的,我希望看到这样的图像
  • @TheGeneral 指出的问题是不是你只看到了最后的注释?如果是这样,我在下面提出了一个解决方案。

标签: c# winforms linq richtextbox


【解决方案1】:

我在这里迈出了一大步,并猜测主要问题是您没有看到所有笔记,而只是看到最后一个。

     var notes = (from u in db.CustomerNotes
                       join em in db.Employee on u.StaffId equals em.EmployeeId
                       where u.CustomerId == CheckIfIdCardExist.CustomerId
                       select new {
                                     Date = u.NoteDate,
                                     notes = u.Notes,
                                     employee = em.FirstName + " " + em.LastName


                                 });

     StringBuilder sb = new StringBuilder();
     foreach(var n in notes)
     {
        sb.AppendFormat(template, n.Date, n.notes, n.employee);
        sb.Append("\n");
     }
     richTextBox1.Text = sb.ToString();

【讨论】:

  • L 非常感谢。现在它正在按我的意愿工作。我已经删除了我的 \t\t 和 Notes 、 staff 和 **** 在我想要的新行中开始,但位置为零。我的意思是,例如,注释正好在日期结束后开始,而不是左侧的长边,而是在日期结束之后。我在模板中尝试了 \n 但没有奏效。但它很棒,它正在工作......非常感谢。如果您能看到 Notes、Staff 和 **** 不是从最左侧开始的问题,我不知道。
  • 听起来好像你需要一个回车符。不幸的是,我不知道 RTF 中的内容是什么。
猜你喜欢
  • 1970-01-01
  • 2016-11-20
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多