使用datagridview时System.InvalidCastException

时间:2017-02-04 11:12:22

标签: c#

    private void VPfn_GenInvoice()
    {
        int iid = Convert.ToInt16(tid.Text);
        int cid = Convert.ToInt16(tcid.Text);
        double subtotal = Convert.ToDouble(tst.Text);
        double atax = Convert.ToDouble(tat.Text);
        double discount = Convert.ToDouble(tdis.Text);
        double total = Convert.ToDouble(ttotal.Text);

        StringBuilder iQ = new StringBuilder();

        foreach(DataRow x in invoice_data.Rows)
        {
            int iiid = VPfunctions.VPfn_N2ID_itm(x["item_name"].ToString());
            string dQuery = "INSERT INTO `invoice_data` (id, sr, item_id, item_qty, item_unit, item_vat, amount) " +
                "VALUES(" + iid + ", " + x["serial_number"].ToString() + ", " + iiid + ", " + x["item_qty"].ToString()
                + ", " + x["item_unit"].ToString() + ", " + x["item_vat"].ToString() + ", " + x["amount"].ToString() + ")";
            iQ.Append(dQuery);
            iQ.AppendLine();
        }

        //Updating main Invoice DB
        string query = "INSERT INTO `invoice` ( id, cusid, cdate, mdate, subtotal, atax, discount, total) VALUES (?A,?B,?C,?D,?E,?F,?G,?H)";
        xxtb.Text = iQ.ToString();
        /**
        try
        {
            //inserting into invoice
            con.Open();
            MySqlCommand xv = new MySqlCommand(query, con);
            xv.Parameters.AddWithValue("?A", iid.ToString());
            xv.Parameters.AddWithValue("?B", cid.ToString());
            xv.Parameters.AddWithValue("?C", DateTime.Now.ToString());
            xv.Parameters.AddWithValue("?D", "-");
            xv.Parameters.AddWithValue("?E", subtotal.ToString());
            xv.Parameters.AddWithValue("?F", atax.ToString());
            xv.Parameters.AddWithValue("?G", discount.ToString());
            xv.Parameters.AddWithValue("?H", total.ToString());
            xv.ExecuteNonQuery();

            //Inserting into invoice_data
            MySqlCommand ixv = new MySqlCommand(iQ.ToString(), con);
            ixv.ExecuteNonQuery();
        }
        catch
        {

        }
        finally
        {
            con.Close();
        } **/
    }

我收到的错误如下: System.InvalidCastException:'无法将'System.Windows.Forms.DataGridViewRow'类型的对象强制转换为'System.Data.DataRow'。'

现在,我正在尝试为自己制作发票软件,但我收到此错误,是的,我正在使用Visual Studio 2017RC和Windows 10 64位。

4 个答案:

答案 0 :(得分:0)

foreach中的x变量必须是DataGridViewRow类型,修改代码如下:

foreach(DataGridViewRow x in invoice_data.Rows)

答案 1 :(得分:0)

我的猜测是invoice_data.Rows是DataGridViewRow的集合,你的foreach正试图将它强制转换为DataRow。如果你改变你的foreach它是否有效:

foreach(DataGridViewRow x in invoice_data.Rows)
...

答案 2 :(得分:0)

foreach(DataRow x in invoice_data.Rows)

此行的x类型错误。 DataRow 是抽象 DataTable 中的一行,而不是GUI元素 DataGridView 。由于 Rows 属性返回 DataGridViewRowCollection 类型的对象,foreach执行静默转换,为了向后兼容,此类型仅实现IEnumerable和不是IEnumerable<DataGridViewRow>。这就是编译器没有警告你无效演员的原因。

因此修复非常简单:

foreach(DataGridViewRow x in invoice_data.Rows)

x上的属性有点不同。您必须使用 Cells 属性来获取数据。您可以使用列名称或列索引。其中一个应该有效:

x.Cells["item_name"].ToString()

x.Cells[0].ToString()

如果名称是第一列。

答案 3 :(得分:0)

人们已经给出了正确的答案

foreach(DataGridViewRow x in invoice_data.Rows)

但是如果有人想知道如何从datagridview读取数据,则必须编写这些行

foreach(DataGridViewRow x in invoice_data.Rows){
Console.WriteLine(x.Cells["ColumnName"].Value.ToString());}