DataKeyField="设置为你的主键列"
就可以通过这个属性等到值
就可以通过这个属性等到值
解决方案 »
- C#怎样通过窗体句柄打开、关闭窗体?
- VS2005 C#串口数据接收事件使用?做短信用的
- C#如何实现C++中的联合体union
- mapxtreme 2008的例子
- b/s的c/s化前途会怎样?
- 设置(C#)winform中combobox默认选项
- 异常详细信息: System.InvalidOperationException: .Net 数据 SQL 提供程序(System.Data.SqlClient)要求 Microsoft 数据访问组件(MDAC) 2
- DrawImage画bmp时如何使部分颜色透明不显示?
- 关于datagrid控件得问题。请高手帮忙解决问题。。
- vs.net中找不到VSS啊
- 请教:关于打印预览的问题
- 心情不好,准备辞职.散分+问个白痴小问题
Width="800"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
OnEditCommand="MyDataGrid_Edit"
OnCancelCommand="MyDataGrid_Cancel"
OnUpdateCommand="MyDataGrid_Update"
DataKeyField="au_id"
AutoGenerateColumns="false"
>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
UpdateText="Update" ItemStyle-Wrap="false"/>
<asp:BoundColumn HeaderText="au_id" SortExpression="au_id"
ReadOnly="True" DataField="au_id" ItemStyle-Wrap="false"/>
<asp:TemplateColumn HeaderText="au_lname" SortExpression="au_lname">
<ItemTemplate>
<asp:Label runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "au_lname") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="edit_LName"
Text='<%# DataBinder.Eval(Container.DataItem, "au_lname") %>'/>
</EditItemTemplate>
</asp:TemplateColumn>
...............................
你可以随便写了,主键在上面已经定了后台代码举例: public void MyDataGrid_Delete(Object sender,
DataGridCommandEventArgs E)
{
String deleteCmd = "DELETE FROM Authors WHERE au_id = @Id";
SqlCommand myCommand = new SqlCommand(deleteCmd, myConnection);
myCommand.Parameters.Add(new SqlParameter("@Id",
SqlDbType.VarChar, 11));
// Initialize the SqlCommand "@Id" parameter to the ID of the row
// that was clicked.
myCommand.Parameters["@Id"].Value =
MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
// Connect to the database and delete the specified row.
myCommand.Connection.Open();
// Test whether the delete was accomplished, and display the
// appropriate message to the user.
try
{
myCommand.ExecuteNonQuery();
Message.InnerHtml = "<b>Record Deleted</b><br>";
}
catch (SqlException)
{
Message.InnerHtml = "ERROR: Could not delete record";
Message.Style["color"] = "red";
}
// Close the connection.
myCommand.Connection.Close();
// Rebind the DataGrid to show the updated information.
BindGrid();
}==================================
myCommand.Parameters["@Id"].Value =
MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
这句是关键代码。