GridView 利用 <asp:BoundField 计算总合;并在页面不展现

该博客介绍了如何在ASP.NET的GridView中使用<asp:BoundField>计算`NormalOverTotalTime`、`HighOverTotalTime`、`NormalOverDegree`和`HighOverDegree`的总和,并在页面上不展示这些字段。通过`RowDataBound`事件处理行绑定时的计算,然后在`RowCreated`事件中隐藏指定列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

---------------------------------------------------- GridView 利用 <asp:BoundField 计算总合;并在页面不展现----------------------------------------------------
<asp:GridView ID="ReportView" runat="server" AutoGenerateColumns="False" GridLines="None" OnRowDataBound="ReportView_RowDataBound" ShowFooter="True" OnRowCreated="ReportView_RowCreated">
      <Columns>
      <asp:BoundField DataField="SN" HeaderText="sssssssssss"/>
      <asp:BoundField DataField="RegName" HeaderText="sssssssss" />
      <asp:TemplateField HeaderText="sssssss">
        <ItemTemplate><%# Convert.ToDateTime(Eval("BeginTime")).ToString("yyyy-MM-dd HH:mm")%></ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="eeee">
        <ItemTemplate><%# Convert.ToDateTime(Eval("EndTime")).ToString("yyyy-MM-dd HH:mm")%></ItemTemplate>
      </asp:TemplateField>
      <asp:BoundField DataField="NormalOverTotalTime" HeaderText="aaaaaa" />
     <%-- <asp:TemplateField HeaderText="ccccc">
        <ItemTemplate><%#Eval("NormalOverTotalTime").ToString() == "" ? "0" : Eval("NormalOverTotalTime").ToString()%>分钟</ItemTemplate>
  &nb

<think>嗯,用户想了解如何配置ASP.NET的GridView控件以及绑定数据源,特别是关于BoundField、CheckBoxField和SqlDataSource的使用示例。首先,我需要回忆一下ASP.NET中GridView的基本用法和相关控件的数据绑定方法。 根据引用[1],用户提到使用SqlConnection和SqlCommand来绑定数据到GridView,这可能涉及到后端代码的数据绑定方式。而引用[2]和引用[3]提到了GridView的事件和字段类型,比如BoundField和DataTextField。引用[4]中的DetailsView和SqlDataSource的示例可能对展示SqlDataSource的配置有帮助。 首先,应该分步骤说明如何配置GridView,包括定义列和绑定数据源。可能需要分为使用SqlDataSource控件和手动绑定数据两种情况。对于SqlDataSource,需要展示在.aspx页面中的声明式配置,包括连接字符串和查询语句。然后,在GridView中设置DataSourceID属性来关联数据源。 接下来,BoundField用于显示普通文本字段,而CheckBoxField用于布尔类型的数据。需要给出每个字段类型的示例代码,比如如何设置DataField和HeaderText。此外,CheckBoxField可能需要处理只读模式的问题,或者是否需要绑定到特定的数据类型。 另外,用户可能想知道如何在后端代码中手动绑定数据,比如使用SqlDataReader或DataSet。引用[1]中的代码示例展示了使用ExecuteReader()方法绑定到GridView,这可以作为手动绑定的例子。需要说明在Page_Load事件中调用数据绑定方法,注意连接的开闭。 还需要考虑分页、排序等功能的启用,这可能涉及到AllowPaging和AllowSorting属性,以及处理对应的事件。过用户的问题可能更关注基础配置,所以这部分可能作为高级应用简要提及。 最后,生成相关问题时要围绕GridView的配置、数据绑定方法、字段类型的使用以及性能优化等方面提问,帮助用户进一步学习。</think>### ASP.NET GridView控件配置与数据绑定步骤 #### 一、基础配置(声明式数据绑定) 1. **配置SqlDataSource** ```xml <asp:SqlDataSource ID="srcCustomers" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT CustomerID, CompanyName, IsActive FROM Customers" runat="server"/> ``` 2. **GridView字段定义** ```xml <asp:GridView ID="GridView1" DataSourceID="srcCustomers" AutoGenerateColumns="false" runat="server"> <Columns> <asp:BoundField DataField="CustomerID" HeaderText="客户编号" /> <asp:BoundField DataField="CompanyName" HeaderText="公司名称" /> <asp:CheckBoxField DataField="IsActive" HeaderText="激活状态" /> </Columns> </asp:GridView> ``` #### 二、代码绑定数据(后端方式) ```csharp protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string cs = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con); con.Open(); GridView1.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection); GridView1.DataBind(); } } } ``` #### 三、高级配置项 1. **分页与排序** ```xml <asp:GridView AllowPaging="true" PageSize="10" AllowSorting="true" OnSorting="GridView1_Sorting" OnPageIndexChanging="GridView1_PageIndexChanging" /> ``` 2. **模板字段** ```xml <asp:TemplateField HeaderText="操作"> <ItemTemplate> <asp:Button ID="btnEdit" Text='<%# Eval("CustomerID") %>' CommandArgument='<%# Eval("CustomerID") %>' runat="server" /> </ItemTemplate> </asp:TemplateField> ``` #### 四、数据绑定要点 - BoundField支持格式设置:`DataFormatString="{0:yyyy-MM-dd}"` - CheckBoxField自动识别布尔类型字段[^3] - 使用`DataKeyNames`属性保存主键值 - 数据绑定后触发`DataBound`事件[^2]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值