如何从List将数据绑定到gridview

时间:2014-08-09 06:25:56

标签: asp.net gridview generic-list

我有一个电话号码的Dto:

namespace Dto
{
public class PhoneNumberDto
{
    public Int16 PhoneTypeId { get; set; }
    public string PhoneNumber { get; set; }
}
}

以及此Dto的列表:

 List<PhoneNumberDto> phonenum = new List<PhoneNumberDto>();

我也有一个gridview:

 <table style="width: 100%">
                <tr>
                    <td style="direction: rtl;"align="center" class="auto-style8">

                        <asp:UpdatePanel ID="UpdatePanelSpect1" runat="server">
                            <ContentTemplate>

                                <asp:GridView ID="GridViewCertificateType" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowCommand="GridViewCertificateType_RowCommand" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True" Width="100%" Font-Names="Tahoma" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">
                                    <Columns>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                              Phone Type
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:DropDownList ID="DDLSpecialty" runat="server" Height="24px" Width="500px">
                                                </asp:DropDownList>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:Label ID="LabelC1" runat="server" Text="Label" Visible="False"></asp:Label>

                                                <asp:TextBox ID="TextBox_Phone" runat="server"></asp:TextBox>                                                </ItemTemplate>
                                        </asp:TemplateField>


                                        <asp:TemplateField>
                                            <FooterTemplate>
                                                <asp:ImageButton ID="ImageButtonAddSpec" runat="server" ImageUrl="~/ImageWebForm/add.png" Width="18px" OnClick="ImageButtonAddSpec_Click" />
                                            </FooterTemplate>
                                            <ItemTemplate>
                                                <asp:ImageButton ID="ImageButtonDel0" runat="server" ImageUrl="~/ImageWebForm/Delete.png" Width="18px" CommandName="Del" CommandArgument="<%# Container.DataItemIndex %>" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                    <FooterStyle BackColor="White" ForeColor="#000066" />
                                    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                                    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                                    <RowStyle ForeColor="#000066" />
                                    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                                    <SortedAscendingCellStyle BackColor="#F1F1F1" />
                                    <SortedAscendingHeaderStyle BackColor="#007DBB" />
                                    <SortedDescendingCellStyle BackColor="#CAC9C9" />
                                    <SortedDescendingHeaderStyle BackColor="#00547E" />
                                </asp:GridView>
                            </ContentTemplate>
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="GridViewCertificateType" EventName="RowCommand" />
                            </Triggers>
                        </asp:UpdatePanel>
                    </td>
                </tr>


            </table> 

现在我想从我的Dot“phonenum”列表中将数据设置到Gridview中的下拉列表和文本框,例如“PhoneTypeId”设置为dropdownlist,phoneNumber设置为文本框

1 个答案:

答案 0 :(得分:0)

您可以将绑定列表定向到GridView,然后在 OnRowDataBound 事件中或直接在模板中执行此类操作。

首先在模板中。

    <asp:TemplateField>
              <HeaderTemplate>
                         Phone Type
             </HeaderTemplate>
             <ItemTemplate>
                 <asp:DropDownList ID="DDLSpecialty" Selectedvalue='<%#((PhoneNumberDto)Container.DataItem).PhoneTypeId%>' runat="server" Height="24px" Width="500px">
                 </asp:DropDownList>
             </ItemTemplate>
             </asp:TemplateField>
            <asp:TemplateField>
            <ItemTemplate>
                 <asp:Label ID="LabelC1" runat="server" Text="Label" 
                  Visible="False"></asp:Label>
                <asp:TextBox ID="TextBox_Phone" 
                 Text='<%#((PhoneNumberDto)Container.DataItem).phoneNumber %>' runat="server"></asp:TextBox>        
           </ItemTemplate>
</asp:TemplateField>

或者在使用FindControl()方法的 OnRowDataBound 事件中,您可以为控件赋值。

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       PhoneNumberDto dtoPhone = (PhoneNumberDto)e.Row.DataItem;
       ((TextBox)(e.Row.FindControl("txtPhoneNo"))).Text = dtoPhone.PhoneNumber;
       ((DropDownList)(e.Row.FindControl("DDLSpecialty"))).SelectedValue = dtoPhone.PhoneTypeId;
    }
}

我希望从2以上选项中获得您的解决方案......