GridView控件的基本应用

.net这一门技术是我们常用的一种技术,其中的很多技术我们都经常用到,比如一些常用的控件,我们就经常的去使用,我在这里向晒一种控件的用法,GridView控件,是以表格的形式来显示数据源中的数据。

     GridView控件常用的方法DataBind是将数据源绑定到控件中。我觉得GridView控件在某些方面还是很好用的,而且也很常用,

下面晒了一点我写的小代码,和一些用法。

   这个程序是一个简单的功能实现,也就是修改和删除,查询详细信息的功能,我觉得这个控件的一些方法很好用,比如说这些简单的功能实现,在这过程中我可以知道怎么绑定,调用一些方法进行操作。

    通过对这个控件的学习让我可以更加熟练的去运用常用的事件,对于刚刚上手的人来说是个不错的例子

 

    protected void Page_Load(object sender, EventArgs e)

    {

        Bind();//调用已经写好的Bind方法

    }

    public SqlConnection GetConnetion()   //获取配置节的链接符,注意要连接数据源

    {

        string myStr = ConfigurationManager.ConnectionStrings["PXSCJConnectionString"].ToString();//获取wed里面链接名

        SqlConnection myConn = new SqlConnection(myStr);

        return myConn;

    }

    public void Bind()//定义一个自定义方法Bind,读取数据库中的信息,并绑定到GridView中去。

    {

        SqlConnection myConn = GetConnetion();//调用链接符

        myConn.Open();

        string str = "select * from aa ";//查询数据库

        SqlCommand myComm = new SqlCommand(str, myConn);

        SqlDataAdapter myDa = new SqlDataAdapter(str, myConn);

        DataSet myDs = new DataSet();

        myDa.Fill(myDs);

        GridView1.DataSource = myDs;

        GridView1.DataKeyNames = new string[] { "id" };//指定控件绑定的主键字段

        GridView1.DataBind();

        myDa.Dispose();

        myDs.Dispose();

        myConn.Close();

    }

    protected void Button1_Click(object sender, EventArgs e)//当点击时引发的事件

    {

        SqlConnection myConn = GetConnetion();

        myConn.Open();

 

        string username = TextBox1.Text.Trim();

        string sqlstr = " select * from aa where username='" + username + "'";//根据用户名对数据库进行查询

 

        SqlCommand myCmd = new SqlCommand(sqlstr, myConn);

 

        SqlDataAdapter myDa = new SqlDataAdapter(myCmd);

 

        DataSet myDs = new DataSet();

 

        myDa.Fill(myDs);

 

        if (myDs.Tables[0].Rows.Count > 0)

        {

            GridView1.DataSource = myDs;

            GridView1.DataBind();

        }

        else

        {

            Response.Write("<script>alert('没有相关记录')</script>");

        }

        myDa.Dispose();

        myDs.Dispose();

        myConn.Close();

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

        GridView1.PageIndex = e.NewPageIndex;

        Bind();

    }

protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)

//当点击详细信息按钮时,引发的事件

{

//获取选择行的系编号

        string id = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();

        string sqlstr = "select * from aa where id='" + id + "'";

        SqlConnection con = GetConnetion();

 

        SqlCommand mycmd = new SqlCommand(sqlstr, con);

        con.Open();

        SqlDataAdapter myDa = new SqlDataAdapter(mycmd);

        DataSet myDs = new DataSet();

 

        myDa.Fill(myDs);

 

        if (myDs.Tables[0].Rows.Count > 0)

        {

            GridView2.DataSource = myDs;//跳转到另一个页面显示

            GridView2.DataBind();//绑定数据源

        }

        myDa.Dispose();

        myDs.Dispose();

        con.Close();

    }

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

//当点击修改按钮时引发的事件

{

//获取选择行的id

        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());

//获取用户名

        string username = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();

        string sqlStr = "update aa set username='" + username + "' where id=" + id;//根据id进行数据库的操作

        SqlConnection myConn = GetConnetion();

        myConn.Open();

        SqlCommand myCmd = new SqlCommand(sqlStr, myConn);

        myCmd.ExecuteNonQuery();

        myCmd.Dispose();

        myConn.Close();

        GridView1.EditIndex = -1;

        this.Bind();

    }

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

//当点击分页时进行的操作

    {

        GridView1.EditIndex = -1;

        this.Bind();//调用Bind方法

    }

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

//当点击删除时引发的事件

    {

        {

//获取id

            int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());

            string str = "delete from aa where id=" + id;//根据id对数据库的数据进行操作

            SqlConnection myconn = GetConnetion();

            myconn.Open();

            SqlCommand mycmd = new SqlCommand(str, myconn);

            mycmd.ExecuteNonQuery();

            mycmd.Dispose();

            myconn.Close();

            GridView1.EditIndex = -1;

            Bind();

        }

    }

 

 

posted @ 2012-12-01 17:54  lgfalmh  阅读(155)  评论(0)    收藏  举报