将多行信息从数据库添加到文本框

时间:2022-08-27 16:51:43

Thanks for reviewing my question. Basically, in my form... The manager can set a reminder to an employee, so when the employee logs into my form... it comes up with a reminder from the manager.

感谢您查看我的问题。基本上,在我的表格中...经理可以为员工设置提醒,因此当员工登录我的表单时......会提示经理提醒。

Trouble is, the data contains multiple reminders from the same user but when I add them to a text box I get this error:

麻烦的是,数据包含来自同一用户的多个提醒,但当我将它们添加到文本框时,我收到此错误:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Core.dll

System.Core.dll中发生了未处理的“System.InvalidOperationException”类型异常

Additional information: Sequence contains more than one element

附加信息:序列包含多个元素

my code:

我的代码:

                System.Windows.Forms.Form f = System.Windows.Forms.Application.OpenForms["Login"];
            int id = Convert.ToInt32(((Login)f).idTb.Text);
            SundownDatabaseEntities4 db = new SundownDatabaseEntities4();
            var getrecord = db.Reminders.Where(a => a.Id == id).SingleOrDefault();

            reminderTb.Text = Convert.ToString(getrecord.Reminder1);

How do I add multiple elements to a text box? Could I use a list box?

如何在文本框中添加多个元素?我可以使用列表框吗?

1 个解决方案

#1


0  

It sounds like you are trying to append multiple different strings to a single textbox, which in this case i would use the appendText() extension on the textbox to add multiple strings. You could also use the ListBox if you would like. I would set the Source to the data you are sending it for ease of use.

听起来你正在尝试将多个不同的字符串附加到单个文本框中,在这种情况下,我将使用文本框上的appendText()扩展名来添加多个字符串。如果您愿意,也可以使用ListBox。为了方便起见,我会将Source设置为您发送的数据。

Here is a link to the append text function https://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.appendtext(v=vs.110).aspx

这是附加文本函数的链接https://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.appendtext(v=vs.110).aspx

Here is a link to how to set-up a source for a listbox https://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.datasource(v=vs.110).aspx

以下是如何设置列表框源代码的链接https://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.datasource(v=vs.110).aspx

Edit

To add on since it was something else causing the issue I would use:

要添加,因为它是导致问题的其他因素我将使用:

List<object> objects = Enumerable.Select(i => i.id == id);

Here is a link to the msdn for more information https://msdn.microsoft.com/library/bb548891(v=vs.100).aspx

以下是msdn的链接以获取更多信息https://msdn.microsoft.com/library/bb548891(v=vs.100).aspx

Edit 2

After finally getting my VS up to date and playing around i think the easiest thing you can do here is:

在最终让我的VS更新并玩了之后,我觉得你能做的最简单的事情是:

db.Reminders.ForEach(reminder => {if(reminder.Id == id){ reminderTb.AppendText(Convert.ToString(reminder.Reminder1));  }})

You might need to adjust accordingly to your specific code not sure if i have your class structure right, but this will look through the array of reminders, look and on that specific id it will append the text to the text box with what I imagine is what you wanted based off your code and the class structure i saw. this should work since you are manually going through the array and adding the text.

您可能需要相应地调整您的特定代码,不确定我的类结构是否正确,但是这将查看提醒数组,查看该特定ID,它会将文本附加到文本框中,我想象的是基于你的代码和我看到的类结构你想要什么。这应该工作,因为您手动浏览数组并添加文本。

#1


0  

It sounds like you are trying to append multiple different strings to a single textbox, which in this case i would use the appendText() extension on the textbox to add multiple strings. You could also use the ListBox if you would like. I would set the Source to the data you are sending it for ease of use.

听起来你正在尝试将多个不同的字符串附加到单个文本框中,在这种情况下,我将使用文本框上的appendText()扩展名来添加多个字符串。如果您愿意,也可以使用ListBox。为了方便起见,我会将Source设置为您发送的数据。

Here is a link to the append text function https://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.appendtext(v=vs.110).aspx

这是附加文本函数的链接https://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.appendtext(v=vs.110).aspx

Here is a link to how to set-up a source for a listbox https://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.datasource(v=vs.110).aspx

以下是如何设置列表框源代码的链接https://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.datasource(v=vs.110).aspx

Edit

To add on since it was something else causing the issue I would use:

要添加,因为它是导致问题的其他因素我将使用:

List<object> objects = Enumerable.Select(i => i.id == id);

Here is a link to the msdn for more information https://msdn.microsoft.com/library/bb548891(v=vs.100).aspx

以下是msdn的链接以获取更多信息https://msdn.microsoft.com/library/bb548891(v=vs.100).aspx

Edit 2

After finally getting my VS up to date and playing around i think the easiest thing you can do here is:

在最终让我的VS更新并玩了之后,我觉得你能做的最简单的事情是:

db.Reminders.ForEach(reminder => {if(reminder.Id == id){ reminderTb.AppendText(Convert.ToString(reminder.Reminder1));  }})

You might need to adjust accordingly to your specific code not sure if i have your class structure right, but this will look through the array of reminders, look and on that specific id it will append the text to the text box with what I imagine is what you wanted based off your code and the class structure i saw. this should work since you are manually going through the array and adding the text.

您可能需要相应地调整您的特定代码,不确定我的类结构是否正确,但是这将查看提醒数组,查看该特定ID,它会将文本附加到文本框中,我想象的是基于你的代码和我看到的类结构你想要什么。这应该工作,因为您手动浏览数组并添加文本。