博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Gridview进入编辑状态,新手来看。
阅读量:4357 次
发布时间:2019-06-07

本文共 2760 字,大约阅读时间需要 9 分钟。

可能是学习能力有限吧,我从昨晚到今天,累积起来起码五六个小时,才终于弄懂了这个Gridview的编辑。毕竟找来的代码都是没有注释和说明的。

我想记录下来,也希望后来的新丁能少走弯路。

 

  

protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                BindData();            }        }        private void BindData()        {            string sql = string.Format("select * from Student");            DataTable dt = SqlHelper.ExctuteDataTable(sql);            GridView1.DataSource = dt;            GridView1.GridLines = GridLines.Both;            GridView1.RowStyle.HorizontalAlign = HorizontalAlign.Center;//单元格文字居中            GridView1.DataBind();                        GridView1.EditRowStyle.ForeColor = Color.Red;        }        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)        {            GridView1.EditIndex = e.NewEditIndex;            BindData();        }        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)        {            /*先获得所要编辑的行的第0号单元格里的第一个子控件,转成TextBox类型然后取值*/            int Sid = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString().Trim());                        /*FindControl里的参数填控件的id,但我们需要先获取这个id,所以不推荐这种用法。*/            //string Sname = (GridView1.Rows[e.RowIndex].Cells[0].FindControl("Sname") as TextBox).Text;            string Sname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString().Trim();                        string Sage = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString().Trim();            string Sphone = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString().Trim();            string Spwd = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString().Trim();            string sql = string.Format("update Student set Sid={0},Sname='{1}',Sage='{2}',Sphone='{3}',Spwd='{4}'where Sid={5}",                 Sid, Sname, Sage, Sphone, Spwd, int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString()));            SqlHelper.ExecuteNonQuery(sql);        }        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)        {            GridView1.EditIndex = -1;            BindData();        }

  

转载于:https://www.cnblogs.com/liangtonali/p/4853602.html

你可能感兴趣的文章
每日英语
查看>>
SpringCloud+feign 基于Springboot2.0 负载均衡
查看>>
【BZOJ5094】硬盘检测 概率
查看>>
mac上n次安装与卸载mysql
查看>>
Python之单元测试——HTMLTestRunner
查看>>
WebNotes(PHP、css、JavaScript等)
查看>>
C++:文件的输入和输出
查看>>
Http协议、Tomcat、servlet
查看>>
Spring Boot (11) mybatis 关联映射
查看>>
macOS 下安装tomcat
查看>>
字符串格式化复习笔记
查看>>
c++ 宏定义调用不定参数的函数
查看>>
动态规划典型例题--背包问题九讲
查看>>
Qt之QHeaderView自定义排序(终极版)
查看>>
python----logging
查看>>
LBP特征 学习笔记
查看>>
与TIME_WAIT相关的几个内核参数修改测试讨论结论
查看>>
webpack构建react应用三:使用webpack Loaders 模块加载器(一)
查看>>
Java JDBC
查看>>
走势终完美 --执子之手
查看>>