使用空数据源显示Gridview的页眉/页脚?

时间:2010-04-26 08:05:09

标签: .net asp.net vb.net gridview

如何使用空数据源显示Asp.Net Gridview的页眉/页脚?

3 个答案:

答案 0 :(得分:2)

阅读this article

或者,您可以使用EmptyDataTemplate property为空数据行定义自己的自定义用户界面。

答案 1 :(得分:1)

您也可以通过为网格的DataSource分配一个虚拟行来执行此操作。 默认情况下,数据源将填充虚拟行。因此网格将显示页脚和&假行也是。 要隐藏行,您可以添加一个虚拟字段以显示是否显示控件。并绑定该字段以控制Visible属性。 最后,当您将实际行添加到数据源时...删除您创建的虚拟行。

示例 - 来源:

    <asp:GridView ID="grdQualify" runat="server" Width="100%" AutoGenerateColumns="False" ShowFooter="true"
                        EnableModelValidation="True">
                        <Columns>
                            <asp:TemplateField HeaderText="Qualifying Exam">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtQualifyingExam" runat="server" Text='<%# Eval("QualifyingExam") %>' Visible='<%# Eval("ShowVisible") %>'></asp:TextBox>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtQualifyingExamF" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Roll Number">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtRollNumber" runat="server" Text='<%# Eval("RollNumber") %>' Visible='<%# Eval("ShowVisible") %>'></asp:TextBox>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtRollNumberF" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Admission Round">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtAdmissionRound" runat="server" Text='<%# Eval("AdmissionRound") %>' Visible='<%# Eval("ShowVisible") %>'></asp:TextBox>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtAdmissionRoundF" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Marks Obtained">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtMarksObtained" runat="server" Text='<%# Eval("MarksObtained") %>' Visible='<%# Eval("ShowVisible") %>'></asp:TextBox>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtMarksObtainedF" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Marks Out of">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtMarksOutOf" runat="server" Text='<%# Eval("MarksOutOf") %>' Visible='<%# Eval("ShowVisible") %>'></asp:TextBox>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtMarksOutOfF" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Percentage">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtPercentage" runat="server" Text='<%# Eval("Percentage") %>' Visible='<%# Eval("ShowVisible") %>'></asp:TextBox>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtPercentageF" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Remarks">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtRemarks" runat="server" Text='<%# Eval("Remarks") %>' Visible='<%# Eval("ShowVisible") %>'></asp:TextBox>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtRemarksF" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:Button ID="btnDeleteQualify" runat="server" Text="Delete" 
                                        Visible='<%# Eval("ShowVisible") %>' onclick="btnDeleteQualify_Click"></asp:Button>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:Button ID="btnAddQualify" runat="server" Text="Add" 
                                        onclick="btnAddQualify_Click"></asp:Button>
                                </FooterTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

示例 - 代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (ViewState["QualifyDetails"] == null)
                ViewState["QualifyDetails"] = new List<Entity_StudentAcademicQualify>();
            GridDataBindQualify((List<Entity_StudentAcademicQualify>)ViewState["QualifyDetails"]);
        }
    }


    private void GridDataBindQualify(List<Entity_StudentAcademicQualify> list)
    {
        if (list.Count == 0)
        {
            Entity_StudentAcademicQualify studentQualify = new Entity_StudentAcademicQualify();

            studentQualify.ShowVisible = false;
            studentQualify.QualifyingExam = string.Empty;
            studentQualify.RollNumber = string.Empty;
            studentQualify.AdmissionRound = string.Empty;
            studentQualify.MarksObtained = 0;
            studentQualify.MarksOutOf = 0;
            studentQualify.Percentage = 0;
            studentQualify.Remarks = string.Empty;

            list.Add(studentQualify);
        }
        else
        {
            list.RemoveAt(0);
        }

        grdQualify.DataSource = list;
        grdQualify.DataBind();

        ViewState["QualifyDetails"] = list;
    }

    protected void btnAddQualify_Click(object sender, EventArgs e)
    {
        GridViewRow row = (GridViewRow)((Button)sender).Parent.Parent;

        List<Entity_StudentAcademicQualify> studentQualifys =
            (List<Entity_StudentAcademicQualify>)ViewState["QualifyDetails"];

        Entity_StudentAcademicQualify studentQualify = new Entity_StudentAcademicQualify();

        studentQualify.ShowVisible = true;
        studentQualify.QualifyingExam = ((TextBox)row.FindControl("txtQualifyingExamF")).Text;
        studentQualify.RollNumber = ((TextBox)row.FindControl("txtRollNumberF")).Text;
        studentQualify.AdmissionRound = ((TextBox)row.FindControl("txtAdmissionRoundF")).Text;
        studentQualify.MarksObtained = Convert.ToInt32(((TextBox)row.FindControl("txtMarksObtainedF")).Text);
        studentQualify.MarksOutOf = Convert.ToInt32(((TextBox)row.FindControl("txtMarksOutOfF")).Text);
        studentQualify.Percentage = Convert.ToInt32(((TextBox)row.FindControl("txtPercentageF")).Text);
        studentQualify.Remarks = ((TextBox)row.FindControl("txtRemarksF")).Text;

        studentQualifys.Add(studentQualify);

        GridDataBindQualify(studentQualifys);
    }

    protected void btnDeleteQualify_Click(object sender, EventArgs e)
    {
        List<Entity_StudentAcademicQualify> studentQualifys =
          (List<Entity_StudentAcademicQualify>)ViewState["QualifyDetails"];

        studentQualifys.RemoveAt(((GridViewRow)((Button)sender).Parent.Parent).RowIndex);

        GridDataBindQualify(studentQualifys);
    }

答案 2 :(得分:1)

我在网上阅读了很多关于此的帖子,最后我提出了我自己的解决方案,满足了我的需求,并希望其他人的需求,包括这个问题。

显示页眉/页脚的需要可能是因为 1)我希望用户看到网格的结构,和 2)我想使用页脚向表中添加新数据。

因此,只需显示“没有数据可用”&#39;是不够的。

所以我的解决方案是测试数据是否可用。如果没有,则创建数据源的结构并使用默认值填充一行的必填字段。这很重要,因为如果在布尔和日期等字段为空时将数据源绑定到网格,那么您将拥有&#39; DBNull&#39;错误。然后将此新数据源绑定到网格。然后按此顺序隐藏行。

Protected Sub BindGrid()
    con = New SqlConnection("mainLocal")
    Dim da As New SqlDataAdapter("SELECT * FROM Test", con)
    Dim dt As New DataTable()
    da.Fill(dt)

    dim rows as integer = dt.Rows.Count
    If rows = 0 Then
        Dim dr As DataRow = Nothing

        dr = dt.NewRow()
        dr("isActive") = False
        dr("Dated") = Date.Now()
        dt.Rows.Add(dr)
    End If

    Me.TestGridView.DataSource = dt
    Me.TestGridView.DataBind()
    If rows = 0 Then Me.TestGridView.Rows(0).Visible = False
End Sub

我想更简单,更短,更整洁!