无法将类型为“<>f__AnonymousType0`2[System.String,System.Int32]”的对象转换为类型 'System.IConvertible'
Unable to cast object of type '<>f__AnonymousType0`2[System.String,System.Int32]' to type 'System.IConvertible'
我试图在列表框的单击事件中将数据列表框填充到文本框,但我发现了这个错误
Additional information: Unable to cast object of type '<>f__AnonymousType0`2[System.String,System.Int32]' to type 'System.IConvertible'
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
StudenRecordDataContext std = new StudentRecordDataContext();
int selectedValue = Convert.ToInt32(listBox1.SelectedValue);
StudentRecord sr = std.StudentRecords.Single(s =>s.ID==selectedValue);
txtId.Text = sr.ID.ToString();
txtName.Text = sr.Name;
txtPassword.Text = sr.Password;
txtCnic.Text = sr.CNIC;
txtEmail.Text = sr.Email;
}
我认为错误在行StudentRecord sr = std.StudentRecords.Single(s =>s.ID==selectedValue);
该错误从何而来?我需要更改什么才能修复该错误?
很抱歉,您向我们提供了错误的程序失败行诊断。
罪魁祸首是这一行:
int selectedValue = Convert.ToInt32(listBox1.SelectedValue);
我预计您之前已经使用 StudentRecords
中来自 StudentRecordDataContext
实例的集合填充了 listbox1
。
如果您 select 来自列表框的值,SelectedValue
包含您添加到项目集合的对象(或通过设置 DataSource
属性 间接)。
要修复您的代码,您可以先确保该对象再次变为 StudentRecord
。这并不容易,因为您创建了一个匿名类型,我希望是这样的:
listbox1.DataSource = new StudentRecordDataContext()
.StudentRecords
.Select(sr => new { Name = sr.Name, ID = sr.ID });
当您尝试检索 SelectedValue 时,您得到的是匿名类型,而不是强类型。
不添加匿名类型,而是创建一个新的 class,它具有 Name 和 Id 的属性:
class StudentRecordItem
{
public string Name {get; set;}
public int ID {get; set;}
}
填充数据源时,为每条记录创建 StudentRecordItem classes 并将它们添加到数据源。
listbox1.DataSource = new StudentRecordDataContext()
.StudentRecords
.Select(sr => new StudentRecordItem { Name = sr.Name, ID = sr.ID });
你的代码可以变成这样:
StudentRecordItem selectedStudent = listBox1.SelectedValue as StudentRecordItem;
if (selectedStudent == null)
{
MessageBox.Show("No student record");
return;
}
int selectedValue = selectedStudent.ID;
你不需要 Convert.ToInt32
因为我假设 ID
已经是一个整数。
请记住,debugger in Visual Studio 显示所有属性和变量的实际类型和值。当类型转换失败时,您可以在那里检查您正在使用的实际类型。
我试图在列表框的单击事件中将数据列表框填充到文本框,但我发现了这个错误
Additional information: Unable to cast object of type '<>f__AnonymousType0`2[System.String,System.Int32]' to type 'System.IConvertible'
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
StudenRecordDataContext std = new StudentRecordDataContext();
int selectedValue = Convert.ToInt32(listBox1.SelectedValue);
StudentRecord sr = std.StudentRecords.Single(s =>s.ID==selectedValue);
txtId.Text = sr.ID.ToString();
txtName.Text = sr.Name;
txtPassword.Text = sr.Password;
txtCnic.Text = sr.CNIC;
txtEmail.Text = sr.Email;
}
我认为错误在行StudentRecord sr = std.StudentRecords.Single(s =>s.ID==selectedValue);
该错误从何而来?我需要更改什么才能修复该错误?
很抱歉,您向我们提供了错误的程序失败行诊断。
罪魁祸首是这一行:
int selectedValue = Convert.ToInt32(listBox1.SelectedValue);
我预计您之前已经使用 StudentRecords
中来自 StudentRecordDataContext
实例的集合填充了 listbox1
。
如果您 select 来自列表框的值,SelectedValue
包含您添加到项目集合的对象(或通过设置 DataSource
属性 间接)。
要修复您的代码,您可以先确保该对象再次变为 StudentRecord
。这并不容易,因为您创建了一个匿名类型,我希望是这样的:
listbox1.DataSource = new StudentRecordDataContext()
.StudentRecords
.Select(sr => new { Name = sr.Name, ID = sr.ID });
当您尝试检索 SelectedValue 时,您得到的是匿名类型,而不是强类型。
不添加匿名类型,而是创建一个新的 class,它具有 Name 和 Id 的属性:
class StudentRecordItem
{
public string Name {get; set;}
public int ID {get; set;}
}
填充数据源时,为每条记录创建 StudentRecordItem classes 并将它们添加到数据源。
listbox1.DataSource = new StudentRecordDataContext()
.StudentRecords
.Select(sr => new StudentRecordItem { Name = sr.Name, ID = sr.ID });
你的代码可以变成这样:
StudentRecordItem selectedStudent = listBox1.SelectedValue as StudentRecordItem;
if (selectedStudent == null)
{
MessageBox.Show("No student record");
return;
}
int selectedValue = selectedStudent.ID;
你不需要 Convert.ToInt32
因为我假设 ID
已经是一个整数。
请记住,debugger in Visual Studio 显示所有属性和变量的实际类型和值。当类型转换失败时,您可以在那里检查您正在使用的实际类型。