0

I have two tables relevant to this question...

tblCellTechnology
  CellTechnologyID, PK
  Description
  CellName

tblActionSchedule
  ActionID, PK
  ActionPhase
  ActionPeriod
  CellTechnology, FK

I have a bulk edit grid view with text boxes displaying the ActionID, Phase, Period and a dropdownlist linked directly to the CellTechnologyID from tblCellTechnology (as per image). I also have another textbox displaying the cell name based on the value in tblScheduleAction.CellTechnologyID (ActionDescription is pulled from another table).

How can I get the selected value from the ddl and write that to tblScheduleAction.CellTechnologyID? When I hard add a CellTechnologyID to tblScheduleActionID, the cellname textbox displays the correct value. I just need to be able to store the ddl.SelectedValue to tblScheduleAction.CellTechnologyID. I am using masterpages and sqldatasources to handle the inserting/updating/deleting of records.

I want the celltechnologyid column in the bottom gridview to display the list of potential celltechnologyid, it does as it is directly linked to that column from the top grid view, the selected value should also be stored in tblScheduleAction.CellTechnologyID. The cellname column should display the respective cellname. As they say, a picture is worth a thousand words... http://imgur.com/21FjOzh

4

1 回答 1

0

通过嵌套的网格视图,我假设您指的是每个父网格视图行内的子网格视图。获取子 gridview 控件的值非常容易。

如何从 ddl 中获取选定的值并将其写入 tblScheduleAction.CellTechnologyID?

  1. 您可以在保存记录FindControl的循环中找到 childgridviewforeach
  2. 然后您将使用查找控件查找下拉列表并保存到您的数据库

检查工作示例(Aspx)

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NestedGridviews.aspx.cs" Inherits="WebApplication1.NestedGridviews" %>
<!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">
    <div>

        <asp:GridView ID="gvParent" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="CategoryID" DataSourceID="sdsParentGrid" 
            onrowdatabound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
                    InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" />
                <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" 
                    SortExpression="CategoryName" />
                <asp:BoundField DataField="Description" HeaderText="Description" 
                    SortExpression="Description" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="False" 
                            DataKeyNames="ProductID" DataSourceID="sdsChildGrid">
                            <Columns>
                                <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                                    InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
                                <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                                    SortExpression="ProductName" />
                                <asp:BoundField DataField="SupplierID" HeaderText="SupplierID" 
                                    SortExpression="SupplierID" />
                                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
                                    SortExpression="CategoryID" />
                                <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" 
                                    SortExpression="Discontinued" />
                                <asp:TemplateField HeaderText="My DropDown Column">
                                    <ItemTemplate>
                                        <asp:DropDownList ID="ddlTest" runat="server">
                                            <asp:ListItem>Potatoes</asp:ListItem>
                                            <asp:ListItem>Tomatoes</asp:ListItem>
                                            <asp:ListItem>Mangoes</asp:ListItem>
                                        </asp:DropDownList>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                        <asp:SqlDataSource ID="sdsChildGrid" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
                            SelectCommand="SELECT [ProductID], [ProductName], [SupplierID], [CategoryID], [Discontinued] FROM [Alphabetical list of products] WHERE ([CategoryID] = @CategoryID)">
                            <SelectParameters>
                                <asp:Parameter Name="CategoryID" Type="Int32" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="sdsParentGrid" runat="server" 
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
            SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories] ORDER BY [CategoryName]">
        </asp:SqlDataSource>

    </div>
    <asp:Button ID="btnSave" runat="server" onclick="btnSave_Click" 
        Text="Save to Database" />
    </form>
</body>
</html>

代码隐藏

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

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

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView gvChild = e.Row.FindControl("gvChildGrid") as GridView;
                SqlDataSource sdsTemp = e.Row.FindControl("sdsChildGrid") as SqlDataSource;
                if (sdsTemp!=null)
                {
                    sdsTemp.SelectParameters[0].DefaultValue = gvParent.DataKeys[e.Row.RowIndex]["CategoryID"].ToString();
                    if (gvChild != null)
                    {
                        gvChild.DataBind();
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow growParent in gvParent.Rows)
            {
                // Find child gridview for each row
                GridView gvChild = growParent.FindControl("gvChildGrid") as GridView;
                if (gvChild != null)
                {
                    // use a foreach loop to find your control
                    foreach (GridViewRow growChild in gvChild.Rows)
                    {
                        DropDownList ddlTemp = growChild.FindControl("ddlTest") as DropDownList;
                        if (ddlTemp != null)
                        {
                            Response.Write(ddlTemp.SelectedValue.ToString());
                        }
                    }
                }
            }
        }
    }
}

在此处输入图像描述

于 2013-04-19T16:22:38.523 回答