Link to home
Start Free TrialLog in
Avatar of Moiz Saifuddin
Moiz Saifuddin

asked on

A Button Event on my DataGrid when clicked does not occur if I use DataSet as DataSource, So How Do I make the click event occur for the button?

The Button is a Edit/Update/Cancel button on the Column properties of the datagrid. So which Event do I use or How can I make that button to do something if its clicked?

I am using VB.NET and the project is a webapp.
Avatar of the-edge
the-edge

add the code to do something to the Button Click event
If this can help you, it's a standard way of using the Edit/Update/Cancel buttons of a datagrid:

First you have to write the methods that will actually handles the Editing/Cancelling:

Sub Grid_Edit(Sender As Object, e As DataGridCommandEventArgs)
     'Some other code
     DataGrid1.EditItemIndex = e.Item.ItemIndex
     DataGrid1.DataBind
End Sub

Sub Grid_CancelEdit(Sender As Object, e As DataGridCommandEventArgs)
     'Some other code
     DataGrid1.EditItemIndex = -1
     DataGrid1.DataBind
End Sub

Next you have to join these methods to your DataGrid, by using the attributes "oneditcommand" and "oncancelcommand":


<asp:datagrid id="DataGrid1" runat="server" oneditcommand="Grid_Edit" oncancelcommand="Grid_CancelEdit"/>

You can also use the attributes "onupdatecommand" and "ondeletecommand"

Here you have some code in C#:

                   http://www.superdotnet.com/Article.aspx?ArticleID=48
Avatar of Moiz Saifuddin

ASKER

HeyCuSo4, This is what I used

Sub Grid_Edit(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
DataGrid1.Visible = False
DataGrid1.EditItemIndex = e.Item.ItemIndex
DataGrid1.DataBind()
End Sub

The DataGrid1.Visible = False is used just to see if the button_click works, an I put the attributes , oneditcommand="DataGrid1"
but it did not.....what could I be doing wrong?

hmm, it's oneditcommand="Grid_Edit" ...
And I think there might also be a conflict in the last code, since you are saying the same thing two times: "Handles Datagrid1.EditCommand" and oneditcommand="Grid_Edit" are equivalent...
ASKER CERTIFIED SOLUTION
Avatar of CuSo4
CuSo4

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good article about the datagrid and it also explains what you want to know in chapter 6:

http://aspnet.4guysfromrolla.com/articles/040502-1.aspx

D'Mzz!
RoverM
HeyCuSo4, I used the same code yu gave me, stil nothing happnd

<asp:DataGrid id="DataGrid1" runat="server" OnEditCommand="Grid_Edit"></asp:DataGrid>

Sub Grid_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
DataGrid1.Visible = False
DataGrid1.EditItemIndex = e.Item.ItemIndex
DataGrid1.DataBind()
End Sub

I have a question, Are you using DataSet as yur DataSource or DataReader
2)Do I place the Sub Grid_Edit...... in page load or anywhere in the code?


Hey CuSo4, Your code is correct, there is also the same event I can use in Vb itself, the editcommand event, the problem was in my page load for some reason.....all the code neccessary to populate the grid was supposed to be in


if not ispostback then

endif

I dont know wy.....but the event triggers with it....


Yes, I think it's because during a postback the "Grid_Edit()" method is always executed after the Page_Load(). And if you include the method to populate the datagrid every-time a postback occures, the Grid_Edit event is cancelled everytime, because the code thinks you have a new Datagrid with new data that has nothing to do with the previous one... When you include the code to populate the datagrid in a "if not postback" code block, it's only executed the first time you load the page. When a postback occures, the datasource stays the same (is stored in Viewstate I think), and the Grid_Edit event can be triggered...
Hi !
So this is the soluction of all of your problems now. but first i tell you something that you might or might not know.

Very First of All the problem lies in [ Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ] event, you did not post the code that what is the method you are using to get data in the datagrid, but as you said you just create a initial work to test so i guessed that you must have put code in PAGE_LOAD event something like this:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable("TestTable")
            dt.Columns.Add("test1", Type.GetType("System.Decimal", True, True))
            dt.Columns.Add("test2", Type.GetType("System.Decimal", True, True))
            Dim i As Integer
            For i = 0 To 5
                'Dim dr As New DataRow()
                'dr.Item()
                Dim o(1) As Object
                o(0) = i
                o(1) = i
                dt.Rows.Add(o)
            Next
            DataGrid1.AutoGenerateColumns = True
            DataGrid1.DataSource = dt.DefaultView
            DataGrid1.DataMember = "TestTable"
            DataGrid1.DataBind()
    End Sub

means just get some temporary data and bind the grid to test data grid events. or else you must have done some sort of differently like creating a dataset and binding it to datagrid, but what ever the way the problem lies in PAGE_LOAD event where datagrid is DATA BINDED.

So when ever a page get to do any thing in ASP.NET, the web page gets a post back to send server notification this is called [Server Round Trip]. So in Page load event you have to write a line like this:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not Page.IsPostBack Then

             '....... the rest of the code to fill any datasets or datatables .........

        End If
             
End Sub

So by adding this line in the DataGrid it saves the refilling of DataSet/DataTable.
Also the technical reason behind is this what your problem is when you click some button on the data grid the SERVER ROUND TRIP starts and datagird contents to be sent back to the server,
along with information that what is the cause of this round trip or say who told the page to go to server.

but as there is no [ IF ] to stop the refilling of data again , the datagrid gets new data evertime, even when the [DataGrid1_ItemCommand] event is to be called. That's why the DataGrid1 Control loses the information about the reason of the web page's post back or server round trip.
and the event [DataGrid1_ItemCommand] is not fired.

You can try your code with or with out of
[ If not page.isPostback then ...
  End if
] statement and you will get your answer.

That's [A BIG] it.

Regards
Meet

PS:
System 1.7 Intel, 128 MB RAM, VS 2002, .NET framework v1.0.3705, and about 20 GB Hard Disk :D
Hay you posted before that you are not able to run the code that you accepted answer here so i posted my answer,
so accept it or i will report to the comunity services

Regards,
Meet.