用数据填充gridviewemptydatatemplate.净c#
本文关键字:gridviewemptydatatemplate 填充 数据 | 更新日期: 2023-09-27 17:54:41
我有一个gridview,它显示的数据取决于文本框和按钮控件。由于gridview不显示任何东西(除非用户在文本框中键入输入),我怎么能填满它与所有的数据表?我正在考虑在EmptyDataTemplate中插入另一个gridview,但是否有任何方法可以显示所有记录,即使没有用户输入?
刚刚开始使用ASP。所以我真的需要你们的帮助。
提前感谢;)
下面是我的代码示例:<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>
我想显示所有记录从一个表中,如果用户不输入任何输入(或填满记录从数据库的EmptyDataTemplate)。
再次感谢!
执行返回数据源的Select命令,因为它将返回表的整个行并绑定到page_load的(!IsPostBack)中的原始网格,下次当您需要根据文本框输入绑定数据时,执行查询并将数据绑定到相同的网格