GridView 自定义简单操作

来源:互联网 发布:java多线程书籍推荐 编辑:程序博客网 时间:2024/05/18 03:31
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class SY16 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (Page.IsPostBack)
            
return;
        DataBind();

    }

    
protected void GridView_pubs_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{
        
int SelectedIndex = e.RowIndex;
        
string pub_id = GridView_pubs.Rows[SelectedIndex].Cells[1].Text;
        BLL.AccessDataBase db 
= new BLL.AccessDataBase();
        db.Delete(pub_id);
        DataBind();
        Response.Write(SelectedIndex.ToString() 
+ pub_id);

    }

    
protected void Button_query_Click(object sender, EventArgs e)
    
{
        BLL.AccessDataBase db 
= new BLL.AccessDataBase();
        DataSet ds 
= db.Query(TextBox_query.Text.ToString());
        GridView_pubs.DataSource 
= ds;
        GridView_pubs.DataBind();
    }

    
protected void GridView_pubs_RowEditing(object sender, GridViewEditEventArgs e)
    
{
        GridView_pubs.EditIndex 
= e.NewEditIndex;
        DataBind();

    }

    
protected void GridView_pubs_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
{
        
int EditIndex = e.RowIndex;
        Response.Write(EditIndex.ToString());
        
string pub_id = ((TextBox)GridView_pubs.Rows[EditIndex].Cells[1].Controls[0]).Text;
        
string pub_name = ((TextBox)GridView_pubs.Rows[EditIndex].Cells[2].Controls[0]).Text;
        
string city = ((TextBox)GridView_pubs.Rows[EditIndex].Cells[3].Controls[0]).Text;
        
string state = ((TextBox)GridView_pubs.Rows[EditIndex].Cells[4].Controls[0]).Text;
        
string country = ((TextBox)GridView_pubs.Rows[EditIndex].Cells[5].Controls[0]).Text;
        BLL.AccessDataBase db 
= new BLL.AccessDataBase();
        db.Update(pub_id, pub_name, city, state, country);
        GridView_pubs.EditIndex 
= -1;
        DataBind();
    }

    
protected void GridView_pubs_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    
{
        GridView_pubs.EditIndex 
= -1;
        DataBind();
    }

    
private void DataBind()
    
{
        BLL.AccessDataBase db 
= new BLL.AccessDataBase();
        DataSet ds 
= db.ShowData();
        GridView_pubs.DataSource 
= ds;
        GridView_pubs.DataBind();
    }

}

原创粉丝点击