GridView控件使用常见问题

    技术2022-05-11  68

    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 on 2005-12-22 23:06 自由、创新、研究、探索…… 阅读(6970) 评论(32)  编辑 收藏 引用 网摘 所属分类: DotNet 2.0

    <script type="text/javascript"> // </script>

    评论

     

    Cells[x].Text取到值  

    回复  更多评论   

    # re: GridView控件使用经验 2005-12-23 10:08 NeedForSleep

    这几天在用这个控件做一些演示。发觉得这个控件似乎只是个半成品。或者说我对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却不会出此问题。。。。。。  回复  更多评论   

    # re: GridView控件使用经验 2005-12-23 12:59 james.yu

    如果要应用 formatstring ,必须将该列 Behavior-> HtmlEncode 设置为False,这个问题也困惑了我很久 :)  回复  更多评论   

    # re: GridView控件使用经验 2005-12-23 17:10 周奔驰

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow row = GridView1.SelectedRow; int id =Convert.ToInt32(GridView1.SelectedDataKey.Value); }  回复  更多评论   

    # re: GridView控件使用经验 2005-12-27 12:36 wu ming

    alex readed!  回复  更多评论   

    # re: GridView控件使用经验之时间设置 2006-01-05 13:47 hr

    前面的同志说的时间格式的问题,我好像解决了。 加入以下代码就好。DataFormatString="{0:yyyy-MM-dd}  回复  更多评论   

    # re: GridView控件使用经验 2006-01-29 16:21 防伪底纹设计 防伪标签 封口标签

    多谢了。困惑了 一个下午/  回复  更多评论   

    # re: GridView控件使用经验 2006-03-09 09:08 Tony_Zhu

    把GridView绑定到DataSet,点击了Edit后出现更新和取消,却发现修改完数据后,更新按钮不响应,不知为什么?郁闷.。。。,请赐教! Email:swaterpig@gmail.com;Thanks!  回复  更多评论   

    # re: GridView控件使用经验 2006-03-09 16:14 尖锐湿疣

    Behavior-> HtmlEncode 我自己手动设置datasource 然后怎么设置HTMLENCODE?   回复  更多评论   

    # re: GridView控件使用经验 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; } }  回复  更多评论   

    # re: GridView控件使用经验 2006-03-10 22:18 自由、创新、研究、探索……

    http://www.thecodeproject.com/aspnet/GridViewRedux.asp http://www.thecodeproject.com/aspnet/addupdate.asp http://www.thecodeproject.com/aspnet/GridViewClientPostBack.asp http://www.thecodeproject.com/aspnet/GridViewConfirmDelete.asp  回复  更多评论   

    # re: GridView控件使用经验 2006-03-10 22:20 自由、创新、研究、探索……

    http://www.thecodeproject.com/aspnet/GridViewRadiobuttons.asp http://www.thecodeproject.com/aspnet/MasterDetail.asp http://www.thecodeproject.com/aspnet/CustomBusinessObjects.asp  回复  更多评论   

    # re: GridView控件使用经验 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) { } }  回复  更多评论   

    # re: GridView控件使用经验 2006-03-21 20:23 自由、创新、研究、探索……

    http://weblogs.asp.net/despos/archive/2005/06/30/416783.aspx  回复  更多评论   

    # re: GridView控件使用经验 2006-04-08 14:24 感到郁闷

    各位好。 我也在使用GRIDVIEW。 查了MSND也找了不少文章。 始终没办法找到其类似DATAGRID中 VirtualItemCount[获取或设置在使用自定义分页时 DataGrid 控件中的实际项数。] 各位的分页功能是怎么写的呢? 难道都读出整个表的数据么?  回复  更多评论   

    # re: GridView控件使用经验 2006-04-14 16:06 梁广永

    学习,  回复  更多评论   

    # re: GridView控件使用经验 2006-07-06 01:17 kiler

    @感到郁闷 分页读取数据,用第3方的分页控件显示分页条,DATAGRID只显示当页数据.  回复  更多评论   

    # re: GridView控件使用经验 2006-07-26 16:22 ssj

    DataFormatString的问题也是困扰了我很久,最终只好写了一个方法来格式化数据:public static void FormatNumericData (GridView grid, params string[] fieldNames) 对于负数,显示为红色 感觉GridView针对使用DataSourceID做了很多附加处理,对于使用DataSource,简直就是鸡肋  回复  更多评论   

    # re: GridView控件使用经验 2006-08-05 16:57 RedVesper

    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()事件中根本就取不到,而且报的错误信息. 指点   回复  更多评论   

    # re: GridView控件使用经验 2006-08-06 00:01 自由、创新、研究、探索……

    http://209.171.52.99/useritems/TariqMultiHeaderGridview.asp http://209.171.52.99/useritems/PagingWithODS.asp  回复  更多评论   

    # re: GridView控件使用经验 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); } }  回复  更多评论   

    # re: GridView控件使用经验 2006-08-30 14:21 自由、创新、研究、探索……

    ASP.NET 2.0: Playing a bit with GridView "Sort Grouping" http://aspadvice.com/blogs/joteke/archive/2006/02/11/15130.aspx  回复  更多评论   

    # re: GridView控件使用经验 2006-09-03 15:35 zhang[匿名]

    如果没有设置模版列,我们将如何取得值 GridViewRow grdRow = (GridViewRow)btnRefuse.Parent.Parent; string strId = grdRow.Cells[0].Text; string memberId = grdRow.Cells[7].Text; 好像不能用,那该什么办。 指点   回复  更多评论   

    # re: GridView控件使用经验 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(); } }   回复  更多评论   

    # re: GridView控件使用经验 2006-09-11 18:51 zhang[匿名]

    自由、创新、研究、探索…… 谢谢啦!!!  回复  更多评论   

    # re: GridView控件使用经验 2006-09-16 10:51 kakake

    要在控件的编辑方法中再次绑定数据就好了  回复  更多评论   

    # re: GridView控件使用经验 2006-10-09 11:49 花芽QQ 78793450

    提个问题: 绑定了数据源的GridView1 想在某个字段内获取同个页面的一个session值。。 通过什么方法可以获得啊、还有想在模板行添加个 textbox 无法显示 <iframe src=user_index.aspx width="100%" height="100%" scrolling="no" frameborder="0"></iframe> 这种值。。   回复  更多评论   

    # re: GridView控件使用经验 2006-10-18 09:44 自由、创新、研究、探索……

    Formatting AutoGenerateColumns in an ASP.NET Grid http://www.codeproject.com/aspnet/FrmtAutoGenClmnASPNETGrid.asp   回复  更多评论   

    # re: GridView控件使用经验 2006-11-04 00:27 young[匿名]

    数据源如果直接设置在Gridview里,那就破坏了三层开发的结构,但是如果不绑定在里面,又没有办法设置列,这该怎么解决啊?  回复  更多评论   

    # re: GridView控件使用经验 2006-11-04 09:04 自由、创新、研究、探索……

    @young[匿名] objectdatasource控件,在.NET 2.0中,有了该控件,可以很方便地沟通表示层和逻辑层,而不会坏了三层开发的结构 李万宝的blog上有好几个ObjectDataSource控件的介绍http://www.cnblogs.com/mqingqing123/category/32262.html?Show=All  回复  更多评论   

    # re: GridView控件使用经验 2007-01-09 08:31 火星星

    在第一条中如果直接写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; }  回复  更多评论   

    # re: GridView控件使用经验 2007-01-11 10:25 violet[匿名]

    请教GridView中GridLines的颜色怎么设置呀??  回复  更多评论   

    #  re: GridView控件使用经验 2005-12-22 23:42 kasafuma

    最新回复(0)