输入数组长度超过了表中的列数。

huangapple go评论61阅读模式
英文:

input array is longer than the number of columns in my table

问题

我有这段代码:

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 = new DataTable();
        table.Rows.Add(num, product, qty, price, totprice);
        OrdersGV.DataSource = table;
        flag = 0;
    }
}

我在这一行代码上遇到了错误:

table.Rows.Add(num, product, qty, price, totprice);

错误信息为:

输入数组的长度超出了此表中的列数

该代码的目的是允许我输入数量。然后我将点击产品列表表,然后再点击"Add to Order"按钮。我希望该按钮能显示在OrdersGV上的新数量。

英文:

I have this code:

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 = new DataTable();
        table.Rows.Add(num, product, qty, price, totprice);
        OrdersGV.DataSource = table;
        flag = 0;
    }
}

I get an error on this line:

table.Rows.Add(num, product, qty, price, totprice);

> input array is longer than the number of columns in this table

The purpose of the code is to let me input a quantity. Then I will click on the product list table and after that I will now click the "Add to Order" button. I want the button to reveal the new quantity on the OrdersGV.

答案1

得分: 1

问题出在这一行:

DataTable table = new DataTable();

这会创建一个全新的表,没有定义任何列,而 DataTable 类型要求您使用一个明确定义的模式。

您不想在这里创建一个新的 DataTable。 您希望找到已经显示在网格中的 现有 数据源,并将行添加到其中。仅当没有现有数据源时,您才需要创建一个带有定义列的 DataTable 对象。1

英文:

The problem is this line:

DataTable table = new DataTable();  

This makes a brand new table, with no columns defined at all, and the DataTable type requires you to use a well-defined schema.

You don't want to create a new DataTable here. You want to find the existing data source that is already showing in the grid, and add the row to that. If (and only if!) there is none, you need to create a DataTable object with defined Columns.

答案2

得分: 0

你不需要“new DataTable”。它只需要使用OrdersGV的数据源进行初始化:

DataTable table = OrdersGV.DataSource as DataTable;
table.Rows.Add(num, product, qty, price, totprice);
英文:

You don't need to new the DataTable. It should only be initial with the dataSource of OrdersGV:

 DataTable table = OrdersGV.DataSource as DataTable;
 table.Rows.Add(num, product, qty, price, totprice);

huangapple
  • 本文由 发表于 2023年2月9日 01:21:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定