SqlDataSource向Gridview添加新列?

时间:2012-07-04 08:21:13

标签: asp.net datagridview sqldatasource

<div style="width:100%; height:200px; overflow:scroll; border-color:White;">
        <asp:GridView ID="UsersGridView" runat="server" 
            CssClass="label" ShowHeaderWhenEmpty="True" Width="100%" 
            AllowSorting="True" HeaderStyle-BackColor="LightGray" CellPadding="4" 
            ForeColor="#333333" GridLines="None" DataSourceID="UsersDataSource" >
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:BoundField HeaderText="Fleet Description" ReadOnly="True" 
                    HeaderStyle-Wrap="false" HtmlEncode="True" >
                    <HeaderStyle Wrap="False"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField DataField="Fleet Code" HeaderText="Fleet Code" ReadOnly="True" 
                    HeaderStyle-Wrap="false" >
                    <HeaderStyle Wrap="False"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField DataField="First Name" HeaderText="First Name" ReadOnly="True" 
                    HeaderStyle-Wrap="false" >
                    <HeaderStyle Wrap="False"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField DataField="Last Name" HeaderText="Last Name" ReadOnly="True" 
                    HeaderStyle-Wrap="false" >
                    <HeaderStyle Wrap="False"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField DataField="Username" HeaderText="User Name" ReadOnly="True" 
                    HeaderStyle-Wrap="false" >
                    <HeaderStyle Wrap="False"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField DataField="Email" HeaderText="Email" ReadOnly="True" 
                    HeaderStyle-Wrap="false" >
                    <HeaderStyle Wrap="False"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" 
                    HeaderStyle-Wrap="false" >
                    <HeaderStyle Wrap="False"></HeaderStyle>
                </asp:BoundField>
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></HeaderStyle>
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
        <asp:SqlDataSource ID="UsersDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
            SelectCommand="SELECT        UserDataAssociation.Fleet as &quot;Fleet Code&quot;, UserDataAssociation.FirstName as &quot;First Name&quot;, UserDataAssociation.LastName as &quot;Last Name&quot;, aspnet_Users.UserName, aspnet_Membership.Email, 
                     UserDataAssociation.Phone
FROM            aspnet_Membership INNER JOIN
                     aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId INNER JOIN
                     UserDataAssociation ON aspnet_Membership.UserId = UserDataAssociation.UserId"></asp:SqlDataSource>
    </div>

我有一个带有select语句的sqldatasource的gridview。但是,当加载页面时,gridview的原始列加倍,由数据源创建。我希望事实并非如此。

我做错了什么?

由于

2 个答案:

答案 0 :(得分:5)

Gridview AutoGenerateColumns属性设置为False

答案 1 :(得分:3)

在编辑列菜单中有一个“自动生成字段”选项,取消选中它。当您选择SqlDataSource作为数据源时,它会创建列,并且由于某种原因它没有取消选中自动生成字段选项,这就是它创建两次的原因。当您单击网格视图时,右上角会出现一个小白色箭头,提供更多选项,如果展开该菜单,单击“编辑列”,然后您可以取消选中“自动生成字段”选项。或者只需将AutoGenerateColumns属性设置为 false

enter image description here