我的gridview也显示了多个ProductName列和ProductID列。我希望它显示的是产品名称可点击列。有人可以帮我找出要排除的代码吗?
<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server">
<br /><br /><br />
<asp:linkbutton id="btnAll" runat="server" text="ALL" onclick="btnAll_Click" />
<asp:repeater id="rptLetters" runat="server" datasourceid="dsLetters">
<headertemplate> | </headertemplate>
<itemtemplate>
<asp:linkbutton id="btnLetter" runat="server"
onclick="btnLetter_Click" text='<%#Eval("Letter")%>' />
</itemtemplate>
<separatortemplate> | </separatortemplate>
</asp:repeater>
<asp:sqldatasource id="dsLetters" runat="server" connectionstring="<%$ ConnectionStrings:ProductsConnectionString %>"
selectcommand="SELECT DISTINCT LEFT(ProductName, 1) AS [Letter] FROM [Product]">
</asp:sqldatasource>
<asp:gridview id="gvProducts" runat="server" datakeynames="ProductID"
datasourceid="dsProductLookup" style="margin-top: 12px;">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="ProductID"
DataNavigateUrlFormatString="Product/Default.aspx?ID={0}"
DataTextField="ProductName" HeaderText="Product Name"
SortExpression="ProductName" />
</Columns>
</asp:gridview>
<asp:sqldatasource id="dsProductLookup" runat="server"
connectionstring="<%$ ConnectionStrings:ProductsConnectionString %>"
selectcommand="SELECT ProductID, ProductName FROM [Product] ORDER BY [ProductName]">
</asp:sqldatasource>
</asp:Content>
代码隐藏:
Protected Sub btnAll_Click(sender As Object, e As EventArgs)
gvProducts.DataBind()
End Sub
Protected Sub btnLetter_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim btnLetter As LinkButton = TryCast(sender, LinkButton)
If btnLetter Is Nothing Then
Return
End If
dsProductLookup.SelectCommand = [String].Format("SELECT ProductID, ProductName
FROM [Product] WHERE ([ProductName] LIKE '{0}%') ORDER BY [ProductName]",
btnLetter.Text)
End Sub
答案 0 :(得分:6)
添加属性AutoGenerateColumns="False"
。否则,GridView
将查看您的数据源,并根据数据源对象的属性自动将列添加到自身。