gridview编辑列里嵌套dropdownlist

把编辑列设模板列,然后dropdownlist绑定数据源
然后在databinding里把selectedvalue的值绑定到要更新的列名上

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns=False  DataKeyNames="id"
                        DataSourceID="SqlDataSource1" Width="292px">
                        <Columns>
                            <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
                            <asp:TemplateField HeaderText="roles" SortExpression="roles">
                                <EditItemTemplate>
                                    &nbsp;<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
                                        DataTextField="power_id" DataValueField="power_id" SelectedValue='<%# Bind("roles") %>'>
                                    </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:cmssConnectionString %>"
                                        SelectCommand="SELECT [power_id] FROM [power]"></asp:SqlDataSource>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("roles") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:CommandField ShowEditButton="True" />
                        </Columns>
                       
                    </asp:GridView>
                </td>
            </tr>
            <tr>
                <td style="width: 73px">
                </td>
                <td style="width: 100px">
                    &nbsp;<asp:SqlDataSource ID="SqlDataSource1" runat="server" UpdateCommand ="update admin set roles=@roles where id=@original_id"  OldValuesParameterFormatString ="original_id" ConnectionString="<%$ ConnectionStrings:cmssConnectionString %>"
                        SelectCommand="SELECT [roles], [id] FROM [admin]"></asp:SqlDataSource>
                </td>

这其实是应该挺简单的问题,我竟然花了两天时间才搞定,之间百度和google不停的搜索,可以搜索到的都是gridviewguy网站里的那个gridview和dropdownlist嵌套的文章,虽然那个实现的比我想要的复杂的多,可是并不是我需要的,好不容易搜索到MSDN里的一篇文章才搞定,那个好像是法文写的,还好我只要看图就能懂~,并不需要看他文字里讲什么.

你可能感兴趣的:(GridView)