分享Webform GridView列表增删改查结合EF数据绑定的程序代码

时间:2021-04-16

  添加gridview数据到前端

<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" Height="318px" Width="961px" AutoGenerateColumns="false"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowCreated="GridView1_RowCreated">
<Columns>
<asp:BoundField HeaderText="编号" DataField="Uid" ReadOnly="true" />
<asp:TemplateField HeaderText="姓名">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"UserName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"UserName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="操作" ShowEditButton="true" ShowCancelButton="true" ShowDeleteButton="true"
EditText="编辑" UpdateText="修改" DeleteText="&lt;img style=&quot;text-decoration:none; border:0px;&quot; src=&quot;images/delete.gif&quot;onclick=&quot;JavaScript:return confirm ('确认删除吗?')&quot; /&gt;" CancelText="取消" />
</Columns>
</asp:GridView>

<asp:Button ID="btnAdd" runat="server" Text="新增" OnClick="btnAdd_Click" />
</div>
</form>

  后端处理增删该查事件

CEducationEntities context = new CEducationEntities();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBind();
}
}
//绑定GridView数据
private void DataBind()
{
List<User> users = context.User.Where(m => m.Uid != string.Empty).ToList();
this.GridView1.DataSource = users;
this.GridView1.DataBind();
}

//编辑选中列表
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
int index = e.NewEditIndex;
GridView1.EditIndex = index;
DataBind();
}

//修改数据和插入更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = e.RowIndex;
GridViewRow rows = this.GridView1.Rows[index];
string name = ((TextBox)rows.FindControl("txtName")).Text.Trim();
string ID = rows.Cells[0].Text;
if (!string.IsNullOrEmpty(ID) && ID != "&nbsp;")
{
var sc = context.User.First(p => p.Uid.Equals(ID));
sc.UserName = name;
context.SaveChanges();
}
else
{
char[] pattern = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
int n = pattern.Length;
string result = "";
Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < 4; i++)
{
int rnd = random.Next(0, n);
result += pattern[rnd];
}
User u = new User()
{
Uid = Guid.NewGuid().ToString(),
Keyword = result,
UserName = ((TextBox)rows.FindControl("txtName")).Text.Trim(),
userLog = string.Empty
};
context.User.Add(u);
context.SaveChanges();
}
this.GridView1.EditIndex = -1;
DataBind();
}

//取消修改
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DataBind();
}

//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string strUid = GridView1.Rows[e.RowIndex].Cells[0].Text;
User users = new User() { Uid = strUid };
context.User.Attach(users);
context.User.Remove(users);
context.SaveChanges();
DataBind();
}

//新增按钮
protected void btnAdd_Click(object sender, EventArgs e)
{
List<User> list = context.User.Where(m => m.Uid != string.Empty).ToList();
User firstUser = new User();
list.Insert(list.Count, firstUser);
this.GridView1.DataSource = list;
this.GridView1.EditIndex = list.Count - 1;
this.GridView1.DataBind();
}

//创建新行
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.EmptyDataRow)
{
e.Row.Cells[0].Enabled = false;
}
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章