datagrid上面选中好要修改的项目,然后点击button就可以将选中的数据导出道新的页面textbxs中
解决方案 »
- C#读取Excel文件显示到Datagird控件中 ,读取Execl过程有问题,请教
- vs 2008 中winforms程序如何打包?
- c# winform 有没有一个事件,来表示winform窗体已加载完成?
- 请教Windows Phone 7 项目方向
- C#有什么缺点呢?
- 如何让listview显示磁盘某个路径下的文件夹或者文件(含图标和名称),就像资源管理器一样。
- C#调用C++中的方法时,传入的byte型参数不对了,请高手指点
- C#窗体开发中的更新,查询,删除,添加功能的实现
- 如何在Windows平台下捕获键盘动作?
- textbox 光标定位问题
- 消息发送问题--在线等待(帮帮我谢谢了)
- 使用Developer Express .NET V3中combox控件的问题?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGridEdit</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<LINK href="../css/print.css" type="text/css" rel="stylesheet">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<h2 id="H21">实现DataGrid控件中的编辑更新</h2>
<asp:DataGrid id="dgEdit" runat="server" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="4" AutoGenerateColumns="False" DataKeyField="EmployeeID" Width="320px">
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<EditItemStyle ForeColor="#003399" BackColor="White"></EditItemStyle>
<AlternatingItemStyle ForeColor="#003399" BackColor="White"></AlternatingItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="LastName">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.LastName") %>' ID="Label1" NAME="Label1" Width="90">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtLastName" Text='<%# DataBinder.Eval(Container, "DataItem.LastName") %>' Width="90" ForeColor="#003399" Font-Size="12pt">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="FirstName">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.FirstName") %>' ID="Label2" NAME="Label2" Width="90">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtFirstName" Text='<%# DataBinder.Eval(Container, "DataItem.FirstName") %>' Width="90" ForeColor="#003399" Font-Size="12pt">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="City">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.City") %>' ID="Label3" NAME="Label3" Width="90">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtCity" Text='<%# DataBinder.Eval(Container, "DataItem.City") %>' Width="90" ForeColor="#003399" Font-Size="12pt">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn HeaderStyle-CssClass ="onlyShow" ItemStyle-CssClass="onlyShow" ButtonType="LinkButton" UpdateText="<img src=../../images/save.gif border=0 alt='保存'/>"
CancelText="<img src=../../images/cancel.gif border=0 alt='取消'/>" EditText="<img src=../../images/edit.gif border=0 alt='编辑'/>"></asp:EditCommandColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 216px; POSITION: absolute; TOP: 272px" runat="server"
Text="编辑"></asp:Button>
</form>
</body>
</HTML>
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;namespace realtime_forms.test
{
/// <summary>
/// DataGridEdit 的摘要说明。
/// </summary>
public class DataGridEdit : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid dgEdit;
private void Page_Load(object sender, System.EventArgs e)
{
//页面初试化时进行数据绑定
if(!IsPostBack)
DataGridDataBind();
} private void DataGridDataBind()
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter("select top 5 EmployeeID,LastName,FirstName,City from Employees",conn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//填充数据集
da.Fill(ds,"testTable");
//进行数据绑定
dgEdit.DataSource = ds.Tables["testTable"];
dgEdit.DataBind();
}
catch(Exception error)
{
//输出异常信息
Response.Write(error.ToString());
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgEdit.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgEdit_CancelCommand);
this.dgEdit.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgEdit_EditCommand);
this.dgEdit.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgEdit_UpdateCommand);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void dgEdit_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//设置DataGrid控件的编辑项的索引为-1,既取消编辑
dgEdit.EditItemIndex = -1;
//数据绑定
DataGridDataBind();
} private void dgEdit_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//设置DataGrid控件的编辑项的索引为选择的当前项
dgEdit.EditItemIndex = e.Item.ItemIndex;
//数据绑定
DataGridDataBind();
} private void dgEdit_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//取得编辑行的关键字段的值
int empID = (int)dgEdit.DataKeys[e.Item.ItemIndex];
//取得文本框中输入的内容
TextBox newCity = (TextBox)e.Item.FindControl("txtCity");
TextBox newFirstName = (TextBox)e.Item.FindControl("txtFirstName");
TextBox newLastName = (TextBox)e.Item.FindControl("txtLastName");
//定义SQL语句
string sqlCom = "update Employees set City='" + newCity.Text + "',FirstName='"+newFirstName.Text+"',LastName='"+newLastName.Text+"' where EmployeeID="+empID.ToString();
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//定义命令对象
SqlCommand cmd = new SqlCommand(sqlCom,conn);
//打开数据连接
conn.Open();
try
{
//执行SQL命令
cmd.ExecuteNonQuery();
//取消编辑
dgEdit.EditItemIndex = -1;
DataGridDataBind();
}
catch(Exception err)
{
//输出异常信息
Response.Write(err.ToString());
}
finally
{
//关闭连接对象
conn.Close();
}
} private void Button1_Click(object sender, System.EventArgs e)
{
} }
}