我試圖使用行[],它是一個字符串數組中的DataTable CartDT添加行。數據錶行填充在C#
DataTable CartDT = new DataTable();
public void DataTableColumn()
{
CartDT.Columns.Add("Product_Name", typeof(string));
CartDT.Columns.Add("Product_ID", typeof(string));
CartDT.Columns.Add("ItemQTY", typeof(string));
CartDT.Columns.Add("Price", typeof(string));
CartDT.Columns.Add("TotalPrice", typeof(string));
}
protected void AddToCart(object sender, GridViewCommandEventArgs e)
{
string[] arg = e.CommandArgument.ToString().Split(';');
int index = Convert.ToInt32(arg[3]);
TextBox itemQuantity = (TextBox)GridView1.Rows[index].Cells[6].FindControl("txtQty");
string[] row = new string[5];
row[0] = arg[0]; //Product_Name
row[1] = arg[1]; //Product_ID
row[2] = itemQuantity.Text; //OrderQTY
row[3] = arg[2]; //Price
row[4]=(Double.Parse(arg[2]) * Convert.ToInt32(itemQuantity.Text)).ToString();// calculate total price
CartDT.Rows.Add(row);//Creating row in Datatable using row[] string array
GridView2.DataSource = CartDT;
GridView2.DataBind();
}
現在,當我執行它,它使錯誤「輸入陣列長於列在這個表中的號碼」 陣列行[]中有準確地5個元素&也DataTable中CartDT具有還5列。 現在我無法準確找到我錯在哪裏。 請幫我找到bug。
Thanx提前。
哪一行拋出一個錯誤,它就有可能是引用一些其他的陣列 – 2013-03-16 08:15:29
只是跑,做行插入代碼的一部分,只是工作精細。我顯然必須硬編碼所有的產品價值。 – 2013-03-16 08:18:41
只是好奇,但你甚至打擾谷歌搜索「輸入數組比這個表中的列數更長」,並自己解決它?在您提出這樣的簡單問題之前,請閱讀[常見問題解答]並做研究。 – 2013-03-16 08:24:03