Link to home
Create AccountLog in
Avatar of VBBRett
VBBRett

asked on

GridView information and how to customize Gridviews

I am trying to work with a Gridview in ASP.net 2.0 with VB.NET.  What I want to do is eliminate the select column and only have the column rows I am pulling from the database to be selectable.  How would I do this?  When the row is selected in the grid, I want the userid to be sent to another details page that would display all the detail information.  Please let me know.  Thanks!
Avatar of RDdice
RDdice
Flag of United States of America image

First off you need to turn off the auto generate columns. Then you will need to set up a column for each field you want to display from your bound dataset.

This is a simple example of a DataGrid and normal BoundFields, which is very similaur to what your looking for. When a user hits Edit, a div on the page is unhidden and details about the element are shown.

You will use the DataKeyNames field to get the id of the row that was selected by:

Dim id As Integer = CInt(gvPages.DataKeys(Convert.ToInt32(e.CommandArgument)).Values(0))


<div style="padding:8px 0px 0px 8px;">
<asp:GridView ShowHeader="False" DataKeyNames="id"  runat="server" AlternatingRowStyle-CssClass="DataGridAlt" ID="gvPages" AutoGenerateColumns="False" CssClass="DataGrid" SelectedItemStyle-CssClass="DataGridSelect"  GridLines=None OnItemCommand="ProductCats_ItemCommand" OnRowEditing="gvPages_RowEditing" OnRowCommand="gvPages_RowCommand">
<Columns>
<asp:BoundField DataField="Id" Visible="False" />
<asp:BoundField DataField="Name" >
    <ItemStyle CssClass="DataGridItem" />
</asp:BoundField>
<asp:ButtonField CommandName="Edit" Text="Edit" >
    <ItemStyle CssClass="DataGridAction" />
</asp:ButtonField>
 
</Columns>
    <SelectedRowStyle CssClass="DataGridSelect" />
    <AlternatingRowStyle CssClass="DataGridAlt" />
</asp:GridView>
<div style="padding:5px 0px 0px 0px;text-align:right;width:155px;">
<asp:LinkButton runat="server" ID="AddNew" Text="Add New" OnClick="AddNew_Click" />
</div>
</div>

Open in new window

Avatar of VBBRett
VBBRett

ASKER

The following is what I have for the Gridview.  I want to be able to select a record in the GridView, query it by Userid and it send me to a details page.  Any idea on how to do that?  See the code for what I have please...thanks!
 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
            <Columns>                
                <asp:BoundField DataField="UserID" Visible="false" /> 
                <asp:BoundField DataField="UserName" Visible="true" 
                HeaderText="UserName" SortExpression="UserName"/>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:brs0903ConnectionString %>" 
            SelectCommand="SELECT UserId, UserName FROM aspnet_Users WHERE (UserName &lt;&gt; N'admin')">
        </asp:SqlDataSource>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Irzana
Irzana
Flag of Sri Lanka image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of VBBRett

ASKER

Thank you very much for the Gridview information.