DataGrid Edit command

flann

Freshman
Joined
Aug 16, 2005
Messages
33
I have a datagrid and I'm trying to put in the edit command. The edit event works, it opens up the current record to edit, but when I click on the update button or cancel button, those events don't fire. Is there a step that I'm missing that would make these fire?
 
How? I'm sorry I'm pretty new to this, and the books I have don't mention any "wiring up".
 
This is assuming you have an EditCommandColumn in your datagrid, each link button will trigger one of these subs. In each sub you get the key field you specify in your datagrid but using the ItemIndex.
Code:
    Private Sub Datagrid1_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Datagrid1.CancelCommand
       'Cancel command
      Datagrid1.EditItemIndex = -1
    End Sub

    Private Sub Datagrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Datagrid1.UpdateCommand
        'Update command
       Dim ProductID As Integer = Datagrid1.DataKeys(e.Item.ItemIndex)
    End Sub

    Private Sub Datagrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Datagrid1.DeleteCommand
        'Delete command
       Dim ProductID As Integer = Datagrid1.DataKeys(e.Item.ItemIndex)
    End Sub
 
yeah, I have all that. What is happening now is that when I click on edit, it goes to the edit event just fine. The problem is when I hit the update button, it goes through my page_load event, then my edit event, but not my update event. Any ideas?
 
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SqlDataAdapter1.SelectCommand.Parameters("@LeadID").Value = intLeadid
        SqlDataAdapter1.Fill(DsNegotiable1)
        DataGrid1.DataBind()
    End Sub

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

    Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
        SqlDataAdapter1.Update(DsNegotiable1)
        DsNegotiable1.Clear()
        DataGrid1.DataBind()
    End Sub
 
Do you need to load your databind in your page_load every time? Check for IsPostBack first, also, how do you know its not getting to your Updatecommand, are you doing anytype of debug message to see if it gets there?
 
I have tried the If not ispostback then do my page load stuff. It doesn't reload my datagrid on the second trip. I debug by walking through every step that the program is taking for that page, it is never getting to the update or cancel events.
 
Code:
			<asp:datagrid id=DataGrid1 runat="server" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3" GridLines="Vertical" DataSource="<%# DsNegotiable1 %>" AutoGenerateColumns="False" DataMember="UnsecuredDebts">
				<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
				<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
				<ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
				<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084"></HeaderStyle>
				<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
				<Columns>
					<asp:BoundColumn DataField="CreditorName" SortExpression="CreditorName"></asp:BoundColumn>
					<asp:BoundColumn DataField="Code" SortExpression="Code" HeaderText="Code"></asp:BoundColumn>
					<asp:BoundColumn DataField="UnsecuredBalOwed" SortExpression="UnsecuredBalOwed" HeaderText="UnsecuredBalOwed"></asp:BoundColumn>
					<asp:BoundColumn DataField="UnsecuredPctRate" SortExpression="UnsecuredPctRate" HeaderText="UnsecuredPctRate"></asp:BoundColumn>
					<asp:BoundColumn DataField="UnsecuredLastPymtDate" SortExpression="UnsecuredLastPymtDate" HeaderText="UnsecuredLastPymtDate"></asp:BoundColumn>
					<asp:BoundColumn DataField="UnsecuredMinMoPymt" SortExpression="UnsecuredMinMoPymt" HeaderText="UnsecuredMinMoPymt"></asp:BoundColumn>
					<asp:ButtonColumn Text="Delete" ButtonType="PushButton" CommandName="Delete"></asp:ButtonColumn>
					<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
				</Columns>
				<PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" Mode="NumericPages"></PagerStyle>
			</asp:datagrid>
 
¿Don't you have to add this to your datagrid definition?
<asp:datagrid id=DataGrid1 runat="server" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3" GridLines="Vertical" DataSource="<%# DsNegotiable1 %>" AutoGenerateColumns="False" DataMember="UnsecuredDebts" OnUpdateCommand="DataGrid1_UpdateCommand">

And OnCancelCommand, etc, etc.....
 
Back
Top