从另一个sqldatasource显示gridview中的字段

时间:2011-07-01 08:20:15

标签: asp.net gridview sqldatasource

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

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

Net-Items

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

这是次要表

Net-Locations

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

谢谢

3 个答案:

答案 0 :(得分:4)

请尝试以下代码。

    <%@ 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;
    }
}

答案 1 :(得分:2)

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

答案 2 :(得分:0)

创建一个单独的SQL数据源,并在该表中查询所需的详细信息。

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

将上述代码放在gridview列中。