简体   繁体   English

将每一行保存在手动构建的gridview中

[英]Save each row in gridview that was built manually

I'm trying to save multiple rows from a gridview into sql database. 我正在尝试将gridview中的多行保存到sql数据库中。 The gridview is manually built in code, thus it is not databind... The reason for this is because this is a grid for parts that has to be added to a quote... Okey so I done the saving part, the only thing is, is that I have to set a foreach to get the data from that row (which I done), but the variables will be set constantly and only getting the last rows data... Can anybody please help me out? gridview是用代码手动构建的,因此它不是数据绑定的。之所以这样,是因为这是一个零件网格,必须将其添加到引号中……Okey,所以我做了保存零件的工作,唯一的事情是,我是否必须设置一个foreach来从该行获取数据(我已完成),但是变量将不断设置,并且仅获取最后一行的数据...有人可以帮我吗? This is what I've done: 这是我所做的:

    conn = new SqlConnection(GetConnectionString());

    string sql = "INSERT INTO CC_ItemsRequested (Item,Quantity, Price, ModelID, DateCreatedOn ) VALUES (@Item, @Quantity, @Price, @ModelID, @DateCreatedOn)";

    try
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        int row = GridView1.Rows.Count;

        cmd.Parameters.AddWithValue("@Item", myPart);
        cmd.Parameters.AddWithValue("@Quantity", myQuantity);
        cmd.Parameters.AddWithValue("@Price", myPrice);
        cmd.Parameters.AddWithValue("@ModelID", methodID);
        cmd.Parameters.AddWithValue("@DateCreatedOn", DateTime.Now);

        for (int i = 0; i < row; i++)
        {
            cmd.Parameters["@Item"].Value = myPart;
            cmd.Parameters["@Quantity"].Value = myQuantity;
            cmd.Parameters["@Price"].Value = myPrice;
            cmd.Parameters["@ModelID"].Value = methodID;
        }

        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();

This will save the data, but I need to do something so that each row will be saved...This is the foreach that I wrote: 这将保存数据,但是我需要做一些事情以便保存每一行...这是我写的foreach:

     //foreach (GridViewRow rows in GridView1.Rows)
        //{
        //    if (rows.RowType == DataControlRowType.DataRow)
        //    {
        //        string myPart = rows.Cells[0].Text;
        //        string myQuantity = rows.Cells[1].Text;
        //        string myPrice = rows.Cells[2].Text; 
        //    }

But the problem is, it is not going to get all the rows in the grid's data, it will basically only save the last row then.... 但是问题是,它不会获得网格数据中的所有行,基本上它只会保存最后一行。

Any ideas please? 有什么想法吗? Thanks in advance! 提前致谢!

I refactored your code a little bit: the SqlCommand has to be executed for each rows, and the parameters have to be retrieve from the GridView before. 我对您的代码进行了一些重构:必须对每行执行SqlCommand,并且之前必须从GridView中检索参数。

conn.Open();

using(SqlCommand cmd = new SqlCommand(sql, conn))
{
    var now = DateTime.Now;

    foreach (GridViewRow row in GridView1.Rows)
    {
        string myPart = row.Cells[0].Text;
        string myQuantity = row.Cells[1].Text;
        string myPrice = row.Cells[2].Text;
        //... etc

        cmd.Parameters.Clear();
        cmd.Parameters.AddWithValue("@Item", myPart);
        cmd.Parameters.AddWithValue("@Quantity", myQuantity);
        cmd.Parameters.AddWithValue("@Price", myPrice);
        cmd.Parameters.AddWithValue("@ModelID, methodID);
        cmd.Parameters.AddWithValue("@DateCreatedOn, now);

        cmd.ExecuteNonQuery();
    }
}

conn.Close();

I am not sure you want to store Price, Quantity and numeric values as text. 我不确定您是否要将价格,数量和数值存储为文本。 You probably want to do some validation first, and do some conversions instead of picking the Text property. 您可能希望先进行一些验证,然后进行一些转换,而不是选择Text属性。 What not take the .Value of the cell (if their CellValueType is defined correctly) ? 什么不采用单元格的.Value(如果正确定义了它们的CellValueType)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM