GridView 用法 基础使用并不难
无非写一个bind()方法查询数据并绑定到GridView中
绑定GridView的时候用到两个方法:
1)GridView.DataSource=dt;
2)GridView.DataBind();
然后每次操作的时候bind()一下
编辑 删除
也就这几个步骤
1)在GridView中添加 CommandField 选中添加or删除or查询
2)在后台复写几个方法
/**
* 编辑点击事件
*/
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Bind();
}
/**
* 修改事件
*/
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string StudentCheck = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.Trim();
string StudentID = GridView1.DataKeys[e.RowIndex][0].ToString().Trim();
string ClassTime = GridView1.DataKeys[e.RowIndex][1].ToString().Trim();
string ClassWeeks = GridView1.DataKeys[e.RowIndex][2].ToString().Trim();
string ClassNumber = GridView1.DataKeys[e.RowIndex][3].ToString().Trim();
if (AddSQLStringToDAL.UpDateStudentCheck(StudentCheck, StudentID, ClassTime, ClassWeeks, ClassNumber))
{
//Label3.Text = StudentCheck+ StudentID+ ClassWeeks + ClassTime+ ClassNumber;
GridView1.EditIndex = -1;
Bind();
}
}
/**
* 取消事件
*/
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Bind();
}
/**
* 删除事件
*/
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string StudentID = GridView1.DataKeys[e.RowIndex][0].ToString().Trim();
string ClassTime = GridView1.DataKeys[e.RowIndex][1].ToString().Trim();
string ClassWeeks = GridView1.DataKeys[e.RowIndex][2].ToString().Trim();
string ClassNumber = GridView1.DataKeys[e.RowIndex][3].ToString().Trim();
if (AddSQLStringToDAL.DelStudentCheck( StudentID, ClassTime, ClassWeeks, ClassNumber))
{
//Label3.Text = StudentCheck+ StudentID+ ClassWeeks + ClassTime+ ClassNumber;
GridView1.EditIndex = -1;
Bind();
}
}
现在呢 我点击编辑的时候 这一行的 每一个列都会编程一个可编辑的TextView
但是 我有一个需求 当我点击编辑的时候 这些列 变成一个可编辑的DropDownList 不需要用户输入
在GridView中除了 有CommandField 还有很多 这里 用到了 TemplateField
这是个啥呢?
列类型是TemplateField,它可以使用模板完全定制列的内容。
提示:
控件模板只是一个可以添加其他内容的容器,如文本、HTML控件甚或ASP.NET控件。
TemplateField提供了6个不同的模板,用于定制列的指定区域,或创建列中的单元格能进入的模式,如编辑模式。表7-6列出了这些模板。
表 7-6
模 板 名 | 说 明 |
ItemTemplate | 用于显示数据绑定控件的TemplateField中的一项 |
AlternatingItemTemplate | 用于显示TemplateField的替换项 |
EditItemTemplate | 用于显示编辑模式下的TemplateField项 |
InsertItemTemplate | 用于显示插入模式下的TemplateField项 |
HeaderTemplate | 用于显示TemplateField的标题部分 |
FooterTemplate | 用于显示TemplateField的脚标部分 |
要在GridView中使用TemplateField,只需用上一节介绍的Add Field对话框给栅格添加列类型。添加了字段后,就会在GridView中添加一个新的<asp: TemplateField>标记
如果看不懂的话 我觉得直接看代码会更好点
<asp:TemplateField HeaderText="chuqin">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem>asd</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("StudentCheck") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
再看看是什么样子
点击编辑之前(数据什么的无视就好!!!!)
点击编辑之后
回过头来看下源码什么意思
EditItemTemplate:用于显示编辑模式下的TemplateField项 在这里插入了一个DropDownList 并设置ListItem
ItemTemplate:用于显示数据绑定控件的TemplateField中的一项 这里用一个Lable 来显示 没有点击编辑时的数据
<%# Bind("StudentCheck") %> 这个在前台绑定数据的代码 在Repeater 中也用到过
可以当成GridView中 BoundField 里的DataField 绑定数据 QwQ
后台的代码什么的 无非就是 把原来的获取TextView 改成获取DropDownList中的ListItem 的Text
就不多说了~