DataGridView

using System;
using System.Data;
using System.Data.OleDb;
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 DataGrid : System.Web.UI.Page
{
    private static string Ids="";
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!this.IsPostBack)
            BindData("select * from myTable");
    }

    private void BindData(string sql)
    {
        GridV.DataSource =GetDataSet(sql);
        GridV.DataBind();
    }

    protected void GridV_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridV.EditIndex = e.NewEditIndex;
        BindData("select * from myTable");

        SetBindedClientScript(e);
    }

    protected void GridV_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridV.EditIndex = -1;
        BindData("select * from myTable");
    }

    private void SetBindedClientScript(GridViewEditEventArgs e)
    {
        string js = @"<script language=javascript>";
        js += "<!-- function SetJs() {";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".focus();";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".size=10;";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".maxlength=10;";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[2].Controls[0].UniqueID + ".attachEvent('onclick',isOK);";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[3].Controls[0].UniqueID + ".size=10;";
        js += "};";
        js += "setTimeout(SetJs,1);";
        js += "//--></script>";

        if (!this.IsStartupScriptRegistered("clientjs"))
            this.RegisterStartupScript("clientjs", js);
    }

    protected void GridV_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton del=(LinkButton)e.Row.Cells[4].Controls[2];
            if(del.Text.ToString()=="删除")
               del.Attributes["onclick"] = "return confirm('确定要删除?')";
        }
    }

    protected void GridV_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string aId=GridV.DataKeys[e.RowIndex].Value.ToString();
        if (Ids == "")
            Ids += aId;
        else
            Ids += "," + aId;
        string sql = "select * from myTable where id not in(" + Ids.ToString()+")";

        BindData(sql);
    }

    /// <summary>
    ///得到DATASET
    /// </summary>
    /// <param name="sql">sql语句</param>
    /// <returns></returns>
    protected DataSet GetDataSet(string sql)
    {
        string StrCon = "";
        StrCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:/Web/FileSite/aSite/DB/aDb.mdb;Persist Security Info=False";
        OleDbConnection Conn = new OleDbConnection(StrCon);
        OleDbCommand Cmd = Conn.CreateCommand();
        Cmd.CommandText =sql;

        Conn.Open();
        //Cmd.ExecuteNonQuery();

        OleDbDataAdapter Adpt = new OleDbDataAdapter();
        Adpt.SelectCommand = Cmd;
        DataSet dst = new DataSet();
        Adpt.Fill(dst);
        Conn.Close();
        return dst;
    }

 }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值