GridView控件是Asp.net 1.1版本流行控件DataGrid的继承者,功能比DataGrid增强不少,但是也有很大的不同啊。将最近使用这个控件的经验同各位同学分享如下:
1\掩藏字段的处理:DataGrid可以将字段直接设置为Visible=false,可以通过Cell[x].Text取到值。 GridView这个功能失效了,可以使用运行时来设定该列为掩藏。处理RowDataBound事件。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[5].Visible = false;
}
2\ 获取所选列的数据:DataGrid可以直接通过所选行来获取,GridView同样的代码无法运行。GridView 可以通过GridViewRow来获取。BtnAudit是模版列中的按钮。
GridViewRow grdRow = (GridViewRow)btnAudit.Parent.Parent;
string strId = grdRow.Cells[0].Text;
string memberId = grdRow.Cells[5].Text;
3\ 最终删除一条数据之前进行确认,这个可以使用摸版列,在摸版列中放置按钮控件,其中有一个客户端事件onclientclick,这里可以写确认处理javascript脚本.例如:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnRefuse" runat="server" OnClick="btnRefuse_Click" Text="拒绝" OnClientClick="return confirm(' 你真的要拒绝这个用户加入俱乐部?')"/>
</ItemTemplate>
</asp:TemplateField>
ObjectDataSource In Depth
posted @ 2005-12-22 23:06
geff zhang 阅读(35594)
评论(43) 编辑 收藏 所属分类:
.net framework
发表评论
这几天在用这个控件做一些演示。发觉得这个控件似乎只是个半成品。或者说我对GridView还不习惯。。。。。。
1、无法绑定控记录。当GridView去绑定DataSet时,如果DataSet是空记录。则GridView连表头都不显示,感到很郁闷。。。。。。
2、绑定列对于时间的格式好像支持的有错误。
比如我有个时间字段datetime="2005-12-23 10:01:00"
当我的GridView使用绑定列,就无法设置格式。
<asp:BoundField DataField="datetime" DataFormatString="{0:HH:mm}" />
这样,在列中,它还是显示:2005-12-23 10:01:00
如果我使用模板列,却是成功的。
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("datetime", "{0:HH:mm}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
这样,在列中,它显示:10:01
可用DataGrid却不会出此问题。。。。。。
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
int id =Convert.ToInt32(GridView1.SelectedDataKey.Value);
}
Behavior-> HtmlEncode
我自己手动设置datasource
然后怎么设置HTMLENCODE?
#10楼[
楼主]2006-03-09 21:39 |
@尖锐湿疣
可以在事件GridView1_RowCreated中进行处理
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
DataControlField c = GridView1.Columns[2];
if (c is BoundField)
{
BoundField bf = (BoundField)c;
bf.HtmlEncode = true;
}
}
#11楼[
楼主]2006-03-10 22:18 |
#12楼[
楼主]2006-03-10 22:20 |
#13楼[
楼主]2006-03-21 20:22 |
Show Header/Footer of Gridview with Empty Data Source
public void BuildNoRecords(GridView gridView, DataSet ds)
{
try {
if (ds.Tables(0).Rows.Count == 0) {
ds.Tables(0).Rows.Add(ds.Tables(0).NewRow());
gridView.DataSource = ds;
gridView.DataBind();
int columnCount = gridView.Rows(0).Cells.Count;
gridView.Rows(0).Cells.Clear();
gridView.Rows(0).Cells.Add(new TableCell());
gridView.Rows(0).Cells(0).ColumnSpan = columnCount;
gridView.Rows(0).Cells(0).Text = "No Records Found.";
}
} catch (Exception ex) {
}
}
#14楼[
楼主]2006-03-21 20:23 |
@感到郁闷
分页读取数据,用第3方的分页控件显示分页条,DATAGRID只显示当页数据.
2\ 获取所选列的数据:DataGrid可以直接通过所选行来获取,GridView同样的代码无法运行。GridView 可以通过GridViewRow来获取。BtnAudit是模版列中的按钮。
GridViewRow grdRow = (GridViewRow)btnAudit.Parent.Parent;
string strId = grdRow.Cells[0].Text;
string memberId = grdRow.Cells[5].Text;
这段代码放到GridView的什么事件中?
我放到RowCommand()事件中根本就取不到,而且报的错误信息.
指点
#20楼[
楼主]2006-08-06 00:01 |
#21楼[
楼主]2006-08-06 11:28 |
@RedVesper
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="name" HeaderText="俱乐部名称" />
<asp:BoundField DataField="username" HeaderText="用户姓名" />
<asp:BoundField DataField="city" HeaderText="所在城市" />
<asp:BoundField DataField="Company" HeaderText="服务的公司" />
<asp:BoundField DataField="memberid" HeaderText="用户ID" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnAudit" runat="server" CausesValidation="false" OnClick= "btnAudit_Click" Text="同意" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnRefuse" runat="server" OnClick="btnRefuse_Click" Text="拒绝" OnClientClick="return confirm(' 你真的要拒绝这个用户加入俱乐部?')"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
</asp:GridView>
/// <summary>
/// 拒绝成员注册审核
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnRefuse_Click(object sender, EventArgs e)
{
try
{
Button btnRefuse = (Button)sender;
GridViewRow grdRow = (GridViewRow)btnRefuse.Parent.Parent;
string strId = grdRow.Cells[0].Text;
string memberId = grdRow.Cells[7].Text;
ClubMemberManager.AuditClubMemberInfo(Convert.ToInt32(strId),memberId, -1);
}
catch (BusinessLayerException ex)
{
this.ProcessLogException(ex);
}
}
#22楼[
楼主]2006-08-30 14:21 |
#24楼[
楼主]2006-09-11 10:19 |
@zhang[匿名]
可以在RowCreated事件中赋予控件CommandArgument属性中保存当前行的ID,例如下面的处理方式
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = this.grvCompany.Rows[index];
string companyID = ((Label)row.Cells[0].FindControl("lblID")).Text;
……
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnbCompanyDetail = (LinkButton)e.Row.FindControl("lnbCompanyDetail");
LinkButton lnbLocations = (LinkButton)e.Row.FindControl("lnbLocations");
LinkButton lnbUsers = (LinkButton)e.Row.FindControl("lnbUsers");
lnbCompanyDetail.CommandArgument = e.Row.RowIndex.ToString();
lnbLocations.CommandArgument = e.Row.RowIndex.ToString();
lnbUsers.CommandArgument = e.Row.RowIndex.ToString();
}
}
#28楼[
楼主]2006-10-18 09:44 |
#30楼[
楼主]2006-11-04 09:04 |
在第一条中如果直接写e.Row.Cells[5].Visible = false;
会出错
必须在下面两条语句中都写
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[3].Visible = false;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[3].Visible = false;
}
#37楼[
楼主]2008-07-15 09:23 |
@fangzi
e.Row.Cells[0].Visible 这是服务端,这样就没有把这一列输出到客户端了,所以在客户端的Js中是取不到的。
--引用--------------------------------------------------
fangzi: 我在 RowDataBound 事件里面写了 e.Row.Cells[0].Visible = false;
<br>来隐藏,但是在脚本里却获得不到第一列的值。并且选中另外一行的话,修改层里的数据不会随之变化。(层与Gridview是同在一个页面)
<br>
--------------------------------------------------------
可以把你需要发生到客户端的字段设置在DataKeyNames就可以取值