简体   繁体   English

指定的强制转换无效C#

[英]Specified cast is not valid c#

I am having trouble with my system. 我的系统有问题。

It says specified cast not valid. 它说指定的演员表无效。

foreach (var q in _list) 
{
    dataGridView1.Rows.Add(q.BroadcastAdminID, string.Format("{0:d}", q.BrodacastDate), q.Time1 + ":00", q.BroadcastMessage, q.BroadcastMessageTagalog, q.BroadcastMessageBisaya, q.SaveCount, (bool)q.IsBroadcast);
}

Try something like this: 尝试这样的事情:

foreach (var q in _list)
{
    int idx = dataGridView1.Rows.Add();
    dataGridView1.Rows[idx].Cells["BroadcastAdminID"].Value = q.BroadcastAdminID;
    dataGridView1.Rows[idx].Cells["BrodacastDate"].Value = string.Format("{0:d}", q.BrodacastDate);
    dataGridView1.Rows[idx].Cells["BroadcastMessage"].Value = q.BroadcastMessage;
    //...
    dataGridView1.Rows[idx].Cells["IsBroadcast"].Value = q.IsBroadcast;
}

or quite simply: 或者很简单:

foreach (var q in _list)
{
    dataGridView1.Rows.Add
    (
        new object[] 
        {
            q.BroadcastAdminID, string.Format("{0:d}", q.BrodacastDate),
            q.Time1 + ":00", q.BroadcastMessage, q.BroadcastMessageTagalog,
            q.BroadcastMessageBisaya, q.SaveCount, 
            Convert.ToBoolean(q.IsBroadcast) //<- use convert if cannot be casted
        } 
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM