DataGridView
首先,绑定数据
Page_Lode事件
一、绑定数据库字段
二、传值
(1) 绑定HyperlinkFirld字段传值
(2) 自定义绑定超级链接传值
三、分页
四、编辑、更新、取消
1 //编辑
2 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
3 {
4 GridView1.EditIndex = e.NewEditIndex;
5 ShowBind();
6 }
7
8
9
10 //取消
11 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
12 {
13 GridView1.EditIndex = -1;
14 ShowBind();
15 }
//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
string product_name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string amount = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string price = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string barcode = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
string sql = "update products set product_name='" + product_name + "',amount='"
+ amount + "',price='" + price + "',barcode='" + barcode + "' where id=" + id;
SqlConnection conn = new SqlConnection("server=.;database=SuperMarket;uid=sa;pwd=sa");
SqlCommand cmd = new SqlCommand(sql, conn);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
GridView1.EditIndex = -1;
ShowBind();
}
五、删除
//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
string product_name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string amount = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string price = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string barcode = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
string sql = "update products set product_name='" + product_name + "',amount='"
+ amount + "',price='" + price + "',barcode='" + barcode + "' where id=" + id;
SqlConnection conn = new SqlConnection("server=.;database=SuperMarket;uid=sa;pwd=sa");
SqlCommand cmd = new SqlCommand(sql, conn);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
GridView1.EditIndex = -1;
ShowBind();
}
删除对话框
第一种,
//删除提示框
//============= 第一种 ==============
//*事件(在对行进行数据绑定后的激发)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[7].Attributes.Add("onclick", "return confirm('你确认要删除吗?')");
}
}
第二种,事件
//删除提示框
//============= 第二种 ==============
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
//e.Row.RowType判断gridview的数据行,而不是页眉和页脚
if (e.Row.RowType == DataControlRowType.DataRow)
{
//e.Row.Cells[7].Controls.Count > 0 判断单元格7内是否有控件,如果没有则不进行任何操作,注意点击编辑按钮时,删除按钮不可见
if (e.Row.Cells[7].Controls.Count > 0)
{
if (((LinkButton)(e.Row.Cells[7].Controls[0])).Text == "删除")
{
((LinkButton)(e.Row.Cells[7].Controls[0])).Attributes.Add("onclick", "return confirm('你确认要删除吗?')");
//e.Row.Cells[7].Attributes.Add("onclick", "return confirm('你确认要删除吗?')");
}
}
}
}
六、模版