2

基本上,我需要帮助提高充满网格视图的页面的性能,以减少 C# 的数量并将尽可能多的 GridView 设置移动到 ASP 代码中(而不是数据本身,它在运行时绑定并且需要保留在 C# 中)。

我对 GridView 控件比较陌生,需要帮助将设置移动到 GridView 中,因为此代码所在的页面将有大约 8 或 9 个表。

这是我的 ASP 代码:

<asp:GridView runat="server" ID="tblBasicProcessingTime"
    Caption="Basic Processing Stats" ShowHeader="False">
</asp:GridView>

还有我的 C# 代码:

var longestTime = ReportData.OrderByDescending(x => x.TimeSpentProcessing).FirstOrDefault();
var averageTime = ReportData.Average(x => x.TimeSpentProcessing);
var shortestTime = ReportData.OrderBy(x => x.TimeSpentProcessing).FirstOrDefault();

var table = new DataTable();

const string col1Name = "Header";
const string col2Name = "Data";

table.Columns.Add(col1Name);
table.Columns.Add(col2Name);

var row1 = table.NewRow();
row1[col1Name] = "Longest Processing Time";
row1[col2Name] = longestTime.TimeSpentProcessing;
table.Rows.Add(row1);

var row2 = table.NewRow();
row2[col1Name] = "Average Processing Time";
row2[col2Name] = averageTime;
table.Rows.Add(row2);

var row3 = table.NewRow();
row3[col1Name] = "Shortest Processing Time";
row3[col2Name] = shortestTime.TimeSpentProcessing;
table.Rows.Add(row3);

tblBasicProcessingTime.DataSource = table;
tblBasicProcessingTime.DataBind();

提前致谢。

4

1 回答 1

1
<asp:GridView
            ID="gvList"
            runat="server"
            AutoGenerateColumns="False"
            EnableModelValidation="True"
            HeaderStyle-HorizontalAlign="Center"
            Width="100%"
            RowStyle-Height="25px"
            HeaderStyle-CssClass="ui-th-div-ie"
            RowStyle-CssClass="TR_ROW1"
            AlternatingRowStyle-CssClass="TR_ROW2"
            HeaderStyle-ForeColor="#0073ea"
            BorderWidth="1px"
            CellPadding="2"
            HorizontalAlign="Center"
            OnPageIndexChanging="gvList_PageIndexChanging"
            AllowPaging="true"
            AllowSorting="true"
            PagerStyle-HorizontalAlign="Right"
            PageSize="1"
            OnSorting="gvList_Sorting">

            <Columns>
                <asp:TemplateField HeaderStyle-Width="20%" HeaderText="<%$ Resources: CommonResources, lbl_InstoreHistLotNo.Text %>" SortExpression="LotNo">
                    <ItemTemplate>
                        <asp:Label ID="lbl_InstoreHistLotNo" runat="server" Text='<%#Eval("LotNo")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderStyle-Width="10%" HeaderText="<%$ Resources: CommonResources, lbl_InstoreHistPartNo.Text %>" SortExpression="PartCode">
                    <ItemTemplate>
                        <asp:Label ID="lbl_InstoreHistPartNo" runat="server" Text='<%#Eval("PartCode")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderStyle-Width="10%" HeaderText="<%$ Resources: CommonResources, lbl_InstoreHistPartName.Text %>" SortExpression="PartName">
                    <ItemTemplate>
                            <asp:Label ID="lbl_InstoreHistPartName" runat="server" Text='<%#Eval("PartName")%>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderStyle-Width="10%" HeaderText="<%$ Resources: CommonResources, lbl_InstoreHistInQuality.Text %>" SortExpression="Quality">
                    <ItemStyle HorizontalAlign="Right" />
                    <ItemTemplate>
                            <asp:Label ID="lbl_InstoreHistInQuality" runat="server" Text='<%#Eval("Quality")%>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderStyle-Width="10%" HeaderText="<%$ Resources: CommonResources, lbl_InstoreHistInUsercode.Text %>" SortExpression="UserCode">
                    <ItemTemplate>
                        <asp:Label ID="lbl_InstoreHistInUsercode" runat="server" Text='<%#Eval("UserCode")%>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderStyle-Width="15%" HeaderText="<%$ Resources: CommonResources, lbl_InstoreHistInTime.Text %>" SortExpression="InStoreDate">
                </asp:TemplateField>
            </Columns>
            <PagerStyle HorizontalAlign="Left"   />
            <PagerSettings Position="TopAndBottom" Mode="Numeric" />
        </asp:GridView>

这只是一个简单的 gridview 数据绑定演示。但是,我的标题文本是从资源文件加载的,您可以自己设置,如字符串“aaa”、“bbb”、“ccc”

在后端,您的代码可能如下所示:

DataTable dt = SqlHelper.ExcuteDataSet("select * from InStoreHistory");
gvList.DataSource = dt;
gvList.DataBind();
于 2012-08-07T09:18:43.833 回答