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

菜鸟请教一Gridview有关问题!

2011-12-23 
菜鸟请教一Gridview问题!!!Gridview绑定数据后怎么样实现数据的编辑和删除功能!也就是说编辑中的更新和取

菜鸟请教一Gridview问题!!!
Gridview绑定数据后怎么样实现数据的编辑和删除功能!
也就是说编辑中的更新和取消,还有删除这三个事件怎么写?
希望各位大虾能说的详细一点,更给出详细的代码更好,谢谢!!!

[解决办法]
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "Data Source=(local);Database=db_04;Uid=jiang;Pwd=123 ";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from tb_Member where id= ' " + GridView1.DataKeys[e.RowIndex].Value.ToString() + " ' ";
sqlcon = new SqlConnection(strCon);
sqlcom = new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "update tb_Member set name= ' "
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + " ',sex= ' "
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + " ',nPlace= ' "
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + " ' where id= ' "
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + " ' ";
sqlcom=new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
public void bind()
{
string sqlstr = "select * from tb_Member ";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "tb_Member ");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "id " };
GridView1.DataBind();
sqlcon.Close();
}
[解决办法]
后台

<html xmlns= "http://www.w3.org/1999/xhtml ">
<head runat= "server ">
<title> Untitled Page </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div align= "left ">
&nbsp; <table width= "100% ">
<tr>
<td style= "width: 56px; height: 237px; ">
</td>
<td align= "left " style= "height: 237px ">
<asp:GridView ID= "GridView1 " AllowSorting= "True " AllowPaging= "True " PageSize= "3 "
AutoGenerateColumns= "False " runat= "server " OnPageIndexChanging= "GridView1_PageIndexChanging "


OnSorting= "GridView1_Sorting " BackColor= "#DEBA84 " BorderColor= "#DEBA84 " BorderStyle= "None "
BorderWidth= "1px " CellPadding= "3 " CellSpacing= "2 " OnRowDataBound= "GridView1_RowDataBound "
OnRowCommand= "GridView1_RowCommand " OnRowCancelingEdit= "GridView1_RowCancelingEdit "
OnRowEditing= "GridView1_RowEditing " OnRowUpdating= "GridView1_RowUpdating " OnRowCreated= "GridView1_RowCreated "
ShowFooter= "True " >
<Columns>
<asp:TemplateField ItemStyle-Width= "20px ">
<ItemTemplate>
<asp:CheckBox ID= "CheckBox1 " runat= "server " />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText= "用户ID " ItemStyle-Width= "150px " ReadOnly= "true " DataFormatString= "此用户的ID是{0} "
SortExpression= "UserID " DataField= "UserID " />
<asp:BoundField HeaderText= "用户名 " ItemStyle-Width= "80px " DataField= "UserName " />
<asp:BoundField HeaderText= "用户性别 " ItemStyle-Width= "100px " DataField= "UserSex " />
<asp:BoundField HeaderText= "用户地址 " ItemStyle-Width= "150px " DataField= "UserAddress " />
<asp:BoundField HeaderText= "工资 " ItemStyle-Width= "60px " DataField= "UserPay " />
<asp:BoundField HeaderText= "状态 " ItemStyle-Width= "60px " DataField= "State " />
<asp:CommandField ShowEditButton= "True " EditText= "修改 " ItemStyle-Width= "90px " UpdateText= "更新 " CancelText= "取消 "
HeaderText= "编辑 " />
<asp:TemplateField ItemStyle-Width= "60px ">
<ItemTemplate>
<asp:LinkButton ID= "LinkButton1 " CommandArgument= "DeleteR " runat= "server "> 删除 </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor= "#F7DFB5 " ForeColor= "#8C4510 " />
<RowStyle BackColor= "#FFF7E7 " ForeColor= "#8C4510 " />
<SelectedRowStyle BackColor= "#738A9C " Font-Bold= "True " ForeColor= "White " />
<PagerStyle ForeColor= "#8C4510 " HorizontalAlign= "Left " />
<HeaderStyle BackColor= "#A55129 " Font-Bold= "True " ForeColor= "White " />
</asp:GridView>
<asp:CheckBox ID= "CheckBox2 " runat= "server " AutoPostBack= "True " OnCheckedChanged= "CheckBox2_CheckedChanged "


Text= "全选 " />
<asp:Button ID= "Button1 " runat= "server " OnClick= "Button1_Click " Text= "状态为1 " />
</td>
</tr>
</table>
&nbsp;
<asp:HiddenField ID= "HiddenField1 " runat= "server " />
<asp:HiddenField ID= "HiddenField2 " runat= "server " />
</div>
</form>
</body>
</html>

[解决办法]
ding
[解决办法]
#region 自带编辑
protected void AfficheGV_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int ID = Convert.ToInt32(((TextBox)(AfficheGV.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim());
string DC = ((TextBox)(AfficheGV.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
Mybf.UpdateInforBF(ID, DC);
AfficheGV.EditIndex = -1;
BindGV();
}
protected void AfficheGV_RowEditing(object sender, GridViewEditEventArgs e)
{
AfficheGV.EditIndex = e.NewEditIndex;
BindGV();
}
protected void AfficheGV_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

Mybf.DeleteInforBF(Convert.ToInt32(AfficheGV.Rows[e.RowIndex].Cells[0].Text));
BindGV();

}

protected void AfficheGV_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
AfficheGV.EditIndex = -1;
BindGV();
}
#endregion
[解决办法]
好东西,顶
[解决办法]
网上有很多教程 看一下视频会更明白一些
[解决办法]
学习中~
[解决办法]
来看看!
[解决办法]
应该渐渐学会使用objectdatasource,你会发现好处多多,想你这种功能,不用写代码就实现了
[解决办法]
而且有利于程序和数据库的移植哦
[解决办法]
MSDN里有说明
[解决办法]
我的后台,比较笨,但适用于初学:
protected void Gv_hy_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
rowHeader.BackColor = System.Drawing.Color.Gray;
rowHeader.Font.Bold = true;
TableCellCollection cells = e.Row.Cells;
TableCell headerCell = new TableCell();
headerCell.Text = "时间 ";
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "明细 ";
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "8点前 ";
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);


headerCell = new TableCell();
headerCell.Text = "8:00 ";
headerCell.ColumnSpan = 2;
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "9:00 ";
headerCell.ColumnSpan = 2;
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "10:00 ";
headerCell.ColumnSpan = 2;
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "11:00 ";
headerCell.ColumnSpan = 2;
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "12:00 ";
headerCell.ColumnSpan = 2;
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "13:00 ";
headerCell.ColumnSpan = 2;
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "14:00 ";
headerCell.ColumnSpan = 2;
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "15:00 ";
headerCell.ColumnSpan = 2;
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "16:00 ";
headerCell.ColumnSpan = 2;
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "17:00 ";
headerCell.ColumnSpan = 2;
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "18:00 ";
headerCell.ColumnSpan = 2;
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "19:00 ";
headerCell.ColumnSpan = 2;
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "20:00 ";
headerCell.ColumnSpan = 2;
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
headerCell = new TableCell();
headerCell.Text = "21:00后 ";
headerCell.ColumnSpan = 1;
headerCell.HorizontalAlign = HorizontalAlign.Center;
rowHeader.Cells.Add(headerCell);
rowHeader.Visible = true;
Gv_hy.Controls[0].Controls.AddAt(0, rowHeader);
}

}
前台和楼上几位差不多。
[解决办法]
强!!!
[解决办法]
GV+SQLdatasource可以自己產生code不用自己寫阿
[解决办法]
建议查看一下MSN的帮助文档.
[解决办法]
都被说完了!!


[解决办法]
mark
[解决办法]
晕 FFFF了 都说了这么多
[解决办法]
不错!都贴了这么多代码了!
[解决办法]
接分~

热点排行