【问题标题】:DataTable row filling in C#C#中的DataTable行填充
【发布时间】:2013-03-05 00:48:26
【问题描述】:

我正在尝试使用 row[] 在 DataTable CartDT 中添加行,它是一个字符串数组。

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

现在当我执行它时,它给出了“输入数组长于该表中的列数”的错误 数组 row[] 中正好有 5 个元素,而且 DataTable CartDT 也有 5 列。 现在我无法准确找到我错的地方。 请帮我找出错误。

提前谢谢。

【问题讨论】:

  • 哪一行报错,可能是引用了其他数组
  • 刚刚运行了执行行插入的代码部分,工作得很好。我显然必须对所有 Product 值进行硬编码。
  • 只是好奇,但是您是否甚至费心在谷歌上搜索“输入数组比此表中的列数长”并自己解决?请阅读FAQ's 并在您提出此类简单问题之前进行研究。

标签: c# asp.net datatable datarow arrays


【解决方案1】:

改为这样做

DataRow dr = CartDT.NewRow();

然后

dr[0] = arg[0];

等等。最后

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

这样,Row 的实例将具有 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();
}

【讨论】:

    【解决方案2】:

    调试,然后中断

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

    【讨论】:

      【解决方案3】:

      试试下面的代码:

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

      【讨论】:

      • ASP.Net 只需要DataBind()
      猜你喜欢
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2010-09-20
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多