【发布时间】:2020-08-27 02:57:27
【问题描述】:
我目前有一个组合框 cbxID,它显示所有 StudentID,并希望在按下按钮加载的同时将不同的元素发送到不同的文本框/组合框/NumericUpDown。
加载按钮应采用当前选定的学生 ID,然后将信息发送到右侧框中。
<Student>
<StudentID>207</StudentID> //supposed to go into txtStudentID
<FirstName>Bob</FirstName> // -->txtFirstName
<LastName>Smith</LastName> // -->txtLastName
<SchoolClass>10</SchoolClass> // -->txtSchoolClass
<Age>17</Age> //-->nudAge (numericupdown)
<Height>186</Height //-->nudHeight
<Gender>male</Gender> //-->combobox
</Student>
我正在使用此代码将 ID 显示到组合框中
private void Form1_Load(object sender, EventArgs e)
{
DataSet dataSet = new DataSet();
dataSet.ReadXml(FilePath);
this.cbxStudentIDs.DataSource = dataSet.Tables[0];
this.cbxStudentIDs.DisplayMember = "ID";
}
【问题讨论】:
-
你可以得到数据行:DataRow row = dataset.Tables[0].AsEnumerable().Where(x => x.Field
("StudentID") == cmxStudentIDs.Text) .FirstOrDefault();当xml具有“StudentID”时,不确定为什么您的代码使用“ID”。 -
我收到此错误
System.IndexOutOfRangeException: -
你从哪里得到异常?如果组合框没有项目,我的代码将不起作用,如果数据集中没有表,我的代码也不会起作用。看起来 Jack 修复了我之前发现的问题,并将“ID”更改为“StudentID”,就像我上面所说的那样。
标签: c# xml visual-studio