好的,我只是举了一个例子,它正在工作。
首先我有我的 DataGrid 定义:
<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>
其次,我必须绑定我的数据网格:
public BindData()
{
....
Grid.DataSource = [MyDataSet With DatasetFiel1 and DatasetField2 fields];
Grid.DataBind();
}
第三,我处理编辑事件(查看上面的 DataGrid 标记):
当您单击网格内的编辑链接按钮时的事件:
protected void Grid_EditCommand(object source, DataGridCommandEventArgs e)
{
Grid.EditItemIndex = e.Item.ItemIndex;
BindData();
}
编辑后单击取消链接按钮时的事件。只需取消更改:
protected void Grid_CancelCommand(object source, DataGridCommandEventArgs e)
{
Grid.EditItemIndex = -1;
BindData();
}
最后一个事件,当您确认您的编辑值时:
protected void Grid_UpdateCommand(object source, DataGridCommandEventArgs e)
{
// Here you save your changes to the database
}
如果您有任何问题,请告诉我...