【问题标题】:update a dataset values with gridview使用 gridview 更新数据集值
【发布时间】:2014-01-03 16:44:48
【问题描述】:

我有一个网格视图:

折扣.aspx

   <asp:GridView ID="GridView2" runat="server" BackColor="White" 
        BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" 
        CellSpacing="1" GridLines="None">
        <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
        <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
        <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
        <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#594B9C" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#33276A" />
    </asp:GridView>

我通过 Discount.aspx.cs 的数据集填充它

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class Discount : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {

        DBservices db2 = new DBservices();
        SqlConnection con = db2.connect("storeConnectionString"); 
        string SelectSTR = "SELECT * FROM Items";  
        SqlDataAdapter da = new SqlDataAdapter(SelectSTR, con);  


        da.Fill(ds);
        GridView2.DataSource = ds;
        GridView2.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        int price;
        for (int i = 0; i < GridView2.Rows.Count; i++)
        {

            if (Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[4]) > Convert.ToInt32(minamount.Text))
            {
                price = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[2]);
                price= price*((100- Convert.ToInt32(discountrate.Text))/100);
                ds.Tables[0].Rows[i].ItemArray[4] = price;
                GridView2.DataBind();
            }
        }
    }

}

如您所见,我试图更新数据集中的价格列,而不是更新 gridview,但问题是 gridview 没有任何变化......

谢谢你,祝你有美好的一天

【问题讨论】:

  • 试试我更新的答案代码。

标签: c# asp.net gridview dataset


【解决方案1】:

您没有在单击按钮时指定 gridview 的数据源。请注意,这些更改不会更新到数据库(您的调用)。

 protected void Button1_Click(object sender, EventArgs e)
{
    int price;
    for (int i = 0; i < GridView2.Rows.Count; i++)
    {

        if (Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[4]) > Convert.ToInt32(minamount.Text))
        {
            price = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[2]);
            price= price*((100- Convert.ToInt32(discountrate.Text))/100);
            ds.Tables[0].Rows[i].ItemArray[4] = price;
            Gridview2.DataSource=ds;
            GridView2.DataBind();
        }
    }
}

【讨论】:

    【解决方案2】:

    你需要用数据源重新绑定网格:

     GridView2.DataSource = ds; //missing
    
      GridView2.DataBind();
    

    您缺少按钮单击事件中的第一行:

    声明一个全局数据适配器;

    然后在改变值之后:

       da.update() // Saves Changes to database
    

    更新2:

        SqlDataAdapter da = new SqlDataAdapter(SelectSTR, con);  
    
        make changes to dataset
    
       da.update() // save changes to database
    

    注意:在外部声明 dataadapter 以便在这两种方法中都可以访问它

    【讨论】:

      猜你喜欢
      • 2012-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      相关资源
      最近更新 更多