2013-03-16 40 views
0

我試圖使用行[],它是一個字符串數組中的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提前。

+1

哪一行拋出一個錯誤,它就有可能是引用一些其他的陣列 – 2013-03-16 08:15:29

+0

只是跑,做行插入代碼的一部分,只是工作精細。我顯然必須硬編碼所有的產品價值。 – 2013-03-16 08:18:41

+0

只是好奇,但你甚至打擾谷歌搜索「輸入數組比這個表中的列數更長」,並自己解決它?在您提出這樣的簡單問題之前,請閱讀[常見問題解答]並做研究。 – 2013-03-16 08:24:03

回答

2

而是做到這一點

DataRow dr = CartDT.NewRow(); 

然後

dr[0] = arg[0]; 

等。最後

CartDT.Rows.Add(dr); 
CartDT.AcceptChanges(); 

這樣行的實例將有CartDT架構。

DataTable CartDT = new DataTable(); 

public void CreateDataTableColumns() 
{ 
    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) 
{ 
    if (CartDT.Columns.Count = 0) 
    { 
     CreateDataTableColumns(); 
    } 

    string[] arg = e.CommandArgument.ToString().Split(';'); 

    int index = Convert.ToInt32(arg[3]);    
    TextBox itemQuantity = 
      (TextBox)GridView1.Rows[index].Cells[6].FindControl("txtQty"); 

    DataRow dr = CartDT.NewRow(); 
    dr[0] = arg[0]; //Product_Name 
    dr[1] = arg[1]; //Product_ID 
    dr[2] = itemQuantity.Text; //OrderQTY 
    dr[3] = arg[2]; //Price 
    dr[4] = (Double.Parse(arg[2]) * Convert.ToInt32(itemQuantity.Text)).ToString(); // calculate total price 

    CartDT.Rows.Add(dr); 
    CartDT.AcceptChanges(); 

    GridView2.DataSource = CartDT; 
    GridView2.DataBind(); 
} 
0

嘗試下面的代碼:

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"); 
     DataRow row = CartDT.NewRow(); 
     row["Product_Name"] = arg[0]; //Product_Name 
     row["Product_ID"] = arg[1]; //Product_ID 
     row["OrderQTY"] = itemQuantity.Text; //OrderQTY 
     row["Price"] = arg[2]; //Price 
     row["TotalPrice"]=(Double.Parse(arg[2]) * Convert.ToInt32(itemQuantity.Text)).ToString(); 
     CartDt.Rows.Add(row); 
     CartDT.AcceptChanges();  
     GridView2.DataSource = CartDT; 
     GridView2.DataBind(); 
    } 
+0

您只需要ASP.Net的'DataBind()' – 2013-03-16 08:22:19

1

調試,並在

CartDT.Rows.Add(row); 

打破,看看有多少列在CartDT;我想你不應該在正確的時間撥打DataTableColumn()

具有插入行做你的代碼只是正常

 DataTable CartDT = new DataTable(); 

     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)); 



     string[] row = new string[5]; 

     row[0] = "1"; //Product_Name 
     row[1] = "2"; //Product_ID 
     row[2] = "3"; //OrderQTY 
     row[3] = "4"; //Price 
     row[4] = "5";// calculate total price 
     CartDT.Rows.Add(row);//Creating row in Datatable using row[] string array 

     for (int i = 0; i < 5; i++) 
     { 
      Console.WriteLine(CartDT.Rows[0][i]); 
     } 

     Console.Read();