Solved

web app datagrid - adding/using edit button to grid items

Posted on 2004-11-01
166 Views
Last Modified: 2010-04-23
Hi,

     I have a datagrid that's bound to a table.  I've inserted a new column in VB.NET and put an Edit button in it.  So what I see is my grid with an "Edit" button next to each item.  How do I now use code to:

1) determine when edit has been clicked
2) determine which index was chosen

Thanks!

JP
0
Question by:gleznov
    7 Comments
     

    Author Comment

    by:gleznov
    For clarity, I right-clicked the datagrid, chose edit template, dragged a button into a new column, called it cmdEdit, and now I want to know how I code for the event of someone pressing an edit button next to whatever record?

    JP
    0
     
    LVL 4

    Accepted Solution

    by:
    Hi,
    Datagrid was made to do a lot of works for you, You just declare the property, and it will do.
    This is from my application and it works great.
    In declare a datagrid, you use something like this :

    OnDeleteCommand="delete_grid"
    OnUpdateCommand="update_grid"
    OnCancelCommand="cancel_edit"
    OnEditCommand="edit_grid"

    and in the column
    +use this for the delete column
    <asp:ButtonColumn ItemStyle-Width="20" CommandName="delete" Text="<img border=0
    src=Images/delete.gif>"></asp:ButtonColumn>

    +this is for the column you want to be edited :

    <ItemTemplate>
    <asp:HyperLink Runat=server ID="Hyperlink2"><%#Bind you data here%>
    </asp:HyperLink>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="edit_cat_name" Runat=server Text='<%#DataBinder.Eval(Container.DataItem,"cat_name")%>' >
    </asp:TextBox>
    </EditItemTemplate>

    +this for the edit/cancel/update

    <asp:EditCommandColumn ItemStyle-Width="120" EditText="<img border=0 alt=Edit src=Images/edit.gif>" CancelText=" Cancel" UpdateText="Update |" ItemStyle-Font-Name="Tahoma" ItemStyle-Font-Bold="True" ItemStyle-Font-Size="8"></asp:EditCommandColumn>

    And now, in the codebehind,

    Sub edit_grid(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles news_in_cat.EditCommand
            news_in_cat.EditItemIndex = e.Item.ItemIndex
            BindGrid()
        End Sub
    Sub cancel_edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
            news_in_cat.EditItemIndex = -1
            BindGrid()
    End Sub
    Sub delete_grid(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
            'Pls delete this (CType(news_in_cat.DataKeys(e.Item.ItemIndex), Integer))

            status.Text = i.ToString + "row(s) affetted "
            news_in_cat.EditItemIndex = -1
        BindGrid()
       
       
        End Sub
        Public Sub update_grid(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles news_in_cat.UpdateCommand

            Dim update_value as String
            update_value = CType(e.Item.Cells(your column number).FindControl("your column name"), TextBox).Text
    Update this CType(news_in_cat.DataKeys(e.Item.ItemIndex), Integer)
            news_in_cat.EditItemIndex = -1

            BindGrid()

        End Sub

    Pls suit it in your application, hope this helps
    0
     

    Author Comment

    by:gleznov
    I didn't code the datagrid, I just dragged, dropped, and then edited template.  I'll see if I can apply the above, I still get confused when altering the HTML with ASP code.  

    JP
    0
     

    Author Comment

    by:gleznov
    Is there a way just to make the button indicate the index?  I don't really want to do the editing w/in the grid, but using controls above the grid.  So when they click that "edit" button, I want it to tell my program to fill the control boxes with the information found at that index in the dataset, then after the change I'll update the database, dataset, and rebind the datagrid.

    JP
    0
     

    Author Comment

    by:gleznov
    OK this isn't working for me.  Could someone please lead me on a step by step way of:

    1) making a new column at the front of the grid
    2) sticking a button with text "Edit" in that column so that it appears by each row
    3) accessing an edit-button-clicked event in code?

    I have 1 and 2 working right (I think) but in case I did something wrong, please be thorough.

    JP
    0
     
    LVL 4

    Expert Comment

    by:vinhthuy_nguyen
    Hi buddy, sorry for a late answer
    Acctually I'm kind of stuck in my company works. Sorry, and pls check these links , may be that helps.
    http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q689q
    http://www.dotnet247.com/247reference/msgs/7/35621.aspx

    Regards.
    0
     

    Author Comment

    by:gleznov
    Got it!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now