<%
@ Page language="c#" Codebehind="RowEditLimit.aspx.cs" AutoEventWireup="false" Inherits="DataGridTest.RowEditLimit"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<
HTML
>
<
HEAD
>
<
title
>
RowEditLimit
</
title
>
<
meta
content
="Microsoft Visual Studio .NET 7.1"
name
="GENERATOR"
>
<
meta
content
="C#"
name
="CODE_LANGUAGE"
>
<
meta
content
="JavaScript"
name
="vs_defaultClientScript"
>
<
meta
content
="http://schemas.microsoft.com/intellisense/ie5"
name
="vs_targetSchema"
>
![ExpandedBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<
script
language
="javascript"
>
function ReceiveKey()
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(event.keyCode==13) document.all.Label1.click();
}
</
script
>
</
HEAD
>
<
body
MS_POSITIONING
="GridLayout"
onkeydown
="ReceiveKey()"
>
<
form
id
="Form1"
method
="post"
runat
="server"
>
<
FONT
face
="宋体"
>
<
asp:datagrid
id
="dg"
style
="Z-INDEX: 101; LEFT: 96px; POSITION: absolute; TOP: 104px"
runat
="server"
Height
="208px"
Width
="304px"
AutoGenerateColumns
="False"
>
<
Columns
>
<
asp:BoundColumn
DataField
="ID"
HeaderText
="ID"
ReadOnly
="True"
></
asp:BoundColumn
>
<
asp:BoundColumn
DataField
="Content"
HeaderText
="Content"
ReadOnly
="True"
></
asp:BoundColumn
>
<
asp:BoundColumn
DataField
="Amount"
HeaderText
="Amount"
></
asp:BoundColumn
>
<
asp:BoundColumn
DataField
="Total"
HeaderText
="Total"
></
asp:BoundColumn
>
<
asp:ButtonColumn
Visible
="False"
Text
="edit"
CommandName
="edit"
></
asp:ButtonColumn
>
<
asp:ButtonColumn
Visible
="False"
Text
="update"
CommandName
="update"
></
asp:ButtonColumn
>
</
Columns
>
</
asp:datagrid
><
asp:label
id
="Label1"
style
="Z-INDEX: 102; LEFT: 440px; POSITION: absolute; TOP: 72px"
runat
="server"
Height
="32px"
Width
="40px"
>
保存
</
asp:label
></
FONT
></
form
>
</
body
>
</
HTML
>
using
System;
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
Skyendless.DataAccess;
![None.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
DataGridTest
![ExpandedBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// RowEditLimit 的摘要说明。
/// </summary>
public class RowEditLimit : System.Web.UI.Page
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
protected System.Web.UI.WebControls.DataGrid dg;
protected System.Web.UI.WebControls.Label Label1;
string connectionString = string.Empty;
private void Page_Load(object sender, System.EventArgs e)
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
//具体连接字符串,据自身情况而定
//
connectionString = "Server = (local);uid = sa;pwd = sa;DataBase = test";
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CreateDB();
DGBind();
}
![InBlock.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 创建数据库
/// </summary>
private void CreateDB()
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string createDB = @"
if not exists(select 1 from sysobjects where name = 'test' and xtype = 'u')
Begin
![InBlock.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Create table Test(ID int
,Content varchar(20)
,Amount int
,Total int
)
![InBlock.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
insert into test select 1,'可修改',100,200
union select 2,'固定',200,200
![InBlock.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
End
";
try
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlHelper.ExecuteNonQuery(connectionString,CommandType.Text,createDB);
}
catch(Exception ex)
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Label1.Text = ex.Message.ToString();
}
![InBlock.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 绑定数据
/// </summary>
private void DGBind()
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DataSet ds = new DataSet();
try
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
//具体路径根据自己的情况进行修改
//
string cmdText = "select * from test";
ds = SqlHelper.ExecuteDataset(connectionString,CommandType.Text,cmdText);
dg.DataSource = ds.Tables[0].DefaultView;
dg.DataBind();
}
catch(Exception ex)
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Label1.Text = ex.Message.ToString ();
}
}
![InBlock.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.dg.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_ItemCommand);
this.dg.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dg_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
![InBlock.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![InBlock.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(e.Item.Cells[1].Text == "可修改")
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
e.Item.Attributes.Add("ondblclick","__doPostBack('"+((LinkButton)e.Item.Cells[4].Controls[0]).ClientID.Replace("__","$_")+"','')");
}
}
if(e.Item.ItemType==ListItemType.EditItem)
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Label1.Attributes.Add("onclick","__doPostBack('"+((LinkButton)e.Item.Cells[5].Controls[0]).ClientID.Replace("__","$_")+"','')");
((TextBox)e.Item.Cells[2].Controls[0]).Attributes.Add("onmouseover","this.select()");
((TextBox)e.Item.Cells[3].Controls[0]).Attributes.Add("onmouseover","this.select()");
}
}
![InBlock.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void dg_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(e.CommandName=="edit")
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.dg.EditItemIndex=e.Item.ItemIndex;
}
if(e.CommandName=="update")
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string cmdText = string.Format("Update test set Amount = {0} ,total = {1} where ID = {2}"
,((TextBox)e.Item.Cells[2].Controls[0]).Text
,((TextBox)e.Item.Cells[3].Controls[0]).Text
,e.Item.Cells[0].Text);
SqlHelper.ExecuteNonQuery(connectionString,CommandType.Text,cmdText);
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Label1.Text = ex.Message.ToString();
}
finally
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.dg.EditItemIndex=-1;
}
}
DGBind();
}
![InBlock.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void Button1_Click(object sender, System.EventArgs e)
![ExpandedSubBlockStart.gif](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
}
}
转载于:https://www.cnblogs.com/hzuIT/articles/474606.html