数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

GridView控件实现数据项的编辑、更新、取消


发布日期:2019年05月26日
 
GridView控件实现数据项的编辑、更新、取消

using System;

using SystemData;

using SystemDataSqlClient;

using SystemConfiguration;

using SystemCollections;

using SystemWeb;

using SystemWebSecurity;

using SystemWebUI;

using SystemWebUIWebControls;

using SystemWebUIWebControlsWebParts;

using SystemWebUIHtmlControls;

public partial class Demo : SystemWebUIPage

{

protected void Page_Load(object sender EventArgs e)

{

if (PageIsPostBack == false)

{

BindData();

}

}

public void BindData()

{

string strSql = select UserIDC_NameE_NameQQ from Demo_User ;

DataTable dt = SqlHelperExecuteDataset(SqlHelperCONN_STRING CommandTypeText strSql null)Tables[];

GridViewDataSource = dt;

GridViewDataKeyNames = new string[] { UserID };//主键

GridViewDataBind();

}

protected void GridView_PageIndexChanging(object sender GridViewPageEventArgs e)

{

GridViewPageIndex = eNewPageIndex;

BindData();

}

protected void GridView_RowEditing(object sender GridViewEditEventArgs e)

{

GridViewEditIndex = eNewEditIndex;

BindData();

}

protected void GridView_RowCancelingEdit(object sender GridViewCancelEditEventArgs e)

{

GridViewEditIndex = ;

BindData();

}

protected void GridView_RowUpdating(object sender GridViewUpdateEventArgs e)

{

string strSql = Update Demo_User set QQ=@QQ where UserID=@UserID ;

SqlParameter[] para = {

new SqlParameter(@QQ((TextBox)(GridViewRows[eRowIndex]Cells[]Controls[]))TextToString()Trim())

new SqlParameter(@UserID(int)GridViewDataKeys[eRowIndex]Value)

};

SqlHelperExecuteNonQuery(SqlHelperCONN_STRING CommandTypeText strSql para);

GridViewEditIndex = ;

BindData();

}

}

<table align=center bgcolor=#cde border= cellpadding= cellspacing= width=%>

<tr>

<th colspan=>

GridView演示</th>

</tr>

<tr>

<td colspan=  >

<asp:GridView ID=GridView runat=server Width=% AutoGenerateColumns=False AllowPaging=True OnPageIndexChanging=GridView_PageIndexChanging PageSize=

OnRowCancelingEdit=GridView_RowCancelingEdit OnRowEditing=GridView_RowEditing OnRowUpdating=GridView_RowUpdating >

<Columns>

<asp:BoundField DataField=UserID HeaderText=UserID ReadOnly=True />

<asp:BoundField DataField=C_Name HeaderText=中文名字 ReadOnly=True />

<asp:BoundField DataField=E_Name HeaderText=英文名字 ReadOnly=True />

<asp:BoundField DataField=QQ HeaderText=QQ帐号 />

<asp:CommandField HeaderText=编辑 ShowEditButton=True />

</Columns>

<RowStyle HorizontalAlign=Center />

<PagerStyle HorizontalAlign=Right />

</asp:GridView>

</td>

</tr>

</table>

               

上一篇:用sql语句动态的创建access表

下一篇:WPF数据绑定之绑定声明