DetailsView无法实现编辑操作,但是删除和插入都可以,为什么呀?

兔儿爷 2007-12-14 11:09:05
DetailsView无法实现编辑操作,但是删除和插入都可以,为什么呀?
...全文
130 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tang_fu 2008-12-01
  • 打赏
  • 举报
回复
是不是你的数据库的编辑权限没有设置好啊、
hulangfy 2008-06-17
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

SqlDataAdapter adapter = new SqlDataAdapter();
private System.Windows.Forms.Button button2;
DataTable dt = new DataTable();

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

InitAdapter();
}

private void InitAdapter()
{
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=sa;database=dbtest");
SqlCommand Selectcmd = conn.CreateCommand();
Selectcmd.CommandText = "select * from account";

//增加查询功能
adapter.SelectCommand = Selectcmd;


//增加插入功能
SqlCommand Insertcmd = conn.CreateCommand();
Insertcmd.CommandText = "insert account values(@name,@pwd)";
Insertcmd.Parameters.Add("@name",SqlDbType.NVarChar,50,"username");
Insertcmd.Parameters.Add("@pwd",SqlDbType.NVarChar,50,"password");

adapter.InsertCommand = Insertcmd;

//增加修改功能
SqlCommand Updatecmd = conn.CreateCommand();
Updatecmd.CommandText = "update account set username=@name,password=@pwd where userid=@id";

SqlParameter para = new SqlParameter();
para.ParameterName = "@name";
para.SqlDbType = SqlDbType.NVarChar;
para.SourceColumn = "username";
para.Size = 50;
para.SourceVersion = DataRowVersion.Current;

SqlParameter para1 = new SqlParameter("@pwd",SqlDbType.NVarChar,50,ParameterDirection.Input,false,(byte)0,(byte)0,"password",DataRowVersion.Current,null);

SqlParameter para2 = new SqlParameter("@id",SqlDbType.Int,4,ParameterDirection.Input,false,(byte)0,(byte)0,"userid",DataRowVersion.Original,null);

Updatecmd.Parameters.Add(para);
Updatecmd.Parameters.Add(para1);
Updatecmd.Parameters.Add(para2);


adapter.UpdateCommand = Updatecmd;


//增加删除功能
SqlCommand Deletecmd = conn.CreateCommand();
Deletecmd.CommandText = "delete account where userid=@id";
SqlParameter para3 = new SqlParameter("@id",SqlDbType.Int,4,ParameterDirection.Input,false,(byte)0,(byte)0,"userid",DataRowVersion.Original,null);
Deletecmd.Parameters.Add(para3);
adapter.DeleteCommand = Deletecmd;


}


/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(488, 328);
this.dataGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(512, 40);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "查询";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(512, 96);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "更新";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(592, 374);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
dt.Clear();
adapter.Fill(dt);
this.dataGrid1.DataSource = dt;
}

private void button2_Click(object sender, System.EventArgs e)
{
DataTable dt1 = (DataTable)dataGrid1.DataSource;
adapter.Update(dt1);
}


}
}
xiaolei1982 2007-12-16
  • 打赏
  • 举报
回复
你说的不清楚给你个msdn例子,当然是正确的
C#

<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

void EmployeesDropDownList_OnSelectedIndexChanged(Object sender, EventArgs e)
{
EmployeeDetailsView.DataBind();
}

void EmployeeDetailsView_ItemUpdated(Object sender, DetailsViewUpdatedEventArgs e)
{
EmployeesDropDownList.DataBind();
EmployeesDropDownList.SelectedValue = e.Keys["EmployeeID"].ToString();
EmployeeDetailsView.DataBind();
}

void EmployeeDetailsView_ItemDeleted(Object sender, DetailsViewDeletedEventArgs e)
{
EmployeesDropDownList.DataBind();
}

void EmployeeDetailsSqlDataSource_OnInserted(Object sender, SqlDataSourceStatusEventArgs e)
{
System.Data.Common.DbCommand command = e.Command;
EmployeesDropDownList.DataBind();
EmployeesDropDownList.SelectedValue =
command.Parameters["@EmpID"].Value.ToString();
EmployeeDetailsView.DataBind();
}

</script>

<html >
<head runat="server">
<title>Northwind Employees</title>
</head>
<body>
<form id="form1" runat="server">

<h3>Northwind Employees</h3>

<table cellspacing="10">

<tr>
<td valign="top">
<asp:DropDownList ID="EmployeesDropDownList"
DataSourceID="EmployeesSqlDataSource"
DataValueField="EmployeeID"
DataTextField="FullName"
AutoPostBack="True"
OnSelectedIndexChanged="EmployeesDropDownList_OnSelectedIndexChanged"
RunAt="Server" />
</td>

<td valign="top">
<asp:DetailsView ID="EmployeeDetailsView"
DataSourceID="EmployeeDetailsSqlDataSource"
AutoGenerateRows="false"
AutoGenerateInsertbutton="true"
AutoGenerateEditbutton="true"
AutoGenerateDeletebutton="true"
DataKeyNames="EmployeeID"
Gridlines="Both"
OnItemUpdated="EmployeeDetailsView_ItemUpdated"
OnItemDeleted="EmployeeDetailsView_ItemDeleted"
RunAt="server">

<HeaderStyle backcolor="Navy"
forecolor="White"/>

<RowStyle backcolor="White"/>

<AlternatingRowStyle backcolor="LightGray"/>

<EditRowStyle backcolor="LightCyan"/>

<Fields>
<asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" InsertVisible="False" ReadOnly="true"/>
<asp:BoundField DataField="FirstName" HeaderText="First Name"/>
<asp:BoundField DataField="LastName" HeaderText="Last Name"/>
<asp:BoundField DataField="Address" HeaderText="Address"/>
<asp:BoundField DataField="City" HeaderText="City"/>
<asp:BoundField DataField="Region" HeaderText="Region"/>
<asp:BoundField DataField="PostalCode" HeaderText="Postal Code"/>
</Fields>
</asp:DetailsView>
</td>
</tr>
</table>

<asp:SqlDataSource ID="EmployeesSqlDataSource"
SelectCommand="SELECT EmployeeID, LastName + ', ' + FirstName AS FullName FROM Employees"
Connectionstring="<%$ ConnectionStrings:NorthwindConnection %>"
RunAt="server">
</asp:SqlDataSource>


<asp:SqlDataSource ID="EmployeeDetailsSqlDataSource"
SelectCommand="SELECT EmployeeID, LastName, FirstName, Address, City, Region, PostalCode
FROM Employees WHERE EmployeeID = @EmpID"

InsertCommand="INSERT INTO Employees(LastName, FirstName, Address, City, Region, PostalCode)
VALUES (@LastName, @FirstName, @Address, @City, @Region, @PostalCode);
SELECT @EmpID = SCOPE_IDENTITY()"

UpdateCommand="UPDATE Employees SET LastName=@LastName, FirstName=@FirstName, Address=@Address,
City=@City, Region=@Region, PostalCode=@PostalCode
WHERE EmployeeID=@EmployeeID"

DeleteCommand="DELETE Employees WHERE EmployeeID=@EmployeeID"

ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
OnInserted="EmployeeDetailsSqlDataSource_OnInserted"
RunAt="server">

<SelectParameters>
<asp:ControlParameter ControlID="EmployeesDropDownList" PropertyName="SelectedValue"
Name="EmpID" Type="Int32" DefaultValue="0" />
</SelectParameters>

<InsertParameters>
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="Region" Type="String" />
<asp:Parameter Name="PostalCode" Type="String" />
<asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" />
</InsertParameters>

<UpdateParameters>
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="Region" Type="String" />
<asp:Parameter Name="PostalCode" Type="String" />
<asp:Parameter Name="EmployeeID" Type="Int32" DefaultValue="0" />
</UpdateParameters>

<DeleteParameters>
<asp:Parameter Name="EmployeeID" Type="Int32" DefaultValue="0" />
</DeleteParameters>

</asp:SqlDataSource>
</form>
</body>
</html>
导言 创建一个数据访问层 创建一个业务逻辑层 母板页和站点导航 基本报表 使用ObjectDataSource展现数据 声明参数 编程设置ObjectDataSource的参数值 主/从 使用DropDownList过滤的主/从报表 使用两个DropDownList过滤的主/从报表 跨页面的主/从报表 使用GridView 和DetailView实现的主/从报表 自定义格式化 基于数据的自定义格式化 在GridView控件中使用TemplateField 在DetailsView控件中使用TemplateField 使用FormView 的模板 在GridView的页脚中显示统计信息 编辑插入删除数据 概述插入、更新和删除数据 研究插入、更新和删除的关联事件 在ASP.NET页面中处理BLL/DAL层的异常 给编辑和新增界面增加验证控件 定制数据修改界面 实现开放式并发 为删除数据添加客户端确认 基于用户对修改数据进行限制 分页和排序 分页和排序报表数据 大数据量时提高分页的效率 排序自定义分页数据 创建自定义排序用户界面 自定义按钮行为 GridView里的Button 使用DataList和Repeater显示数据 用DataList和Repeater来显示数据 格式化DataList和Repeater的数据 使用DataList来一行显示多条记录 数据控件的嵌套 使用DataList和Repeater过滤数据 使用DropDownList过滤的主/从报表 跨页面的主/从报表 使用Repeater和DataList实现的主/从报表 使用DataList编辑删除数据 综叙:在DataList里编辑删除数据 批量更新 处理BLL和DAL的异常 在编辑插入界面里添加验证控件 自定义DataList编辑界面 实现开放式并发 为删除数据添加客户端确认 基于用户对修改数据进行限制 DataList和Repeater的分页和排序 DataList和Repeater数据分页 DataList和Repeater数据排序(一) DataList和Repeater数据排序(二) DataList和Repeater数据排序(三) DataList和Repeater的自定义按钮行为 DataList和Repeater里的自定义button 从ASP.NET页面直接访问数据库 47 使用SqlDataSource 控件查询数据(Reeezak) 48 在SqlDataSource中使用参数化查询(Reeezak) 49 使用SqlDataSource插入、更新以及删除数据(Reeezak
导言 创建一个数据访问层 创建一个业务逻辑层 母板页和站点导航 基本报表 使用ObjectDataSource展现数据 声明参数 编程设置ObjectDataSource的参数值 主/从 使用DropDownList过滤的主/从报表 使用两个DropDownList过滤的主/从报表 跨页面的主/从报表 使用GridView 和DetailView实现的主/从报表 自定义格式化 基于数据的自定义格式化 在GridView控件中使用TemplateField 在DetailsView控件中使用TemplateField 使用FormView 的模板 在GridView的页脚中显示统计信息 编辑插入删除数据 概述插入、更新和删除数据 研究插入、更新和删除的关联事件 在ASP.NET页面中处理BLL/DAL层的异常 给编辑和新增界面增加验证控件 定制数据修改界面 实现开放式并发 为删除数据添加客户端确认 基于用户对修改数据进行限制 分页和排序 分页和排序报表数据 大数据量时提高分页的效率 排序自定义分页数据 创建自定义排序用户界面 自定义按钮行为 GridView里的Button 使用DataList和Repeater显示数据 用DataList和Repeater来显示数据 格式化DataList和Repeater的数据 使用DataList来一行显示多条记录 数据控件的嵌套 使用DataList和Repeater过滤数据 使用DropDownList过滤的主/从报表 跨页面的主/从报表 使用Repeater和DataList实现的主/从报表 使用DataList编辑删除数据 综叙:在DataList里编辑删除数据 批量更新 处理BLL和DAL的异常 在编辑插入界面里添加验证控件 自定义DataList编辑界面 实现开放式并发 为删除数据添加客户端确认 基于用户对修改数据进行限制 DataList和Repeater的分页和排序 DataList和Repeater数据分页 DataList和Repeater数据排序(一) DataList和Repeater数据排序(二) DataList和Repeater数据排序(三) DataList和Repeater的自定义按钮行为 DataList和Repeater里的自定义button 从ASP.NET页面直接访问数据库 47 使用SqlDataSource 控件查询数据(Reeezak) 48 在SqlDataSource中使用参数化查询(Reeezak) 49 使用SqlDataSource插入、更新以及删除数据(Reeezak
导言 创建一个数据访问层 创建一个业务逻辑层 母板页和站点导航 基本报表 使用ObjectDataSource展现数据 声明参数 编程设置ObjectDataSource的参数值 主/从 使用DropDownList过滤的主/从报表 使用两个DropDownList过滤的主/从报表 跨页面的主/从报表 使用GridView 和DetailView实现的主/从报表 自定义格式化 基于数据的自定义格式化 在GridView控件中使用TemplateField 在DetailsView控件中使用TemplateField 使用FormView 的模板 在GridView的页脚中显示统计信息 编辑插入删除数据 概述插入、更新和删除数据 研究插入、更新和删除的关联事件 在ASP.NET页面中处理BLL/DAL层的异常 给编辑和新增界面增加验证控件 定制数据修改界面 实现开放式并发 为删除数据添加客户端确认 基于用户对修改数据进行限制 分页和排序 分页和排序报表数据 大数据量时提高分页的效率 排序自定义分页数据 创建自定义排序用户界面 自定义按钮行为 GridView里的Button 使用DataList和Repeater显示数据 用DataList和Repeater来显示数据 格式化DataList和Repeater的数据 使用DataList来一行显示多条记录 数据控件的嵌套 使用DataList和Repeater过滤数据 使用DropDownList过滤的主/从报表 跨页面的主/从报表 使用Repeater和DataList实现的主/从报表 使用DataList编辑删除数据 综叙:在DataList里编辑删除数据 批量更新 处理BLL和DAL的异常 在编辑插入界面里添加验证控件 自定义DataList编辑界面 实现开放式并发 为删除数据添加客户端确认 基于用户对修改数据进行限制 DataList和Repeater的分页和排序 DataList和Repeater数据分页 DataList和Repeater数据排序(一) DataList和Repeater数据排序(二) DataList和Repeater数据排序(三) DataList和Repeater的自定义按钮行为 DataList和Repeater里的自定义button 从ASP.NET页面直接访问数据库 47 使用SqlDataSource 控件查询数据(Reeezak) 48 在SqlDataSource中使用参数化查询(Reeezak) 49 使用SqlDataSource插入、更新以及删除数据(Reeezak

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧