using GridView in .Net 2

Posts   
 
    
mmeenagh31 avatar
mmeenagh31
User
Posts: 3
Joined: 30-Jun-2005
# Posted on: 16-Aug-2005 14:44:36   

I'm trying to figure out how to implement paging and deleting in gridview in asp.net v2 this is my code to bind to gridview

  VendorItemDependancyTypedList showdependancy = Vendor.getDependancy(item.Id);
        __Dependencies.DataSource = showdependancy;
        __Dependencies.DataBind();

and this is my code for my gridview

<asp:GridView ID="__Dependencies" runat="server" AutoGenerateColumns="False" AllowPaging="True" PageSize="5">
                        <Columns>
                            <asp:TemplateField HeaderText="Vendor Item">
                                <ItemTemplate>
                                    <asp:Label ID="__vendorItem" runat="server" Text='<%# Bind("itemNumber") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("itemNumber") %>'></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField HeaderText="Ratio" DataField="ratio" />                                                 
                            <asp:TemplateField ShowHeader="False">
                                <ItemTemplate>
                                    <asp:LinkButton ID="__Delete" runat="server"  OnClick="__DeleteDependencies_Click" CausesValidation="False" CommandName="Delete"
                                        Text="Delete"></asp:LinkButton>
                                        <asp:Label id="__LoginID" visible="false" runat="server" text= '<%# Bind("vendorItemID") %>' /> 
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

I've tried onclick events to get delete working but it has not worked yet. It is more important that I can get the paging functionality working. can anyone help me out with this one

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 17-Aug-2005 11:51:27   

It just shows the complete typedlist in the grid? When I try to reproduce it, it does show a paged grid, and when I select 'autogenerate delete button' for the grid, it does show me a 'delete' link.

Of course, you have to handle the events yourself. With a typedlist, you have to produce a delete for the entity represented by the row (or entites represented by the row), as the typedlist is readonly.

Frans Bouma | Lead developer LLBLGen Pro
mmeenagh31 avatar
mmeenagh31
User
Posts: 3
Joined: 30-Jun-2005
# Posted on: 17-Aug-2005 15:07:05   

Got it working eventually used this code: Deleting:

 public delegate void GridViewDeleteEventHandler(Object sender, GridViewDeleteEventArgs e);


    void __Dependencies_RowDeleting(Object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow i = __Dependencies.Rows[e.RowIndex];
        int dependancyid = Convert.ToInt32(((Label)i.FindControl("__DependancyId")).Text.ToString());
        Vendor.deleteDependancy(dependancyid);
        BindData();
    }

Paging:

void __Dependencies_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        __Dependencies.PageIndex = e.NewPageIndex;
        BindData();

    }

This is the HTML part:

<asp:GridView ID="__Dependencies" runat="server" AutoGenerateColumns="False" AllowPaging="True"
                        PageSize="5" OnRowDeleting="__Dependencies_RowDeleting" OnPageIndexChanging="__Dependencies_PageIndexChanging">
                        <Columns>
                            <asp:TemplateField HeaderText="Vendor Item">
                                <ItemTemplate>
                                    <asp:Label ID="__DependancyId" runat="server" Visible="false" Text='<%# Bind("id") %>'></asp:Label>
                                    <asp:Label ID="__ItemNumber" runat="server" Text='<%# Bind("itemNumber") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Ratio">
                                <ItemTemplate>
                                    <asp:Label ID="__Ratio" runat="server" Text='<%# Bind("ratio") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:CommandField ShowDeleteButton="True" />
                        </Columns>
                    </asp:GridView>

I still want to be able to enable sorting in the GridView any help on this would also be great.smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 18-Aug-2005 12:17:22   

For sorting, enable sorting in the grid. You tehn get these sorting links at the top which you then have to handle yourself

Frans Bouma | Lead developer LLBLGen Pro
alexdresko
User
Posts: 336
Joined: 08-Jun-2004
# Posted on: 26-Oct-2005 18:12:37   

mmeenagh31 wrote:

I still want to be able to enable sorting in the GridView any help on this would also be great.smile

You ever figure this out? I don't feel like thinking right now. simple_smile