首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

datagrid怎么实现数据行的删除、编辑

2012-01-08 
datagrid如何实现数据行的删除、编辑datagrid有几列数据,其中前两列为数据,后两列分别显示删除和编辑的超链

datagrid如何实现数据行的删除、编辑
datagrid有几列数据,其中前两列为数据,后两列分别显示删除和编辑的超链接,点击超链接就可以对数据进行相关的操作
该如何实现,望指教
谢谢

[解决办法]
使用VS2005里面的GridView对象更好哦
[解决办法]

如何在DataGrid控件中实现编辑、删除、分类以及分页操作
德仔工作室:http://www.dezai.cn/Article_Show.asp?ArticleID=7680
[解决办法]
我用的是2005,这些会对你有帮助的.
好好看看.注意事件.

//删除
32 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
33 {
34 string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
35 sqlcon = new SqlConnection(strCon);
36 sqlcom = new SqlCommand(sqlstr,sqlcon);
37 sqlcon.Open();
38 sqlcom.ExecuteNonQuery();
39 sqlcon.Close();
40 bind();
41 }
42
43 //更新
44 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
45 {
46 sqlcon = new SqlConnection(strCon);
47 string sqlstr = "update 表 set 字段1='"
48 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
49 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
50 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='" 
51 + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
52 sqlcom=new SqlCommand(sqlstr,sqlcon);
53 sqlcon.Open();
54 sqlcom.ExecuteNonQuery();
55 sqlcon.Close();
56 GridView1.EditIndex = -1;
57 bind();
58 }
59
60 //取消
61 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
62 {
63 GridView1.EditIndex = -1;
64 bind();
65 }


[解决办法]
public partial class _Default : System.Web.UI.Page
{
SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
conn = new SqlConnection("uid=sa;database=northwind");
if (!IsPostBack)
{
//WritePictrue();
GridView1Bind();
}
}
//数据绑定
public void GridView1Bind()
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select employeeid,lastname+firstname as 姓名,city as 城市,address as 地址 from employees", conn);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
//删除事件(先要设置GridView1属性中的数据中的DataKeyNames的值=删除数据库中那一列的列名)
protected void GridView1_DeleteCommand(object source, DataListCommandEventArgs e)
{
//以id为删除条件
string id = GridView1.DataKeys[e.Item.ItemIndex].ToString();
SqlCommand comm = new SqlCommand("delete from employees where employeeid='" + id + "'", conn);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
GridView1Bind();
}
[解决办法]
动态设置
public partial class Default2 : System.Web.UI.Page
{
SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
conn = new SqlConnection("uid=sa;database=northwind");
if (!IsPostBack)
{
SetGridStyle();
SetField();
GridBind();
}


}
public void GridBind()
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select customerid,companyname,city,address from customers", conn);
da.Fill(ds);
gridInfo.DataSource = ds;
gridInfo.DataBind();

}
public void SetGridStyle()
{
gridInfo.AutoGenerateColumns = false;
gridInfo.AllowPaging = true;
gridInfo.PageSize = 2;
gridInfo.PagerSettings.Position = PagerPosition.Bottom;
gridInfo.PagerStyle.HorizontalAlign = HorizontalAlign.Center;
gridInfo.HeaderStyle.BackColor = Color.SlateBlue;
gridInfo.RowStyle.BackColor = Color.SpringGreen;
gridInfo.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast;
gridInfo.PagerSettings.FirstPageText = "首页";
gridInfo.PagerSettings.LastPageText = "尾页";
gridInfo.PagerSettings.PreviousPageText = "上一页";
gridInfo.PagerSettings.NextPageText = "下一页";
}
public void SetField()
{
string[] keyNames = new string[] { "customerid" };
gridInfo.DataKeyNames = keyNames;

//创建绑定列
BoundField xh = new BoundField();
BoundField gsm = new BoundField();
BoundField cs = new BoundField();
BoundField dz = new BoundField();

xh.HeaderText = "序号";
xh.ReadOnly = true;
xh.ItemStyle.Wrap = false;

gsm.HeaderText = "公司名";
gsm.DataField = "companyname";
gsm.ItemStyle.Wrap = false;
gsm.ReadOnly = true;

cs.HeaderText = "城市";
cs.DataField = "city";
cs.ReadOnly = true;
cs.ItemStyle.Wrap=false;

dz.HeaderText = "地址";
dz.DataField = "address";
dz.ReadOnly = false;
dz.ItemStyle.Wrap = false;

//创建命令按钮列
CommandField edit = new CommandField();
edit.HeaderText = "编辑";
edit.ShowCancelButton = true;
edit.ShowEditButton = true;
//edit.ShowDeleteButton = true;
edit.ButtonType = ButtonType.Button;
edit.CancelText = "取消";
edit.EditText = "编辑";
//edit.DeleteText = "删除";
edit.ShowHeader = true;

//创建一个模板列
TemplateField delete = new TemplateField();
delete.ItemTemplate = new GridTemlate(DataControlRowType.DataRow, "删除", "btDelete");
delete.HeaderText = "删除";





//把创建好的列绑定到GridView上
gridInfo.Columns.Add(xh);
gridInfo.Columns.Add(gsm);
gridInfo.Columns.Add(cs);
gridInfo.Columns.Add(dz);
gridInfo.Columns.Add(edit);
gridInfo.Columns.Add(delete);


}
protected void gridInfo_RowEditing(object sender, GridViewEditEventArgs e)
{
gridInfo.EditIndex = e.NewEditIndex;
GridBind();
}
protected void gridInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gridInfo.EditIndex = -1;
GridBind();
}

}
class GridTemlate:ITemplate
{

private DataControlRowType rowType;//行的类型
private string colunmName;//列名
private string cid;//控件id
public GridTemlate(DataControlRowType rowType, string name, string id)
{
this.rowType = rowType;
this.colunmName = name;
this.cid = id;
}

public void InstantiateIn(Control container)
{

switch (rowType)

case DataControlRowType.Header:
Literal msg = new Literal();


msg.Text = colunmName;
msg.ID = cid;
container.Controls.Add(msg);
break;
case DataControlRowType.DataRow:
Button bt = new Button();
bt.Text = colunmName;
bt.ID = cid;
bt.Attributes["onclick"] = "return confirm('确实要删除此项吗?')";

container.Controls.Add(bt);
break;
default:
break;

}
}

 
}

热点排行