如何只更新gridview中更新的行?

时间:2012-03-20 05:36:44

标签: c# asp.net

在此gridview中仅更新更新行(仅复选框列)的最便捷方法是什么?什么是检查行更新的方便方法?

C#

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<customer> listCustomer = new List<customer>();
            customer cust1 = new customer(){name="fred",email="fred@mail.com",jobless="true"};
            customer cust2 = new customer(){name="mark",email="mark@mail.com",jobless="false"};
            listCustomer.Add(cust1);
            listCustomer.Add(cust2);
            GridView1.DataSource=listCustomer;
            GridView1.DataBind();
        }


    }

protected void btnUpdate_Click1(object sender,EventArgs e)         {             foreach(GridView1.Rows中的GridViewRow rw)             {                 CheckBox thiscontrol =(CheckBox)rw.Cells [0] .FindControl(“cb”);                 var ch = thiscontrol.Checked;                 //只更新更新的行?             }         }

    public class customer
    {
        public string name { get; set; }
        public string email { get; set; }
        public string jobless { get; set; }

    }

HTML

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="gridviewUpdate._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="jobless" runat="server" Checked='<%# Eval("jobless").ToString().Equals("true") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="email" />
                <asp:BoundField DataField="name" />

            </Columns>
        </asp:GridView>
    </div>

                  

1 个答案:

答案 0 :(得分:1)

您可以使用GridView.RowUpdating活动.....

<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" OnRowUpdating="TaskGridView_RowUpdating">

在代码背后,

 protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {    
    //logic code here
  }