在gridview中显示来自另一个sqldatasource的字段

发布于 2024-11-18 07:07:51 字数 474 浏览 1 评论 0原文

我正在尝试使用另一个表中的数据实现 gridview 字段,表中名为“LocationID”的字段之一是位置表的外键。 我不想在网格视图上显示数字,我只想查看位置表中的地址字段并使用下拉列表控件编辑值。

这是与 gridview 和 sqldatasource 一起使用的主表

Net-Items

 [ID],[Name]
,[Model]
,[SerialNumber]
,[Company]
,[PurchaseDate]
,[PurchasePrice]
,[MonthPrice]
,[CommitmentPrice]
,[Status]
,[CommitmentDate]
,[Free]
,[TillDate]
,[LocationID]

,这是辅助表,

Net-Locations

[ID]
,[ContactName]
,[Address]
,[City]

谢谢

I am trying to implement a gridview field with data from another table, one of the fields in the table called "LocationID" is a foreign key to the Locations table.
I don't want to display the number on the gridview I want to see only the address field from the Locations table and edit the value using a dropdownlist control.

this is the main table in use with the gridview and sqldatasource

Net-Items

 [ID],[Name]
,[Model]
,[SerialNumber]
,[Company]
,[PurchaseDate]
,[PurchasePrice]
,[MonthPrice]
,[CommitmentPrice]
,[Status]
,[CommitmentDate]
,[Free]
,[TillDate]
,[LocationID]

and this is the secondary table

Net-Locations

[ID]
,[ContactName]
,[Address]
,[City]

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜心 2024-11-25 07:07:51

请尝试下面的代码。

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <h3>Items Details</h3>
    <div>
        <asp:SqlDataSource ID="sds_Items" runat="server" 
            ConnectionString="<%$ ConnectionStrings:Local %>" 
            ProviderName="<%$ ConnectionStrings:Local.ProviderName %>" 
            SelectCommand="select * from Items"></asp:SqlDataSource>
        <asp:SqlDataSource ID="sds_Location" runat="server" 
            SelectCommand="select * from location" 
            ConnectionString="<%$ ConnectionStrings:Local %>" 
            ProviderName="<%$ ConnectionStrings:Local.ProviderName %>"></asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="sds_Items" AutoGenerateEditButton="True" ShowFooter="True">
            <Columns>
            <asp:TemplateField HeaderText="ID" >
                <itemtemplate>
                    <asp:Label ID="ID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                </itemtemplate>
                <edititemtemplate>
                    <asp:TextBox ID="txtID" runat="server" Text='<%# Bind("ID") %>' Width="98%" MaxLength="6"></asp:TextBox>
                </edititemtemplate>
                <footertemplate>
                   <asp:TextBox ID="txtNewID" runat="server" Text='<%# Bind("ID") %>' Width="98%" MaxLength="6"></asp:TextBox>
                </footertemplate>
                <itemstyle width="80px" /> 
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Location" >
                <ItemTemplate>
                    <asp:Label ID="lblLocation" runat="server" Text='<%# LookupLocation(DataBinder.Eval(Container.DataItem, "LocationID")) %>'></asp:Label> 
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlLocation" runat="server" DataSourceID="sds_Location" DataTextField="ID" DataValueField="ID" SelectedValue='<%#Bind("LocationID")%>' Width="98%">
                    </asp:DropDownList> 
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="ddlNewLocation" runat="server" DataSourceID="sds_Location" DataTextField="Address" DataValueField="ID" SelectedValue='<%#Bind("LocationID")%>' Width="95%">
                    </asp:DropDownList> 
                </FooterTemplate>
               <ItemStyle Width="25%" /> 
           </asp:TemplateField> 
            </Columns>
        </asp:GridView>
    </div>
    </form>
    </body>
    </html>





using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected string LookupLocation(object idObj)
    {
        if (string.IsNullOrEmpty(idObj.ToString()))
            return null;

        // store the permissionID passed as an input parameter
        string LocationId = idObj.ToString();

        // find the corresponding name
        IEnumerator enumos = sds_Location.Select(new DataSourceSelectArguments()).GetEnumerator();
        while (enumos.MoveNext())
        {
            DataRowView row = enumos.Current as DataRowView;

            if ((string)row["ID"].ToString() == LocationId)
                return string.Concat(row["Address"].ToString());
        }

        return LocationId;
    }
}

Please try the below code.

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <h3>Items Details</h3>
    <div>
        <asp:SqlDataSource ID="sds_Items" runat="server" 
            ConnectionString="<%$ ConnectionStrings:Local %>" 
            ProviderName="<%$ ConnectionStrings:Local.ProviderName %>" 
            SelectCommand="select * from Items"></asp:SqlDataSource>
        <asp:SqlDataSource ID="sds_Location" runat="server" 
            SelectCommand="select * from location" 
            ConnectionString="<%$ ConnectionStrings:Local %>" 
            ProviderName="<%$ ConnectionStrings:Local.ProviderName %>"></asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="sds_Items" AutoGenerateEditButton="True" ShowFooter="True">
            <Columns>
            <asp:TemplateField HeaderText="ID" >
                <itemtemplate>
                    <asp:Label ID="ID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                </itemtemplate>
                <edititemtemplate>
                    <asp:TextBox ID="txtID" runat="server" Text='<%# Bind("ID") %>' Width="98%" MaxLength="6"></asp:TextBox>
                </edititemtemplate>
                <footertemplate>
                   <asp:TextBox ID="txtNewID" runat="server" Text='<%# Bind("ID") %>' Width="98%" MaxLength="6"></asp:TextBox>
                </footertemplate>
                <itemstyle width="80px" /> 
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Location" >
                <ItemTemplate>
                    <asp:Label ID="lblLocation" runat="server" Text='<%# LookupLocation(DataBinder.Eval(Container.DataItem, "LocationID")) %>'></asp:Label> 
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlLocation" runat="server" DataSourceID="sds_Location" DataTextField="ID" DataValueField="ID" SelectedValue='<%#Bind("LocationID")%>' Width="98%">
                    </asp:DropDownList> 
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="ddlNewLocation" runat="server" DataSourceID="sds_Location" DataTextField="Address" DataValueField="ID" SelectedValue='<%#Bind("LocationID")%>' Width="95%">
                    </asp:DropDownList> 
                </FooterTemplate>
               <ItemStyle Width="25%" /> 
           </asp:TemplateField> 
            </Columns>
        </asp:GridView>
    </div>
    </form>
    </body>
    </html>





using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected string LookupLocation(object idObj)
    {
        if (string.IsNullOrEmpty(idObj.ToString()))
            return null;

        // store the permissionID passed as an input parameter
        string LocationId = idObj.ToString();

        // find the corresponding name
        IEnumerator enumos = sds_Location.Select(new DataSourceSelectArguments()).GetEnumerator();
        while (enumos.MoveNext())
        {
            DataRowView row = enumos.Current as DataRowView;

            if ((string)row["ID"].ToString() == LocationId)
                return string.Concat(row["Address"].ToString());
        }

        return LocationId;
    }
}
久随 2024-11-25 07:07:51

请考虑以下教程关于如何配置 SqlDatasource 控件。特别是第 4 步:使用自定义 SQL 语句或存储过程。

Please consider the following tutorial on how to configure the SqlDatasource control. Especially the Step 4: Using a Custom SQL Statement or Stored Procedure.

玻璃人 2024-11-25 07:07:51

创建一个单独的 SQL 数据源并查询位置表,详细说明该表中您想要的内容。

<edititemtemplate>
    <asp:DropDownList ID="ddl_Address" runat="server" DataSourceID="sqlDS_Location" SelectedValue='<%# Bind("Address") %>' DataTextField="Address" DataValueField="Address">
    </asp:DropDownList>                          
</edititemtemplate>

将以上代码放入 gridview 列中。

Create a separate SQL Datasource and query the Location table details what ever you want in that table.

<edititemtemplate>
    <asp:DropDownList ID="ddl_Address" runat="server" DataSourceID="sqlDS_Location" SelectedValue='<%# Bind("Address") %>' DataTextField="Address" DataValueField="Address">
    </asp:DropDownList>                          
</edititemtemplate>

Put the above code in the gridview columns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文