此段代码放置在和里,作用通过建立一个模版,在GridView控件中添加一列,此列中包含一个文本框,将文本框中绑定到book_id列。
5、 参照PPT中的内容,添加gridview3.aspx页面,在此页面中只添加一个GridView控件。通过编程来实现在GridView中编辑数据。
A、首先通过编程将book
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
_info表中的数据通过GridView显示;
B、再在GridView中添加“编辑”按钮:在”源“视图中在中添加代码:
asp:CommandFieldShowEditButton=“true”/
C、编辑GridView控件的RowEditing、RowUpdating、RowCancelingEdit事件。
注:e.RowIndex 为被编辑行在GridView中的行号(从0开始)
GridView1.Rows[e.RowIndex]. 为GridView中被编辑的行。
GridView1.Rows[e.RowIndex].Cells[1]. 为GridView中被编辑的行的第2列。
GridView1.Rows[e.RowIndex].Cells[1].Controls[0]为GridView中被编辑的行的第2列中的第一个控件。
**GridView.aspx
**
<%@ Page Language=“C#” AutoEventWireup=“true” CodeFile=“GridView.aspx.cs” Inherits=“GridView” %>
<asp:GridView ID=“GridView1” runat=“server” AutoGenerateColumns=“False”
DataKeyNames=“book_id” DataSourceID=“SqlDataSource1”
EnableModelValidation=“True”>
<asp:BoundField DataField=“book_name” HeaderText=“book_name”
SortExpression=“book_name” />
<asp:BoundField DataField=“author” HeaderText=“author”
SortExpression=“author” />
<asp:BoundField DataField=“price” HeaderText=“price” SortExpression=“price” />
<asp:ImageField DataImageUrlField=“image_path” HeaderText=“图片”>
</asp:ImageField>
<asp:HyperLinkField HeaderText=“详细信息” NavigateUrl="~/Default.aspx"
Text=“详细信息” />
<asp:templatefield headertext=“Type”>
<asp:TextBox ID=“TextBox1” datasourceid=“SqlDataSource1”
runat=“server” Text=’<%# Eval(“book_id”) %>’></asp:TextBox>
</asp:templatefield>
</asp:GridView>
<asp:SqlDataSource ID=“SqlDataSource1” runat=“server”
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand=“SELECT * FROM [book_info]”></asp:SqlDataSource>
gridview3.aspx
<%@ Page Language=“C#” AutoEventWireup=“true” CodeFile=“GridView3.aspx.cs” Inherits=“GridView3” MaintainScrollPositionOnPostback=“True” %>
<asp:GridView ID=“GridView1” runat=“server” EnableModelValidation=“True”
onrowcancelingedit=“GridView1_RowCancelingEdit”
onrowediting=“GridView1_RowEditing” onrowupdating=“GridView1_RowUpdating”
<asp:CommandField ShowEditButton=“True” />
</asp:GridView>
gridview3.aspx.cx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class GridView3 : System.Web.UI.Page
{
//创建连接对象
public SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[“testConnectionString”].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
myBind();
}
}
///
/// GridViews数据绑定
///
public void myBind()
{
string sqlStr = “select * from book_info”;
SqlDataAdapter myDataAdapter = new SqlDataAdapter(sqlStr, conn);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet, “book_info”);
GridView1.DataSource = myDataSet;
GridView1.DataBind();
}
//编辑
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//设计编辑行的索引
GridView1.EditIndex = e.NewEditIndex;
myBind();
}