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

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
End Sub

Sub Grid_CancelEdit(Sender As Object, e As DataGridCommandEventArgs)
     'Some other code
     DataGrid1.EditItemIndex = -1
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#:

Moizsaif123Author Commented:
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
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?

Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

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...
I quickely tested it and this code works at 100% in VB.NEY:
In your aspx file:

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

In your code-behind file:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Good article about the datagrid and it also explains what you want to know in chapter 6:


Moizsaif123Author Commented:
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
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?

Moizsaif123Author Commented:
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


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()
                Dim o(1) As Object
                o(0) = i
                o(1) = i
            DataGrid1.AutoGenerateColumns = True
            DataGrid1.DataSource = dt.DefaultView
            DataGrid1.DataMember = "TestTable"
    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.


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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.