C# 使用gridview更新数据集值

C# 使用gridview更新数据集值,c#,asp.net,gridview,dataset,C#,Asp.net,Gridview,Dataset,我有一个网格视图: 折扣.aspx <asp:GridView ID="GridView2" runat="server" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None"> <FooterStyle BackColor="

我有一个网格视图:

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

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.Configuration;
使用系统数据;
使用System.Data.SqlClient;
公共部分类折扣:System.Web.UI.Page
{
数据集ds=新数据集();
受保护的无效页面加载(对象发送方、事件参数e)
{
DBservices db2=newdbservices();
SqlConnection con=db2.connect(“storeConnectionString”);
string SelectSTR=“从项目中选择*”;
SqlDataAdapter da=新的SqlDataAdapter(选择str,con);
da.填充(ds);
GridView2.DataSource=ds;
GridView2.DataBind();
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
国际价格;
对于(int i=0;iConvert.ToInt32(minamount.Text))
{
price=Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[2]);
价格=价格*((100-转换为32(贴现率文本))/100);
ds.Tables[0]。行[i]。ItemArray[4]=价格;
GridView2.DataBind();
}
}
}
}
正如您所看到的,我试图更新数据集中的Price列,而不是更新gridview,但问题是gridview没有任何变化


谢谢您,祝您度过愉快的一天

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

 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,以便在两种方法中都可以访问它

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

受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
国际价格;
对于(int i=0;iConvert.ToInt32(minamount.Text))
{
price=Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[2]);
价格=价格*((100-转换为32(贴现率文本))/100);
ds.Tables[0]。行[i]。ItemArray[4]=价格;
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.DataSource=ds;
            GridView2.DataBind();
        }
    }
}