询问者
GridView无法更新功能

问题
-
GridView的代码如下:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="DDID" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" Width="715px"
onrowupdating="GridView1_RowUpdating"
onrowdeleting="GridView1_RowDeleting" Height="60px" >
<Columns>
<asp:BoundField DataField="DDID" HeaderText="编号" InsertVisible="False"
ReadOnly="True" SortExpression="DDID" >
<ItemStyle Font-Size="9pt" />
</asp:BoundField>
<asp:BoundField DataField="CPID" HeaderText="产品编号" SortExpression="CPID"
ReadOnly="True" >
<ItemStyle Font-Size="9pt" />
</asp:BoundField>
<asp:BoundField DataField="CPNAME" HeaderText="产品名" SortExpression="CPNAME"
ReadOnly="True" >
<ItemStyle Font-Size="9pt" />
</asp:BoundField>
<asp:BoundField DataField="CPDJ" HeaderText="产品单价" SortExpression="CPDJ"
ReadOnly="True" >
<ItemStyle Font-Size="9pt" />
</asp:BoundField>
<asp:TemplateField HeaderText="数量" SortExpression="SL">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" Width="60px" runat="server" Text='<%# Bind("SL") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("SL") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="9pt" />
</asp:TemplateField>
<asp:BoundField DataField="CPSUM" HeaderText="合计" SortExpression="CPSUM"
ReadOnly="True" >
<ItemStyle Font-Size="9pt" />
</asp:BoundField>
<asp:CommandField ShowEditButton="True" >
<ItemStyle Font-Size="9pt" />
</asp:CommandField>
<asp:CommandField ShowDeleteButton="True" >
<ItemStyle Font-Size="9pt" />
</asp:CommandField>
</Columns>
</asp:GridView>GridView1的RowUpdating事件代码如下:
但就是无法实现更新功能.也没有报错,麻烦看看代码哪里错了.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int RowKey = e.RowIndex;//整型变量获取当前的所属值
TextBox Mytext;
Mytext = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
Cpdt.Rows[RowKey]["SL"] = Convert.ToInt32(Mytext.Text.ToString());//数量
Cpdt.Rows[RowKey]["CPSUM"] = Convert.ToInt32(Mytext.Text) * Convert.ToSingle(Cpdt.Rows[RowKey]["CPDJ"]);//单价
Cpdt.AcceptChanges();
var mm = Cpdt.AsEnumerable().Sum(p => Convert.ToDouble(p["CPSUM"]));
Label2.Text = mm.ToString();
GridView1.EditIndex = -1;
GridView1.DataSource = Cpdt;
GridView1.DataBind();}
全部回复
-
内存中Session的虚拟表
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable Cpdt= Session["你的名字"] as DataTable;
int RowKey = e.RowIndex;//整型变量获取当前的所属值
TextBox Mytext;
Mytext = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
Cpdt.Rows[RowKey]["SL"] = Convert.ToInt32(Mytext.Text.ToString());//数量
Cpdt.Rows[RowKey]["CPSUM"] = Convert.ToInt32(Mytext.Text) * Convert.ToSingle(Cpdt.Rows[RowKey]["CPDJ"]);//单价
Cpdt.AcceptChanges();
Session["你的名字"] = Cpdt;
var mm = Cpdt.AsEnumerable().Sum(p => Convert.ToDouble(p["CPSUM"]));
Label2.Text = mm.ToString();
GridView1.EditIndex = -1;
GridView1.DataSource = Cpdt;
GridView1.DataBind();}
另外请确定你的事件被执行了! -
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int RowKey = e.RowIndex;//整型变量获取当前的所属值
TextBox Mytext;
Mytext = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
Cpdt.Rows[RowKey]["SL"] = Convert.ToInt32(Mytext.Text.ToString());//数量
Cpdt.Rows[RowKey]["CPSUM"] = Convert.ToInt32(Mytext.Text) * Convert.ToSingle(Cpdt.Rows[RowKey]["CPDJ"]);//单价
Cpdt.AcceptChanges();
var mm = Cpdt.AsEnumerable().Sum(p => Convert.ToDouble(p["CPSUM"]));
Label2.Text = mm.ToString();Label3.Text = Mytext.Text.ToString();
GridView1.EditIndex = -1;
GridView1.DataSource = Cpdt;
GridView1.DataBind();}