asp.net - 根据 MS Access 查询的结果设置 gridview 列的值

标签 asp.net sql vb.net ms-access gridview

我有一个查询,它从 MS Access 中的表中提取值

SELECT
    tblInstructor.InstructorID,
    tblInstructor.Football AS InstructorRole,
    tblInstructor.Gym AS InstructorRole,
    tblInstructor.Yoga AS InstructorRole
FROM
    tblInstructor
WHERE
    tblInstructor.InstructorID = @InstructID
    OR tblInstructorRole.Football = 1
    OR tblInstructorRole.Gym = 1
    OR tblInstructor.Yoga = 1

然后我将数据绑定(bind)到 GridView

在我的 aspx 页面中,我使用以下方法在 gridview 中构建我的列

<asp:TemplateField HeaderText="Instructor ID">
    <ItemTemplate >
        <asp:Label ID="lblInstructorID" runat="server" Text='<%# Eval("InstructorID") %>' >
        </asp:Label>
    </ItemTemplate>
</asp:TemplateField>  

我在这里遇到了一个问题,我收到多次返回的错误,但是每个讲师只有一个角色,所以我试图获取讲师的角色,即取决于使用上面的 sql 语句是否正确然后将导师角色栏设置为导师的角色

 <asp:TemplateField HeaderText="Instructor Role">
     <ItemTemplate >
        <asp:Label ID="lblInstructRole" runat="server" Text='<%# Eval("InstructorRole") %>' >
        </asp:Label>
     </ItemTemplate>
 </asp:TemplateField>   

我的表有 3 个单独的字段用于教练角色足球、健身房、 Yaga ,它们是教练时间表中的真/假值。我一直在尝试(没有运气)让 gridview 中的 InstructorRole 列来显示他们的角色文本,即足球

我还尝试执行以下操作:

If myReader(3) = True then
    Dim Role1 As String = DataBinder.Eval(e.Item.DataItem,"InstructorRole")
    Role1 = "Football"
elseif myReader(4) = true then
    Dim Role2 As String = DataBinder.Eval(e.Item.DataItem,"InstructorRole")
    Role1 = "Gym"

如果有人可以建议我该怎么做,我似乎无法弄清楚。

最佳答案

您必须在 SQL 查询中使用 Case 语句。您的最终查询将如下所示:

SELECT
tblInstructor.InstructorID,
(case when tblInstructor.Football = 1 then tblInstructor.Football
 case when tblInstructor.Gym = 1 then tblInstructor.Gym
 case when tblInstructor.Yoga = 1 then tblInstructor.Yoga
 else '' END) as InstructorRole
FROM
    tblInstructor
WHERE
    tblInstructor.InstructorID = @InstructID

关于asp.net - 根据 MS Access 查询的结果设置 gridview 列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5924601/

相关文章:

mysql - MySQL 中表比较的多次更新

php - 有没有办法将选定的属性组合到返回的 Laravel 模型中的特定属性中?

vb.net - 如何为只有 nUnit 测试的项目定义入口点?

xml - VB.NET 中的 LINQ to XML

c# - 使用 <ul> 进行导航(无法记住状态)

c# - 检索域 url

javascript - 从不在页面启动时显示窗口

c# - 在 ASP 中继器 C# 中显示/隐藏 div 标签

sql - 将多行合并为一个

asp.net - 为什么我的 GridView SelectedIndexChanged 事件没有触发?