0
    int flag = 0;
    int num = 0;
    int price, totprice, qty;
    string product;

    private void ProductsGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        product = ProductsGV.SelectedRows[0].Cells[1].Value.ToString();
        //qty =  Convert.ToInt32(OrderQtyTb.Text);
        price = Convert.ToInt32(ProductsGV.SelectedRows[0].Cells[3].Value.ToString());
        //totprice = qty * price;
        flag = 1;
    }

    private void button4_Click(object sender, EventArgs e)
    {
        if (OrderQtyTb.Text == "")
            MessageBox.Show("Please Enter Quantity");
        else if (flag == 0)
            MessageBox.Show("Please Select Product");
        else
        {
            num = num + 1;
            qty = Convert.ToInt32(OrderQtyTb.Text);
            totprice = qty * price;
            DataTable table = OrdersGV.DataSource as DataTable;
            table.Rows.Add(num, product, qty, price, totprice);
            flag = 0;
        }
    }

Please help me to fix the error. The error is on the line code table.Rows.Add(num, product, qty, price, totprice);. I just want to add the data in a data grid where i will input number of quantity of a product and then select a product among the product list table then lastly i will confirm.

John
  • 11
  • Has `table` been initialized when `button4` is clicked? – Kieran Devlin Feb 16 '23 at 14:42
  • 1
    `OrdersGV.DataSource as DataTable` that will be null if it's the wrong type. Attach a debugger and see where it's null. I find the debugger setting in Visual Studio to break on exceptions to be helpful. – Nikki9696 Feb 16 '23 at 14:43
  • I think you have not set `OrdersGV.DataSource` – machan Feb 16 '23 at 14:43
  • Can OrderGV.DataSource be cast as DataTable? Otherwise the safe cast returns `null`. Either way you should check `table` for `null` before using it. – RedFox Feb 16 '23 at 15:14

0 Answers0