<asp:DataGrid runat="server" ID="dgWatch" AutoGenerateColumns="False" Width="2560">
        <Columns>
            <asp:BoundColumn DataField="值班人">
                <HeaderStyle Width="80px" />
            </asp:BoundColumn>
            
            <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:DropDownList runat="server">
                        <asp:ListItem Value="1" Selected="True">早班</asp:ListItem>
                        <asp:ListItem Value="2">中班</asp:ListItem>
                        <asp:ListItem Value="3">晚班</asp:ListItem>
                        <asp:ListItem Value="4">公休</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
                <HeaderStyle Width="80px" />
            </asp:TemplateColumn>
<asp:TemplateColumn>
                <ItemTemplate>
                    <asp:DropDownList runat="server">
                        <asp:ListItem Value="1" Selected="True">早班</asp:ListItem>
                        <asp:ListItem Value="2">中班</asp:ListItem>
                        <asp:ListItem Value="3">晚班</asp:ListItem>
                        <asp:ListItem Value="4">公休</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
                <HeaderStyle Width="80px" />
            </asp:TemplateColumn>
<asp:TemplateColumn>
                <ItemTemplate>
                    <asp:DropDownList runat="server">
                        <asp:ListItem Value="1" Selected="True">早班</asp:ListItem>
                        <asp:ListItem Value="2">中班</asp:ListItem>
                        <asp:ListItem Value="3">晚班</asp:ListItem>
                        <asp:ListItem Value="4">公休</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
                <HeaderStyle Width="80px" />
            </asp:TemplateColumn>
<asp:TemplateColumn>
                <ItemTemplate>
                    <asp:DropDownList runat="server">
                        <asp:ListItem Value="1" Selected="True">早班</asp:ListItem>
                        <asp:ListItem Value="2">中班</asp:ListItem>
                        <asp:ListItem Value="3">晚班</asp:ListItem>
                        <asp:ListItem Value="4">公休</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
                <HeaderStyle Width="80px" />
            </asp:TemplateColumn>
<asp:TemplateColumn>
                <ItemTemplate>
                    <asp:DropDownList runat="server">
                        <asp:ListItem Value="1" Selected="True">早班</asp:ListItem>
                        <asp:ListItem Value="2">中班</asp:ListItem>
                        <asp:ListItem Value="3">晚班</asp:ListItem>
                        <asp:ListItem Value="4">公休</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
                <HeaderStyle Width="80px" />
            </asp:TemplateColumn>         </Columns>
    </asp:DataGrid>

解决方案 »

  1.   

    在DropDownList设置一个ID,然后用循环访问每一项,(DropDownList)dgWatch.item.Findcontrol("查找ddl的ID");即可得到对象
      

  2.   

    aspx:
    <asp:DropDownList ID="DropDownList1" runat="server">
                            <asp:ListItem Value="1" Selected="True">早班</asp:ListItem>
                            <asp:ListItem Value="2">中班</asp:ListItem>
                            <asp:ListItem Value="3">晚班</asp:ListItem>
                            <asp:ListItem Value="4">公休</asp:ListItem>
                        </asp:DropDownList>
    cs:
    foreach (DataGridItem dgi in dgWatch.Items)
            {
                DropDownList ddl = (DropDownList)dgi.FindControl("DropDownList1");
                //其他DropDownList用一样的方法可以找到
                if (ddl != null)
                {
                    //对象存在
                }
            }
      

  3.   

    我现在是希望一次性把值班人以及(读出dropdownlist中选定项的value值)即值班类型读取出来,然后插入到数据库表中,请问有什么方法吗?我主要是没大用过dataGrid控件
      

  4.   

    以下是页面源代码,劳烦看看  
    <asp:DataGrid runat="server" ID="dgWatch" AutoGenerateColumns="False" Width="2560px" AllowPaging="True"  PageSize="5" OnPageIndexChanged="dgWatch_PageIndexChanged">
            <Columns>
                <asp:BoundColumn DataField="值班人">
                    <HeaderStyle Width="80px" />
                </asp:BoundColumn>
                
                <asp:TemplateColumn>
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server">
                            <asp:ListItem Value="1" Selected="True">早班</asp:ListItem>
                            <asp:ListItem Value="2">中班</asp:ListItem>
                            <asp:ListItem Value="3">晚班</asp:ListItem>
                            <asp:ListItem Value="4">公休</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                    <HeaderStyle Width="80px" />
                </asp:TemplateColumn>
                 <asp:TemplateColumn>
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList2" runat="server">
                            <asp:ListItem Value="1" Selected="True">早班</asp:ListItem>
                            <asp:ListItem Value="2">中班</asp:ListItem>
                            <asp:ListItem Value="3">晚班</asp:ListItem>
                            <asp:ListItem Value="4">公休</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                    <HeaderStyle Width="80px" />
                </asp:TemplateColumn>
                 <asp:TemplateColumn>
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList3" runat="server">
                            <asp:ListItem Value="1" Selected="True">早班</asp:ListItem>
                            <asp:ListItem Value="2">中班</asp:ListItem>
                            <asp:ListItem Value="3">晚班</asp:ListItem>
                            <asp:ListItem Value="4">公休</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                    <HeaderStyle Width="80px" />
                </asp:TemplateColumn>
        
            </Columns>
                 <PagerStyle NextPageText="下一页" PrevPageText="上一页" />
        </asp:DataGrid>
      

  5.   

    上面已经说得很清楚了,有一个foreach循环,你想怎么取都可以了啊 自己判断一下是否选定这个应该很简单吧 和平时操作DropDownList是一样的
      

  6.   

    foreach (DataGridItem item in dgWatch.Items)
                {
                    for (int i = 1; i < dgWatch.Columns.Count; i++)
                    {
                        /*
                         * 这个Controls[0]中的“0”你要适当的换一下才能找到对应的控件,
                         * 因为这个空间我没有加ID。你也可以加ID然后用FindControl("")查找。
                         **/
                        DropDownList list = (DropDownList)item.Cells.Controls[0];
                        string str = list.SelectedValue;//-----这个就是你要获取的值。
                    }
                }
      

  7.   

    忘了加判断了。
    if(list != null)
    {
        string str= list.SelectedValue;
    }
      

  8.   

    我希望以
    用户名  班种(即dropdownlist中选定的值) 日期 
    的形式插入数据库,上面的方法行吗?
    sql 语句应如下的形式,请各位指点一下:
    insert into banzhong values(u_name,bz_id,pbz_time)