[英]An unhandled exception of type 'System.InvalidCastException'
[英]An unhandled exception of type 'System.InvalidCastException' occurred in UretimPlan.exe
我正在开发一个项目来在两个数据网格视图之间传输选定的行。 但是,当我运行项目并按下允许我将选定行从父表拖放到子表的按钮时,我收到以下错误。 如果你能帮忙,我会很高兴。
private void button2_Click(object sender, EventArgs e)
{
dataGridView2.Rows.Clear();
foreach (DataGridViewRow item in dataGridView1.Rows)
{
if ((bool)item.Cells[0].Value==true)
{
int n = dataGridView2.Rows.Add();
dataGridView2.Rows[n].Cells[0].Value = item.Cells[1].Value.ToString();
dataGridView2.Rows[n].Cells[1].Value = item.Cells[2].Value.ToString();
dataGridView2.Rows[n].Cells[2].Value = item.Cells[3].Value.ToString();
dataGridView2.Rows[n].Cells[3].Value = item.Cells[4].Value.ToString();
dataGridView2.Rows[n].Cells[4].Value = item.Cells[5].Value.ToString();
dataGridView2.Rows[n].Cells[5].Value = ((DateTime)item.Cells[6].Value).ToString("MM-dd-yyyy");
dataGridView2.Rows[n].Cells[6].Value = item.Cells[7].Value.ToString();
}
}
}
错误在这一行
dataGridView2.Rows[n].Cells[5].Value = ((DateTime)item.Cells[6].Value).ToString("MM-dd-yyyy");
此外,当我将选定的行传输到另一个表时,我希望将其从数据库中的该表中删除。 我怎样才能做到这一点?
(日期时间)item.Cells[6].Value
此转换失败,因为没有要转换的值,或者该值不是DateTime
。
它可能是一个字符串吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.