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

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

using System

using System Data

using System Data SqlClient

using System Configuration

using System Collections

using System Web

using System Web Security

using System Web UI

using System Web UI WebControls

using System Web UI WebControls WebParts

using System Web UI HtmlControls

public partial class Demo : System Web UI Page

{

     protected void Page_Load(object sender EventArgs e)

     {

         if (Page IsPostBack == false)

         {

             BindData()

         }

     }

     public void BindData()

     {

         string strSql = select UserID C_Name E_Name QQ from Demo_User

         DataTable dt = SqlHelper ExecuteDataset(SqlHelper CONN_STRING CommandType Text strSql null) Tables[ ]

         GridView DataSource = dt

         GridView DataKeyNames = new string[] { UserID }//主键

         GridView DataBind()

     }

     protected void GridView_PageIndexChanging(object sender GridViewPageEventArgs e)

     {

         GridView PageIndex = e NewPageIndex

         BindData()

     }

     protected void GridView_RowEditing(object sender GridViewEditEventArgs e)

     {

         GridView EditIndex = e NewEditIndex

         BindData()

     }

     protected void GridView_RowCancelingEdit(object sender GridViewCancelEditEventArgs e)

     {

         GridView EditIndex =

         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)(GridView Rows[e RowIndex] Cells[ ] Controls[ ])) Text ToString() Trim())

                                 new SqlParameter( @UserID (int)GridView DataKeys[e RowIndex] Value)

                                }

         SqlHelper ExecuteNonQuery(SqlHelper CONN_STRING CommandType Text strSql para)

         GridView EditIndex =

         BindData()

     }

}

<table align= center bgcolor= #c de 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>

lishixinzhi/Article/program/net/201311/11647

public static DataTable Cpdt

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

Cpdt = new DataTable()

Createbt()

GridView1.DataSource = Cpdt

GridView1.DataBind()

}

}

void Createbt()

{

DataColumn mycol = new DataColumn()

Cpdt.Columns.Add(new DataColumn("CPID", typeof(Int32)))

Cpdt.Columns.Add(new DataColumn("CPNAME", typeof(String)))

Cpdt.Columns.Add(new DataColumn("CPDJ", typeof(Single)))

Cpdt.Columns.Add(new DataColumn("SL", typeof(Int32)))

Cpdt.Columns.Add(new DataColumn("CPSUM", typeof(Double)))

Cpdt.AcceptChanges()

Cpdt.PrimaryKey = new DataColumn[] { Cpdt.Columns[0] }

Cpdt.AcceptChanges()

}

protected void Button1_Click(object sender, EventArgs e)

{

DataRow myrow = Cpdt.NewRow()

if (Cpdt.Rows.Count==0)

{

myrow[0] = 1

}

else

{

myrow[0] = int.Parse(Cpdt.Rows[Cpdt.Rows.Count-1][0].ToString()) + 1

}

myrow[1] = 2

myrow[2] = 3

myrow[3] = 4

Cpdt.Rows.Add(myrow)

var mm = Cpdt.AsEnumerable().Sum(p =>Convert.ToDouble(p["SL"]))

//Label3.Text = mm.ToString()

Cpdt.AcceptChanges()

GridView1.DataSource = Cpdt

GridView1.DataBind()

}

删除 *** 作的话可以在ID列前面插入DataGridViewCommoBoxColumn列,用于选择要删除的项,然后判断是否选中。编辑的话可以在新窗口中。将当前选中的行DataGridViewRow传递过去。然后将值绑定到控件上。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11951677.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存