从网格视图插入到我的数据库中的值从其余列的第二列开始都是相同的

本文关键字:二列 开始 余列 插入 视图 网格 我的 数据库 | 更新日期: 2023-09-27 18:32:11

我能够插入数据而没有任何错误,但值不是需要的。

这是我的数据网格视图

第二张图片是我的数据库这是插入到我的数据库中的内容

ASP网络代码

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AutoGenerateColumns="False" DataKeyNames="RefNo" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:BoundField DataField="RefNo" HeaderText="RefNo" InsertVisible="False" 
            ReadOnly="True" SortExpression="RefNo" />
        <asp:TemplateField HeaderText="ProductID" SortExpression="ProductID">
            <EditItemTemplate>
                <asp:TextBox ID="ProductID" runat="server" Text='<%# Eval("ProductID") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="txtProductid" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name" SortExpression="Name">
            <EditItemTemplate>
                <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Name" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
            SortExpression="Quantity" />
        <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
        <asp:BoundField DataField="Amount" HeaderText="Amount" 
            SortExpression="Amount" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:MyOwnMeatshopConnectionString %>" 
    SelectCommand="SELECT Orders.RefNo, Orders.ProductID, Products.Name, Orders.Quantity, Orders.Price, Orders.Amount FROM Orders INNER JOIN Products ON Orders.ProductID = Products.ProductID">
</asp:SqlDataSource>
<asp:Button ID="btnSave" runat="server" class="btn btn" Text="Save" 
    style="color:White" BackColor="Black" onclick="btnSave_Click"/>
</form>

代码隐藏

    protected void btnSave_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {

            if (row.RowType == DataControlRowType.DataRow)
            {

                string RefNo = GridView1.DataKeys[row.RowIndex].Values[0].ToString();
                string ProductID = GridView1.DataKeys[row.RowIndex].Values["txtProductid"].ToString();
                //Label ProductID = (row.FindControl("ProductID") as Label);
                Label Name = row.FindControl("Name") as Label;
                string Price = GridView1.DataKeys[row.RowIndex].Values[0].ToString();
                string Quantity = GridView1.DataKeys[row.RowIndex].Values[0].ToString();
                string Amount = GridView1.DataKeys[row.RowIndex].Values[0].ToString();
                con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandText = "INSERT INTO SODetails (SOID, RefNo, ProductID, Name, Price, Quantity, Amount) VALUES (@SOID, @RefNo, @ProductID, @Name, @Price, @Quantity, @Amount)";
                cmd.Parameters.AddWithValue("@UserID", Session["userid"].ToString());
                cmd.Parameters.Add("@SOID", SqlDbType.VarChar).Value = SOID;
                cmd.Parameters.Add("@RefNo", SqlDbType.VarChar).Value = RefNo.ToString();
                cmd.Parameters.Add("@ProductID", SqlDbType.VarChar).Value = ProductID;
                cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name.Text;
                cmd.Parameters.Add("@Price", SqlDbType.VarChar).Value = Price.ToString();
                cmd.Parameters.Add("@Quantity", SqlDbType.VarChar).Value = Quantity.ToString();
                cmd.Parameters.Add("@Amount", SqlDbType.VarChar).Value = Amount.ToString();
                cmd.ExecuteNonQuery();
                SqlDataReader dr = cmd.ExecuteReader();
                cmd.CommandText = "SELECT SOID, RefNo, ProductID, Name, Price, Quantity, Amount FROM SODetails";
                GridView1.EditIndex = -1;
                GridView1.DataBind();
                con.Close();
            }
        }
    }

从网格视图插入到我的数据库中的值从其余列的第二列开始都是相同的

更改以下代码并从 BoundFields 获取值,因为 Value[0] 是 Ref 列

string Price = GridView1.DataKeys[row.RowIndex].Values[0].ToString();
string Quantity = GridView1.DataKeys[row.RowIndex].Values[0].ToString();
string Amount = GridView1.DataKeys[row.RowIndex].Values[0].ToString();