datagrid的精妙使用

本文通过一个具体的示例展示了如何使用 ASP.NET 中的 DataGrid 控件进行数据展示、分页、选择、编辑、取消和删除操作。具体实现包括连接数据库、填充数据集以及执行 SQL 语句等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文链接:http://user.qzone.qq.com/361983679/blog/1211387184  datagrid的精妙使

   大家来看看我做的这个例子可能对你们有点帮助的。 在.aspx.cs下的代码 ­
using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    /*
     * 2008年4月11在宿舍
     * 作者:苏飞  06北大青鸟
     * */
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }     
    }
    private  void BindGrid()//相当于一个重写的DataBind()方法
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Student;Integrated Security=True");
        con.Open();
        SqlDataAdapter objDataAdapter = new SqlDataAdapter("select * from StudentInfo",con);
        DataSet studentDataSet = new DataSet();
        objDataAdapter.Fill(studentDataSet ,"StudentInfo");
        this.DataGrid1.DataSource = studentDataSet.Tables["StudentInfo"].DefaultView;
        con.Close();
        this.DataGrid1.DataBind();   
    }
    private void ExecuteSql(string strSql)//说明:执行传递过来的SQL语句
    {
        try
        {
            //this.Label1.Text = "sufei";
           SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Student;Integrated Security=True");
           con.Open();
           SqlDataAdapter objdataAdapter=new SqlDataAdapter (strSql, con);
           DataSet datset = new DataSet();
           objdataAdapter.Fill(datset ,"StudentInfo");
           con.Close();
        }
        catch (Exception e)
        {
            Response.Write("<script language = 'javascript'>alert('" + e.Message + "');</script>");
        }
    }
    protected void DataGrid1_PageIndexChanged1(object source, DataGridPageChangedEventArgs e)
    {
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        BindGrid();   
    }
    protected void DataGrid1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.Label1.Text ="您选择了" +this.DataGrid1.SelectedItem.Cells[1].Text.ToString();  
    }
    protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
    {
        this.DataGrid1.EditItemIndex = e.Item.ItemIndex;
        BindGrid();
    }
    protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
    {
        this.DataGrid1.EditItemIndex = -1;
        BindGrid();
    }
    protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
    {
        if (DataGrid1.Items .Count == 1)//当本页只有一行数据时,让它没有分页。因为删除后就没有数据了,而页还在则会出错
        {
            if (DataGrid1.CurrentPageIndex != 0)
            {
                DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex - 1;
            }     
        }
        string mysql = "DELETE FROM StudentInfo where ID='" + e.Item.Cells[0].Text.ToString() + "'";//处于非编辑状态
        //执行这个方法用来执行SQL语句
        ExecuteSql(mysql);
        BindGrid();
        if (DataGrid1.Items.Count == 1)
        {
            if (DataGrid1.CurrentPageIndex != 0)
            {
                DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex - 1;
            }
        }
    }
    protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
    {
        string ID = e.Item.Cells[0].Text;//处于非编辑状态
        string strName = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;//处于编辑状态
        string strAge = ((TextBox)(e.Item.Cells[2].Controls[0])).Text;
        string strSex = ((TextBox)(e.Item.Cells[3].Controls[0])).Text;
        string strClass = ((TextBox)(e.Item.Cells[4].Controls[0])).Text;
        string strSql = "update StudentInfo set Name='" + strName + "',Age=" + strAge ;
        strSql += ",Sex='" + strSex + "',Class='" + strClass+ "' where ID='" +ID + "'";
        ExecuteSql(strSql);
        DataGrid1 . EditItemIndex = -1;
        BindGrid();
       //((TextBox)(e.Item .Cells [1].Controls [0])).Text .ToString ();//为用户在编辑时显示一个文本框
    }
    protected void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case  ListItemType .Item :
            case ListItemType .EditItem :
            case ListItemType .AlternatingItem:
                Button mydeletebutten = (Button)e.Item.FindControl("btndelete");
                //mydeletebutten.Text = "自选删除";
                mydeletebutten.Attributes.Add("onclick","return confirm('你确定要删除第"+e.Item .ItemIndex.ToString () +"行吗?')");
                break;         
        }
    }
    protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        if (e.CommandName == "userDelete")
        {
          DataGrid1_DeleteCommand(source,e);
        }
    } 
}

sql数据库 ­ ­ ­ ­

­

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值