前台:
<asp:GridView ID="GridView1"runat="server" DataKeyNames="ID"
OnRowEditing="GridView1_RowEditing"OnRowUpdating="GridView1_RowUpdating"OnRowCancelingEdit="GridView1_CancelingEdit"
OnRowDeleting="GridView1_RowDeleting" PageSize="15"AllowPaging="true"OnPageIndexChanging="GridView1_PageIndexChanging"
ShowHeader="true">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server"Text="ID"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server"Text=‘<%#Bind("ID") %>‘ID="Label0"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server"Text="姓名"></asp:Label></HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server"Text=‘<%#Bind("UserName") %>‘ID="Label2"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBoxText=‘<%#Bind("UserName") %>‘runat="server"ID="UserName"></asp:TextBox><asp:RequiredFieldValidator
runat="server" ErrorMessage="不能为空" ControlToValidate="UserName"Display="Dynamic">*</asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server"Text="金额"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server"Text=‘<%#Bind("Money") %>‘ID="Label33"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBoxText=‘<%#Bind("Money")%>‘runat="server"ID="Money"></asp:TextBox>
<asp:RequiredFieldValidatorID="RequiredFieldValidator2" runat="server"ErrorMessage="不能为空"
ControlToValidate="Money"Display="Dynamic">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidatorID="RegularExpressionValidator1" runat="server"ErrorMessage="整数或浮点数"
ValidationExpression="^d*$|[1-9]d*.d*|0.d*[1-9]d*$"ControlToValidate="Money"
Display="Dynamic">*</asp:RegularExpressionValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True"ShowEditButton="True" HeaderText="操作"/>
</Columns>
<HeaderStyle BackColor="blue" ForeColor="white"/>
</asp:GridView>
后台代码:
string myConnectionString =ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString;
SqlConnection myConnection = new SqlConnection();
protectedvoid Page_Load(object sender, EventArgs e)
{
myConnection.ConnectionString =myConnectionString;
if (!IsPostBack)
{
T();
}
}
public void T()
{
myConnection.Open();
string sql = "Select * From Test";
SqlDataAdapter da = new SqlDataAdapter(sq l,myConnection);
DataSet ds = new DataSet();
da.Fill(ds, "Test");
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
myConnection.Close();
}
public void GridView1_RowUpdating(object sender,GridViewUpdateEventArgs e)
{
myConnection.Open();
GridViewRow row = GridView1.Rows[e.RowIndex];
int ID =int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
string userName =((TextBox)row.FindControl("UserName")).Text.ToString();
double Money =Convert.ToDouble(((TextBox)row.FindControl("Money")).Text.ToString());
SqlDataReader dr;
string sql = "Update Test Set UserName=‘" + userName + "‘,Money=" +Money + " Where ID=" + ID + "";
SqlCommand myCommand = new SqlCommand(sql,myConnection);
dr = myCommand.ExecuteReader();
GridView1.EditIndex = -1;
myConnection.Close();
T();
}
public void GridView1_RowDeleting(object sender,GridViewDeleteEventArgs e)
{
myConnection.Open();
GridViewRow row = GridView1.Rows[e.RowIndex];
int ID =int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
SqlDataReader dr;
string sql = "Delete From Test Where ID=" + ID +"";
SqlCommand myCommand = new SqlCommand(sql,myConnection);
dr = myCommand.ExecuteReader();
GridView1.EditIndex = -1;
myConnection.Close();
T();
}
public void GridView1_RowEditing(object sender,GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
T();
}
public void GridView1_CancelingEdit(object sender,GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
T();
}
protected void GridView1_PageIndexChanging(object sender,GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
T();
}