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

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

接下来是gridview的使用
GridView的绑定,添加,删除,更改;_第2张图片
GridView的绑定,添加,删除,更改;_第3张图片

GridView的绑定,添加,删除,更改;_第4张图片
GridView的绑定,添加,删除,更改;_第5张图片
前台代码如下

<%@ 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();
        }
    }
    /// 
    /// GridView的数据绑定;
    /// 
      //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("");
           
        }
        else
        {
            Response.Write("");
            
        }
    }

  



    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("");
            gvStudent.EditIndex = -1;
        }
        else
        {
            Response.Write("");
        }
    }

    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("");
        }
        else
        {
            Response.Write("");
        }

    }

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

    protected void gvStudent_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

结果如下
GridView的绑定,添加,删除,更改;_第6张图片

你可能感兴趣的:(c#,asp.net,ui,数据库,c#)