DataGridView

DataGridView

首先,绑定数据

clip_image002

Page_Lode事件

clip_image004

clip_image006

一、绑定数据库字段

clip_image008

二、传值

(1) 绑定HyperlinkFirld字段传值

clip_image010

clip_image012

(2) 自定义绑定超级链接传值

clip_image006[1]

clip_image014

clip_image016

clip_image018

三、分页

clip_image020

clip_image022

clip_image024

四、编辑、更新、取消

 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 }


clip_image026

//更新

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();

}

五、删除

clip_image028

//更新

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();

}

删除对话框

第一种,

clip_image030

//删除提示框

//============= 第一种 ==============

//*事件(在对行进行数据绑定后的激发)

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

e.Row.Cells[7].Attributes.Add("onclick", "return confirm('你确认要删除吗?')");

}

}

第二种,事件

clip_image032

//删除提示框

//============= 第二种 ==============

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('你确认要删除吗?')");

}

}

}

}

六、模版

clip_image034

原文地址:https://www.cnblogs.com/tangge/p/2212028.html