GridView的绑定,添加,删除,更改;

这篇具有很好参考价值的文章主要介绍了GridView的绑定,添加,删除,更改;。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

GridView的绑定,添加,删除,更改;
在用GridView控件时候的操作如下
用的的数据库如下GridView的绑定,添加,删除,更改;

接下来是gridview的使用
GridView的绑定,添加,删除,更改;
GridView的绑定,添加,删除,更改;

GridView的绑定,添加,删除,更改;
GridView的绑定,添加,删除,更改;
前台代码如下

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridviewUserInfo.aspx.cs" Inherits="GridviewUserInfo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="gvStudent" runat="server" AutoGenerateColumns="False" Height="177px" Width="1056px" DataKeyNames="id" OnRowCancelingEdit="gvStudent_RowCancelingEdit" OnRowDeleting="gvStudent_RowDeleting" OnRowEditing="gvStudent_RowEditing" OnRowUpdating="gvStudent_RowUpdating" OnSelectedIndexChanged="gvStudent_SelectedIndexChanged" >
                <Columns>
                    <asp:TemplateField HeaderText="学号">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Id") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
                        </ItemTemplate>
                      
                    </asp:TemplateField>
                    <asp:BoundField DataField="Name" HeaderText="姓名" />
                    <asp:BoundField DataField="Sex" HeaderText="性别" />
                    <asp:BoundField DataField="Age" HeaderText="年龄" />
                    <asp:BoundField DataField="Address" HeaderText="地址" />
                    <asp:CommandField ShowDeleteButton="True" />
                    <asp:CommandField ShowEditButton="True" />
                </Columns>
            </asp:GridView>
        </div>
        <br />
        <br />
        <div>
            姓名:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
            性别:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
            年龄:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
            地址:<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />
            <asp:Button ID="btnSubmit" runat="server" Text="添加" OnClick="btnSubmit_Click" />
        </div>
    </form>
</body>
</html>

后台代码如下

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/*
 绑定
1.数据库位置
2.数据库连接通道
3.找到表的位置,根据通道
4.创建数据集
5.把找到的表,放到数据集
6.将数据集放到数据源
7.将数据源绑定

添加
Button
1.获取要添加的内容
2.数据库位置
3.数据库连接通道
4.将获取要添加的内容添加到表里,根据通道
5.打开数据库
6.获取受影响行数
7.关闭数据库
8.判断是否成功
9.弹窗告诉是否成功
 



删除
删除事件
放入ID
1.数据库位置
2.数据库连接通道
3.获取受影响行数的id
4.SQL语句
5.将删除SQL语句放到表里,根据通道
5.打开数据库
6.获取受影响行数
7.关闭数据库
8.判断是否成功
9.弹窗告诉是否成功
 */
/*
 SqlConnection  数据库连接对象
 SqlDataAdapter   数据填充器(数据适配器)
 DataSet        数据集(内存的表的集合)
 DataSource    数据源
 DataBind      数据绑定
 SqlCommand  数据库执行语句
 * ExecuteNonQuery获取受影响行数
 
选择:select * from table1 where 范围
插入:insert into table1(field1,field2) values(value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field1=value1 where 范围
 */
public partial class GridviewUserInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            StudentBind();
        }
    }
    /// <summary>
    /// GridView的数据绑定;
    /// </summary>
      //Student是数据库的名字
      string connStr = "Data Source=.;Initial Catalog=Student;Integrated Security=True";
    public void StudentBind()
    {
        SqlConnection conn = new SqlConnection(connStr);//数据库的连接对象;
        string sql = "select * from StudentInfos";//sql语句从数据库里面查询数据;
        SqlDataAdapter sda = new SqlDataAdapter(sql,conn);//数据填充器;
        DataSet ds = new DataSet();//建立数据存放的容器
        sda.Fill(ds);//将数据放入到容器里面;
        gvStudent.DataSource = ds;//数据源
        gvStudent.DataBind();//数据的绑定;
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //首先去掉前后的空格;Trim();
        string names = TextBox2.Text.Trim();
        string gender = TextBox3.Text.Trim();
        string ages = TextBox4.Text.Trim();
        string address1 = TextBox5.Text.Trim();
        SqlConnection conn = new SqlConnection(connStr);
        string sql = "insert into StudentInfos values('" + names + "','" + gender + "','" + ages + "','" + address1 + "')";
        SqlCommand cmd = new SqlCommand(sql, conn);
        conn.Open();
        int flag = cmd.ExecuteNonQuery();//获取受影响的行数;
        conn.Close();
        if (flag > 0)
        {
            StudentBind();
            Response.Write("<script>alert('添加成功')</script>");
           
        }
        else
        {
            Response.Write("<script>alert('添加失败')</script>");
            
        }
    }

  



    protected void gvStudent_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        //这是指获取GridView1控件的第“e.RowIndex+1”行的第2列单元格内的第一个控件
        //e.RowIndex是指当前鼠标选中的行的序号,+1是因为数组的下标从0开始,0表示第1,1实际表示第2了
        string names =( (TextBox)(this.gvStudent.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
        string gender = ((TextBox)(this.gvStudent.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
        string ages = ((TextBox)(this.gvStudent.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
        string address1 = ((TextBox)(this.gvStudent.Rows[e.RowIndex].Cells[4].Controls[0])).Text;
        //取出当前操作行的DataKeys对应的Names的ID
        int id = Convert.ToInt32(gvStudent.DataKeys[e.RowIndex]["id"]);
        string sql = "update StudentInfos set Name='" + names+ "',Sex='" + gender+ "',Age='" + ages+ "',Address='" + address1+"'where Id='"+id+"'";
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand sda = new SqlCommand(sql, conn);
        conn.Open();
        int flag = sda.ExecuteNonQuery();
        conn.Close();
        if (flag > 0)
        {
            StudentBind();
            Response.Write("<script>alert('编辑成功')</script>");
            gvStudent.EditIndex = -1;
        }
        else
        {
            Response.Write("<script>alert('编辑失败')</script>");
        }
    }

    protected void gvStudent_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //把当前编辑的索引,赋给控件的编辑索引,(让编辑的行显示出来)
        gvStudent.EditIndex = e.NewEditIndex;
        //绑定
        StudentBind();
    }

    protected void gvStudent_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //获取编辑行的id值
        int id = Convert.ToInt32(gvStudent.DataKeys[e.RowIndex]["id"]);
        SqlConnection conn = new SqlConnection(connStr);
        string sql = "delete from StudentInfos where Id='" + id + "'";
        SqlCommand cmd = new SqlCommand(sql, conn);
        conn.Open();
        int flag = cmd.ExecuteNonQuery();
        conn.Close();
        if (flag > 0)
        {
            StudentBind();
            Response.Write("<script>alert('删除成功')</script>");
        }
        else
        {
            Response.Write("<script>alert('删除失败')</script>");
        }

    }

    protected void gvStudent_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        //让编辑行的索引退回去
        gvStudent.EditIndex = -1;
        //绑定
        StudentBind();
    }

    protected void gvStudent_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

结果如下
GridView的绑定,添加,删除,更改;文章来源地址https://www.toymoban.com/news/detail-504255.html

到了这里,关于GridView的绑定,添加,删除,更改;的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Linux文件管理(文件/目录的创建、更改、删除)

    1.严格区分大小写 2.文件命名不能使用字符\\\"/\\\" 3.目录或文件名的长度不能超过255个字符 建议: (1)文件名由两个或两个以上单词组成时,尽量使用\\\"_\\\"来代替space键 (2)尽量不用字母的大小写来区分文件或者目录 4.Linux的文件扩展名在Linux的操作系统中没有意义。换句话说,

    2024年02月07日
    浏览(39)
  • opencv如何给图片添加中文并更改字体

     opencv中自带的cv2.putText()函数不能在图像中绘制汉字,可以通过添加PIL模块来达到在图像中显示汉字 通过PIP命令来添加库 指令如下 下载模块之后就可以通过调用来在图片上显示中文了,示例代码如下  如果想更改中文显示字体的话,在电脑的字体库中找到你想要显示的的字

    2024年02月09日
    浏览(41)
  • 删除AWS绑定的信用卡账户

    前言 想使用aws的免费一年的ec2服务,所以必须先绑定信用卡,试了绑定储蓄卡还不行。绑定信用卡的时候只需要写卡号,卡片到期日期以及户头名即可。买上面的服务都不需要输入密码就可以购买。如果你用的免费服务,如果到期1年之后有其他费用,会悄无声息的扣款你的

    2024年02月12日
    浏览(34)
  • Pycharm添加或者更改python解析器(Interpreter)

    打开目录,添加AnaCond添加环境下的python.exe,确定即可。 点击编辑配置 直接下拉选择相应解析器即可。

    2024年02月16日
    浏览(28)
  • Crow:黑魔法 添加路由3 绑定lambda

    Crow:黑魔法2 new_rule_tagged实现模板参数的绑定-CSDN博客 RuleT 实际的类型是TaggedRulexxx,yyy,... 所以这个函数就是生成一个TaggedRulexxx,yyy,...的对象,然后将其加入到: std::vectorstd::unique_ptrBaseRule all_rules_; 之后返回这个对象  TaggedRule类的

    2024年02月04日
    浏览(30)
  • QMessageBox手动添加按钮并绑定按钮的信号

    视频展示效果(结合代码看效果更佳哦,代码在最下面): QMessageBox手动添加有重试效果的按钮 效果图:  点击详细文本之后展开如下图:   图标可选: QMessageBox::Critical 错误图标 QMessageBox::NoIcon 没有图标 QMessageBox::Question 提问图标 QMessageBox::Information 消息图标 QMessageBox::Wa

    2024年02月16日
    浏览(28)
  • 更改c盘计算机权限,Win10系统中C盘文件无法更改写入删除提示没有权限如何解决

    C盘是电脑中重要的系统盘,我们有时候需要在C盘中进行创建或者修改删除文件等,可是在win10系统中,很多用户却遇到磁盘文件无法更改写入删除等操作,并提示没有权限,碰到这样的要如何解决呢?接下来随小编来看看Win10系统中C盘文件无法更改写入删除提示没有权限的解

    2024年02月11日
    浏览(36)
  • windows11删除微软账户,使用本地账户登录,解除绑定

    进入设置,左侧点击“账户”,选择账户信息,找到账户设置,点击“ 改用本地账户登录 ”或者”停 止自动登录到所有的Microsoft应用 ”等,可能略有不同,但是都是一个效果。 注意,有时候可能没有改用本地账户登录选项,我一开始也没有 改用本地账户登录 选项,就选择

    2024年02月16日
    浏览(83)
  • Win10系统无法写入删除更改C盘文件的解决方法

    前言:有个客户的windows系统,明明是administrator,确对windows下的文件无法编辑保存,头疼。 网上找了好多资料,终于... 1、打开 gpedit.msc,策略管理器   2、在本地组策略编辑器界面中,我们依次展开至“计算机配置 - Windows设置 - 安全设置 - 本地策略 - 安全选项”中,找到“

    2024年02月12日
    浏览(34)
  • 024:vue中动态添加ref,通过ref更改css

    第024个 查看专栏目录: VUE ------ element UI 在vue和element UI联合技术栈的操控下,本专栏提供行之有效的源代码示例和信息点介绍,做到灵活运用。 (1)提供vue2的一些基本操作:安装、引用,模板使用,computed,watch,生命周期(beforeCreate,created,beforeMount,mounted, beforeUpdate,upda

    2024年02月10日
    浏览(19)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包