GridView怎么在asp.net中使用
这期内容当中小编将会给大家带来有关GridView怎么在asp.net中使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
前台代码:
<asp:GridViewID="tabgv"runat="server"DataKeyNames="ysId"ShowFooter="True"OnRowDataBound="GridView1_RowDataBound" OnRowCreated="GridView1_RowCreated"AutoGenerateColumns="False"Width="99%"CssClass="gridview" EnableModelValidation="True"OnRowCancelingEdit="tabgv_RowCancelingEdit"OnRowEditing="tabgv_RowEditing" OnRowUpdating="tabgv_RowUpdating"> <RowStyleBorderColor="Black"BorderStyle="None"/> <Columns> <asp:BoundFieldDataField="gsHkrno"HeaderText="项目编号"ReadOnly="True"> <ItemStyleWidth="5%"HorizontalAlign="Center"/> <FooterStyleBorderWidth="0px"/> </asp:BoundField> <asp:TemplateFieldHeaderText="项目名称"> <ItemTemplate> <asp:HyperLinkID="Hyp_gsName"runat="server"ForeColor="Blue"Text='<%#Bind("gsName")%>'></asp:HyperLink> </ItemTemplate> <ItemStyleWidth="25%"HorizontalAlign="left"/> <FooterTemplate> <asp:LabelID="Label4"runat="server"Font-Bold="True"Text="合计:"Width="61px"></asp:Label> </FooterTemplate> <FooterStyleBorderColor="Black"BorderWidth="0px"HorizontalAlign="Right"/> </asp:TemplateField> <asp:TemplateFieldHeaderText="概算金额"> <ItemTemplate> <asp:LabelID="labgsMoney"runat="server"Text='<%#Bind("gsMoney","{0:N2}")%>'></asp:Label> </ItemTemplate> <ItemStyleWidth="8%"HorizontalAlign="right"/> <FooterTemplate> <asp:LabelID="labtotalgsMoney"runat="server"ForeColor="Red"></asp:Label> </FooterTemplate> <FooterStyleBorderColor="Black"HorizontalAlign="Right"/> </asp:TemplateField> <asp:TemplateFieldHeaderText="开工至上年底\n累计完成概算"> <ItemTemplate> <asp:LabelID="labkgzsndwcGsMoney"runat="server"Text='<%#Bind("footMoney","{0:N2}")%>'></asp:Label> <asp:HiddenFieldID="hdnysYear"runat="server"Value='<%#Eval("ysYear")%>'/> <asp:HiddenFieldID="hdngsId"runat="server"Value='<%#Eval("gsId")%>'/> <asp:HiddenFieldID="hdngsHkrno"runat="server"Value='<%#Eval("gsHkrno")%>'/> <asp:HiddenFieldID="hdnChildFlag"runat="server"Value='<%#Eval("childFlag")%>'/> </ItemTemplate> <ItemStyleWidth="8%"HorizontalAlign="right"/> <FooterTemplate> <asp:LabelID="labtotalfootMoney"runat="server"ForeColor="Red"></asp:Label> </FooterTemplate> <FooterStyleBorderColor="Black"HorizontalAlign="Right"/> </asp:TemplateField> <asp:TemplateFieldHeaderText="投资计划金额"> <ItemTemplate> <asp:LabelID="labtzqysMoney"runat="server"Text='<%#Bind("tzqysMoney","{0:N2}")%>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBoxID="txttzqysMoney"runat="server"Text='<%#Eval("tzqysMoney")%>' Style="text-align:right"Width="100px" onKeyPress="if(event.keyCode!=46&&event.keyCode!=45&&(event.keyCode<48||event.keyCode>57))event.returnValue=false"></asp:TextBox> </EditItemTemplate> <ItemStyleWidth="8%"HorizontalAlign="right"/> <FooterTemplate> <asp:LabelID="labtotaltzqysMoney"runat="server"ForeColor="Red"></asp:Label> </FooterTemplate> <FooterStyleBorderColor="Black"HorizontalAlign="Right"/> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:LabelID="labtzhysMoney"runat="server"Text='<%#Bind("tzhysMoney","{0:N2}")%>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBoxID="txttzhysMoney"runat="server"Text='<%#Eval("tzhysMoney")%>' Style="text-align:right"Width="100px" onKeyPress="if(event.keyCode!=46&&event.keyCode!=45&&(event.keyCode<48||event.keyCode>57))event.returnValue=false"></asp:TextBox> </EditItemTemplate> <ItemStyleWidth="8%"HorizontalAlign="right"/> <FooterTemplate> <asp:LabelID="labtotaltzhysMoney"runat="server"ForeColor="Red"></asp:Label> </FooterTemplate> <FooterStyleBorderColor="Black"HorizontalAlign="Right"/> </asp:TemplateField> <asp:TemplateFieldHeaderText="备注"> <ItemTemplate> <asp:LabelID="labtremark"runat="server"Text='<%#Eval("remark")%>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBoxID="txtremark"runat="server"Text='<%#Eval("remark")%>'Width="100px"></asp:TextBox> </EditItemTemplate> <ItemStyleWidth="10%"HorizontalAlign="Center"/> </asp:TemplateField> <asp:TemplateFieldShowHeader="False"> <EditItemTemplate> <asp:LinkButtonID="LkBtnUpdate"runat="server"CausesValidation="True"CommandName="Update" Text="更新"></asp:LinkButton> <asp:LinkButtonID="LkBtnCtrl"runat="server"CausesValidation="False"CommandName="Cancel" Text="取消"></asp:LinkButton> </EditItemTemplate> <ItemTemplate> <asp:LinkButtonID="LinkButton2"runat="server"CausesValidation="False"CommandName='<%#Eval("childFlag").ToString()=="0"?"":"Edit"%>' Text='<%#Eval("childFlag").ToString()=="0"?"锁定":"编辑"%>'></asp:LinkButton> </ItemTemplate> <ItemStyleWidth="8%"HorizontalAlign="Center"/> </asp:TemplateField> </Columns> </asp:GridView>
注意:
1、通过设置 ReadOnly="True" 表示此列不可以编辑;2、通过Text='<%# Bind("tzhysMoney", "{0:N2}") %>'进行金额格式化;3、重点的编辑\显示\合计
<asp:TemplateField> <ItemTemplate> <asp:LabelID="labtzhysMoney"runat="server"Text='<%#Bind("tzhysMoney","{0:N2}")%>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBoxID="txttzhysMoney"runat="server"Text='<%#Eval("tzhysMoney")%>' Style="text-align:right"Width="100px" ></asp:TextBox> </EditItemTemplate> <ItemStyleWidth="8%"HorizontalAlign="right"/> <FooterTemplate> <asp:LabelID="labtotaltzhysMoney"runat="server"ForeColor="Red"></asp:Label> </FooterTemplate> <FooterStyleBorderColor="Black"HorizontalAlign="Right"/> </asp:TemplateField>
后台代码:
行绑定时的操作:
protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse) { GridViewRowgr=e.Row; if(gr.RowType==DataControlRowType.DataRow) { HiddenFieldhdnysYear=(HiddenField)e.Row.FindControl("hdnysYear"); HiddenFieldhdngsHkrno=(HiddenField)e.Row.FindControl("hdngsHkrno"); HiddenFieldhdnChildFlag=(HiddenField)e.Row.FindControl("hdnChildFlag"); HyperLinkhy=(HyperLink)e.Row.FindControl("Hyp_gsName"); if(hdnChildFlag.Value!="1") { //设置连接地址\参数 hy.NavigateUrl="ys_edit_right.aspx?gsHkrno="+hdngsHkrno.Value+"&sysObjNo="+sysObjNo+"&ysYear="+hdnysYear.Value; hy.Font.Underline=true; hy.ForeColor=System.Drawing.Color.Blue; hy.Target="mainFrame"; } else { hy.ForeColor=System.Drawing.Color.Black; } //计算合计 LabellabgsMoney=(Label)(e.Row.FindControl("labgsMoney")); if(labgsMoney!=null||labgsMoney.Text!="") { gsMoneytotal+=Convert.ToDecimal(labgsMoney.Text); } LabellabkgzsndwcGsMoney=(Label)(e.Row.FindControl("labkgzsndwcGsMoney")); if(labkgzsndwcGsMoney!=null||labkgzsndwcGsMoney.Text!="") { footMoneytotal+=Convert.ToDecimal(labkgzsndwcGsMoney.Text); } //编辑状态时,不然点击编辑时会出错,找不到控件 if((e.Row.RowState&DataControlRowState.Edit)==DataControlRowState.Edit) { TextBoxtxttzqysMoney=(TextBox)(e.Row.FindControl("txttzqysMoney")); if(txttzqysMoney!=null||txttzqysMoney.Text!="") { tzqysMoneytotal+=Convert.ToDecimal(txttzqysMoney.Text); } TextBoxtxttzhysMoney=(TextBox)(e.Row.FindControl("txttzhysMoney")); if(txttzhysMoney!=null||txttzhysMoney.Text!="") { tzhysMoneytotal+=Convert.ToDecimal(txttzhysMoney.Text); } } else//非编辑状态时 { LabellabtzqysMoney=(Label)(e.Row.FindControl("labtzqysMoney")); if(labtzqysMoney!=null||labtzqysMoney.Text!="") { tzqysMoneytotal+=Convert.ToDecimal(labtzqysMoney.Text); } LabellabtzhysMoney=(Label)(e.Row.FindControl("labtzhysMoney")); if(labtzhysMoney!=null||labtzhysMoney.Text!="") { tzhysMoneytotal+=Convert.ToDecimal(labtzhysMoney.Text); } } } } //取消编辑 protectedvoidtabgv_RowCancelingEdit(objectsender,GridViewCancelEditEventArgse) { tabgv.EditIndex=-1; stringstrysYear=(Convert.ToInt32(ysYear)-1).ToString(); GvDataBind(gsHkrno,"0002",ysYear,"1900-01-0100:00:00",strysYear+"-12-2500:00:00"); } //编辑 protectedvoidtabgv_RowEditing(objectsender,GridViewEditEventArgse) { tabgv.EditIndex=e.NewEditIndex; stringstrysYear=(Convert.ToInt32(ysYear)-1).ToString(); GvDataBind(gsHkrno,"0002",ysYear,"1900-01-0100:00:00",strysYear+"-12-2500:00:00"); } //更新 protectedvoidtabgv_RowUpdating(objectsender,GridViewUpdateEventArgse) { stringsqlstr="updateys_ysitem_listsettzqysMoney=@tzqysMoney,tzhysMoney=@tzhysMoney,remark=@remarkwhereysId=@ysId"; SqlParameter[]para={newSqlParameter("@tzqysMoney",SqlDbType.VarChar), newSqlParameter("@tzhysMoney",SqlDbType.VarChar), newSqlParameter("@remark",SqlDbType.VarChar), newSqlParameter("@ysId",SqlDbType.VarChar) }; para[0].Value=((TextBox)(tabgv.Rows[e.RowIndex].FindControl("txttzqysMoney"))).Text.ToString().Trim(); para[1].Value=((TextBox)(tabgv.Rows[e.RowIndex].FindControl("txttzhysMoney"))).Text.ToString().Trim(); para[2].Value=((TextBox)(tabgv.Rows[e.RowIndex].FindControl("txtremark"))).Text.ToString().Trim(); para[3].Value=tabgv.DataKeys[e.RowIndex].Value.ToString(); pms.SqlHelper.ExecuteNonQuery(CommandType.Text,sqlstr,para); tabgv.EditIndex=-1; stringstrysYear=(Convert.ToInt32(ysYear)-1).ToString(); GvDataBind(gsHkrno,"0002",ysYear,"1900-01-0100:00:00",strysYear+"-12-2500:00:00"); }
上述就是小编为大家分享的GridView怎么在asp.net中使用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注恰卡编程网行业资讯频道。
推荐阅读
-
ASP.NET怎么实现登出系统并清除Cookie
-
CentOS下怎么实现Mono和Asp.net
CentOS下怎么实现Mono和Asp.net这篇文章主要介绍“C...
-
ASP.NET的Core AD域登录过程怎么实现
ASP.NET的Core AD域登录过程怎么实现本文小编为大家详细...
-
ASP.NET MVC中两个配置文件的作用是什么
-
ASP.NET MVC过滤器执行顺序是什么
-
ASP.NET Core Zero模块系统的示例分析
ASP.NET Core Zero模块系统的示例分析这篇文章主要为...
-
asp.net多级分类统计列表怎么实现
asp.net多级分类统计列表怎么实现本篇内容介绍了“asp.ne...
-
C#怎么用ASP.NET Core开发学生管理系统
-
ASP.net中aspx与cs函数怎么实现互调
ASP.net中aspx与cs函数怎么实现互调,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇...
-
ASP.NET中怎么优化站点构建托管资源
这篇文章将为大家详细讲解有关ASP.NET中怎么优化站点构建托管资源,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完...