【发布时间】:2021-05-14 13:20:46
【问题描述】:
我正在查询数据库以填充 Datagrid。我不希望数据库中的所有内容都显示在表格上,只是一些重要信息。
我是 VB.net 的新手,我习惯于 asp.net 核心 MVC 和实体框架
数据源是无效类型。它必须是 IListSource、IEnumerable 或 IDataSource。 下面是我的代码
Private Sub GetTSANotification()
Dim query As String = "SELECT * FROM [dbo].[notifyMe]"
Dim DBConnection As String = System.Configuration.ConfigurationManager.ConnectionStrings("Notification").ConnectionString
Using cmd As New SqlConnection(DBConnection)
Using da As New SqlCommand(query)
Using sda As New SqlDataAdapter()
da.Connection = cmd
sda.SelectCommand = da
Using data As New DataTable()
sda.Fill(data)
notifyReport.DataSource = da
notifyReport.DataBind()
End Using
End Using
End Using
End Using
End Sub
这是我的数据网格。 我只想显示数据库中的 5 个项目,用户不需要数据库列中的其他项目
<asp:GridView CssClass="table table-bordered table-striped" ID="notifyReport" runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging" PageSize="10">
<Columns>
<asp:BoundField DataField="id" HeaderText="Id" SortExpression="id"></asp:BoundField>
<asp:BoundField DataField="customerName" HeaderText="Customer Name" SortExpression="customerName"></asp:BoundField>
<asp:BoundField DataField="customerEmail" HeaderText="Email" SortExpression="customerEmail"></asp:BoundField>
<asp:BoundField DataField="fee" HeaderText="Fee" SortExpression="fee"></asp:BoundField>
<asp:BoundField DataField="feedType" HeaderText="Feed Type" ReadOnly="True" SortExpression="feedType"></asp:BoundField>
<asp:BoundField DataField="narrationDesc" HeaderText="Narration" ReadOnly="True" SortExpression="narrationDesc"></asp:BoundField>
</Columns>
</asp:GridView>
【问题讨论】:
-
为什么不只查询您需要的字段,而不是从 NotifyMe 中选择 *。从 NotifyMe 中选择 Id、CustomerName....
-
该代码应该可以工作。尝试清除解决方案并重建所有内容。如果错误仍然存在,请使用调试器检查该代码中发生的情况。旁注,您的变量名称非常混乱。
-
然后只选择部分数据
"SELECT * FROM [dbo].[notifyMe]"-->"SELECT *col1, col2, ... FROM [dbo].[notifyMe]"
标签: c# sql .net vb.net datagridview