(1)步骤
1.从工具箱的数据一栏拖一个GridView和SqlDataSource控件到设计页面,给SqlDataSource配置数据源,单击配置数据源->选择要连接的数据库数据连接->选择指定来自表和视图的列选择某一张表和需要的字段->点击高级选择生成SELECT,UPDATE,SELECT语句->测试数据完成
2.给GridView选择数据源,勾选启动分页,启动编辑,启动删除,运行就会出现如下(你的数据表中数据):
(2)这里的编辑和删除本是可以实现的,但由于表与表之间有关系,所以删除某行数据时会受REFERENCE 约束,与其他表冲突,因此我们只能自己添加控件来实现客户需求.如下图:
以上添加如图所示的编辑、删除控件源代码(这里只给出一个字段的形式,其他类似):
<Columns>
<asp:BoundField DataField="customerNo" HeaderText="客户编号" ReadOnly="True"
SortExpression="customerNo" />
<asp:TemplateField HeaderText="客户名">
<EditItemTemplate>
<asp:TextBox ID="TextBox_row_customerName" runat="server"
style="margin-bottom: 0px">asp:TextBox>
<asp:HiddenField ID="HiddenField_row_customerName" runat="server" Value='<%# Eval("customerName") %>' />
EditItemTemplate>
<ItemTemplate>
<%# Eval("customerName")%>
ItemTemplate>
asp:TemplateField>
(3)添加控件后,我们要做的就是完善代码
//1.绑定数据
public void bind()
{
string sql = "";
sql = "select * from [Customers]";
if (TextBox_CompanyName.Text.Trim() == "" && TextBox_contactname.Text.Trim() == "")
{
sql = "select * from Customers";
}
else
{
sql = "select * from Customers where CompanyName like '%" + TextBox_CompanyName.Text + "%'"
+ " and ContactName like '%" + TextBox_contactname.Text + "%'";
}
//设置SqlDataSource 执行sql语句
SqlDataSource2.SelectCommand = sql;
//设置GridView中DataSourceID
GridView1.DataSourceID = SqlDataSource2.ID;
//绑定GridView数据
GridView1.DataBind();
}
//2.删除数据
protected void GridView_customer_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string number = GridView1.Rows[e.RowIndex].Cells[0].Text.ToString();
//先把orderdetails的数据清空
SqlDataSource2.DeleteCommand = "delete from [Order Details]";
SqlDataSource2.Delete();
//首先删除Orders表中的关联数据
SqlDataSource2.DeleteCommand = "delete from [Orders] where CustomerID = '" + number + "'";
SqlDataSource2.Delete();
//删除选中数据
SqlDataSource2.DeleteCommand = "delete from [Customers] where CustomerID = '" + number + "'";
SqlDataSource2.Delete();
bind();
}
//3.搜索
protected void Button_customer_search_Click(object sender, EventArgs e)
{
bind();
}
//4.编辑时激发
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
}
//5.绑定数据后激发
protected void customer_dataBond(object sender, GridViewRowEventArgs e)
{
TextBox TextBox_row_customerName = (TextBox)e.Row.FindControl("TextBox_row_customerName") as TextBox;
//e.Row.RowType
if (e.Row.RowType == DataControlRowType.DataRow)
{
//客户名
if (TextBox_row_customerName != null)
{
TextBox_row_customerName.Text = ((HiddenField)e.Row.FindControl("HiddenField_row_customerName")).Value;
}
//电话
TextBox TextBox_row_telephone = (TextBox)e.Row.FindControl("TextBox_row_telephone") as TextBox;
if (TextBox_row_telephone != null)
{
TextBox_row_telephone.Text = ((HiddenField)e.Row.FindControl("HiddenField_row_telephone")).Value;
}
//地址
TextBox TextBox_row_address = (TextBox)e.Row.FindControl("TextBox_row_address") as TextBox;
if (TextBox_row_address != null)
{
TextBox_row_address.Text = ((HiddenField)e.Row.FindControl("HiddenField_row_address")).Value;
}
//邮编
TextBox TextBox_row_zip= (TextBox)e.Row.FindControl("TextBox_row_zip") as TextBox;
if (TextBox_row_zip != null)
{
TextBox_row_zip.Text = ((HiddenField)e.Row.FindControl("HiddenField_row_zip")).Value;
}
}
}
//6.更新数据
protected void gdvw_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//用户名
TextBox TextBox_row_customerName = gdvw.Rows[e.RowIndex].FindControl("TextBox_row_customerName") as TextBox;
//姓名
TextBox TextBox_row_telephone = gdvw.Rows[e.RowIndex].FindControl("TextBox_row_telephone") as TextBox;
//邮箱
TextBox TextBox_row_address = gdvw.Rows[e.RowIndex].FindControl("TextBox_row_address") as TextBox;
//电话
TextBox TextBox_row_zip = gdvw.Rows[e.RowIndex].FindControl("TextBox_row_zip") as TextBox;
string number = gdvw.Rows[e.RowIndex].Cells[0].Text.ToString();
string sql = "";
if (TextBox_row_customerName != null && TextBox_row_customerName.Text != "")
{
sql += " customerName='" + TextBox_row_customerName.Text + "',";
}
if (TextBox_row_telephone != null && TextBox_row_telephone.Text != "")
{
sql += " telephone='" + TextBox_row_telephone.Text + "',";
}
if (TextBox_row_address != null && TextBox_row_address.Text != "")
{
sql += " address='" + TextBox_row_address.Text + "',";
}
if (TextBox_row_zip != null && TextBox_row_zip.Text != "")
{
sql += " zip='" + TextBox_row_zip.Text + "',";
}
if (sql != "")
{
sql = sql.Remove(sql.LastIndexOf(","), 1);
sql = "update Customer set " + sql + " where customerNo= '" + number + "'";//like '%" + textbox_customerName.Text + "%' "
custmdatsoce.UpdateCommand = sql;
custmdatsoce.Update();
bind();
}
}
protected void gdvw_RowEditing(object sender, GridViewEditEventArgs e)
{
gdvw.EditIndex = e.NewEditIndex;
}
}
(1)配置数据源同上
(2)属性上ListView本身有启动编辑,启动删除,比GridView还多了一个启动插入,不需要添加代码,就能实现,但删除时与GridView类似出现相同的问题,同样需要我们添加代码(3)代码与GridView也类似,需要注意以下几点
1. 删除数据和数据更新中
string number = listVw.DataKeys[e.ItemIndex].Value.ToString();
2. GridView中获取要创建或绑定数据的行是通过Row而ListView中获取要创建或绑定数据的项要通过Item
3. 数据绑定中
if (e.Item.ItemType == ListViewItemType.DataItem)