S Nash
S Nash

Reputation: 2499

DataGrid Does not seem to allow editing

I'm using a "WebForm" Datagrid.

DataGrid is bound to a dataset and shows data.

Based on my findings to make DataGrid Editable All I need to do is to add an Editcommandcolumn.

I did that, so I have:

<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>

Now the datagrid shows a new column with a hyperlink "EDIT" in the first coulmn.

The problem is nothing happens when I click on Edit on any row.

What am I missing?

Upvotes: 1

Views: 730

Answers (1)

gustavodidomenico
gustavodidomenico

Reputation: 4681

Ok, I just made an example that it is working.

First I have my DataGrid definition:

<asp:DataGrid ID="Grid" runat="server" DataKeyField="DatasetField1" AutoGenerateColumns="False" GridLines="None" OnCancelCommand="Grid_CancelCommand" OnEditCommand="Grid_EditCommand" OnUpdateCommand="Grid_UpdateCommand">
<Columns>
     <asp:BoundColumn HeaderText="Column1" DataField="DatasetField1"></asp:BoundColumn>
     <asp:BoundColumn HeaderText="Column2" DataField="DatasetField2"></asp:BoundColumn>
     <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" HeaderText="Edit"></asp:EditCommandColumn>
 </Columns>

Second I must bind my data grid:

public BindData()
{
    ....
    Grid.DataSource = [MyDataSet With DatasetFiel1 and DatasetField2 fields];
    Grid.DataBind();
}

Third I handle the editting events (look in the DataGrid markup above):

An event for when you click the Edit link button inside the grid:

protected void Grid_EditCommand(object source, DataGridCommandEventArgs e)
{
    Grid.EditItemIndex = e.Item.ItemIndex;
    BindData();
}

An event for when you click the Cancel link button after editting. Just to cancel the changes:

protected void Grid_CancelCommand(object source, DataGridCommandEventArgs e)
{
    Grid.EditItemIndex = -1;
    BindData();
}

Finally an event when you confirm your editting values:

protected void Grid_UpdateCommand(object source, DataGridCommandEventArgs e)
{
    // Here you save your changes to the database
}

Let me know if you have any issue...

Upvotes: 1

Related Questions