简体   繁体   中英

Filling an Gridview EmptyDataTemplate with data ASP.NET C#

I have a gridview where the data it shows depends on the textbox and button control. Since the gridview doesnt show anything (unless a user typed an input in the textbox) how can I fill it with all data from the tables? I'm thinking of inserting another gridview inside of the EmptyDataTemplate but is there any way I could show all the records even without user input?

Just started in ASP.NET so I really need your help guys.

Thanks in advance;)

Here's my code sample:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
        DataKeyNames="lenid" DataSourceID="returningDataSource" ForeColor="#333333" 
        GridLines="None">
        <RowStyle BackColor="#EFF3FB" />
        <Columns>
            <asp:CommandField HeaderStyle-Width="120px" ButtonType="Button" ShowEditButton="True" ShowDeleteButton="True" />
            <asp:BoundField DataField="bookid" HeaderText="Book ID/ISBN" 
                SortExpression="bookid" />
            <asp:BoundField DataField="booktitle" HeaderText="Title" 
                SortExpression="booktitle" />
            <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" 
                SortExpression="EmployeeID" />
            <asp:BoundField DataField="department" HeaderText="Department" 
                SortExpression="department" />
            <asp:BoundField DataField="dateborrowed" HeaderText="Date borrowed" 
                SortExpression="dateborrowed" />
            <asp:BoundField DataField="datereturned" HeaderText="Date returned" 
                SortExpression="datereturned" NullDisplayText="-- not yet returned --" />
        </Columns>
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <EmptyDataTemplate>
            <asp:GridView ID="GridView2" runat="server" AllowPaging="True" 
                AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
                DataKeyNames="lenid" DataSourceID="returningDataSource" ForeColor="#333333" 
                GridLines="None">
                <RowStyle BackColor="#EFF3FB" />
                <Columns>
                    <asp:CommandField HeaderStyle-Width="120" ButtonType="Button" ShowDeleteButton="True" ShowEditButton="True" />
                    <asp:BoundField DataField="bookid" HeaderText="Book ID/ISBN" 
                        SortExpression="bookid" />
                    <asp:BoundField DataField="booktitle" HeaderText="Title" 
                        SortExpression="booktitle" />
                    <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" 
                        SortExpression="EmployeeID" />
                    <asp:BoundField DataField="department" HeaderText="Department" 
                        SortExpression="department" />
                    <asp:BoundField DataField="dateborrowed" HeaderText="Date borrowed" 
                        SortExpression="dateborrowed" />
                    <asp:BoundField DataField="datereturned" HeaderText="Date returned" 
                        SortExpression="datereturned" NullDisplayText="-- not yet returned --" />
                </Columns>
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
            <asp:SqlDataSource ID="returningDataSource" runat="server" 
                ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
                DeleteCommand="DELETE FROM [LendTable] WHERE [lenid] = @lenid" 
                InsertCommand="INSERT INTO [LendTable] ([bookid], [EmployeeID], [department], [dateborrowed], [datereturned]) VALUES (@bookid, @EmployeeID, @department, @dateborrowed, @datereturned)" 
                SelectCommand="SELECT dbo.LendTable.lenid, dbo.LendTable.bookid, dbo.LendTable.EmployeeID, dbo.LendTable.department, dbo.LendTable.dateborrowed, dbo.LendTable.datereturned, dbo.TblBooks.booktitle FROM dbo.LendTable INNER JOIN dbo.TblBooks ON dbo.LendTable.bookid = dbo.TblBooks.bookid" 

                UpdateCommand="UPDATE [LendTable] SET [bookid] = @bookid, [EmployeeID] = @EmployeeID, [department] = @department, [dateborrowed] = @dateborrowed, [datereturned] = @datereturned WHERE [lenid] = @lenid">
                <DeleteParameters>
                    <asp:Parameter Name="lenid" Type="Int32" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="bookid" Type="Int64" />
                    <asp:Parameter Name="EmployeeID" Type="String" />
                    <asp:Parameter Name="department" Type="String" />
                    <asp:Parameter Name="dateborrowed" Type="DateTime" />
                    <asp:Parameter Name="datereturned" Type="DateTime" />
                    <asp:Parameter Name="lenid" Type="Int32" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="bookid" Type="Int64" />
                    <asp:Parameter Name="EmployeeID" Type="String" />
                    <asp:Parameter Name="department" Type="String" />
                    <asp:Parameter Name="dateborrowed" Type="DateTime" />
                    <asp:Parameter Name="datereturned" Type="DateTime" />
                </InsertParameters>
            </asp:SqlDataSource>
        </EmptyDataTemplate>
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>

I want to display all the records from a table if a user doesn't type any input (or fill the EmptyDataTemplate with records from the database).

Thanks again!

Execute the Select command of the returning data source as its gonna return the entire rows od the table and bind to the original grid in the (.IsPostBack) of the page_load and next time when you need to bind data based on text box input execute the query and bind the data to same grid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM