2011-09-23 65 views
2

我的gridview显示了多个ProductName列和ProductID列。我希望它展示的是产品名称可点击列。有人可以帮我弄清楚要排除哪些代码?VB.net gridview显示的数据比我想要的要多

<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 

enter image description here

回答

6

添加属性AutoGenerateColumns="False"。否则,GridView会查看数据源并根据数据源对象的属性自动将列添加到其自身。

+0

太棒了!谢谢!这正是我需要的 – jlg