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

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
gleznovAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
vinhthuy_nguyenConnect With a Mentor Commented:
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
 
gleznovAuthor Commented:
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
 
gleznovAuthor Commented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
gleznovAuthor Commented:
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
 
gleznovAuthor Commented:
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
 
vinhthuy_nguyenCommented:
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
 
gleznovAuthor Commented:
Got it!
0
All Courses

From novice to tech pro — start learning today.