手工创建datagrid数据列/模板列/按钮事件+简单的数据操作类(asp.net)[2]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

   conn.Close();
   }
  }

  public static DataSet getDataSet(string strConnection,string strSql)
  {
   if (strConnection==null || strConnection.Length==0)
   {
    throw new ArgumentNullException( "strConnection" );
   }
   if (strSql==null || strSql.Length==0)
   {
    throw new ArgumentNullException( "strSql" );
   }
   using(SqlConnection conn=new SqlConnection(strConnection))
   {
    DataSet ds=new DataSet();
    conn.Open();
    SqlDataAdapter da =new SqlDataAdapter(strSql,conn);
    da.Fill(ds);
    conn.Close();
    return ds;
   }
  }
  public static DataSet getDataSet(SqlConnection conn,string strSql)
  {
   if (conn==null)
   {
    throw new ArgumentNullException( "SqlConnection" );
   }
   if (strSql==null || strSql.Length==0)
   {
    throw new ArgumentNullException( "strSql" );
   }
   using(SqlDataAdapter da =new SqlDataAdapter(strSql,conn))
   {
    DataSet ds=new DataSet();
    da.Fill(ds);
    conn.Close();
    return ds;
   }
  }
  public static int executeNonQuery(string strConnection,string strSql)
  {
   if (strConnection==null || strConnection.Length==0)
   {
    throw new ArgumentNullException( "strConnection" );
   }
   if (strSql==null || strSql.Length==0)
   {
    throw new ArgumentNullException( "strSql" );
   }
   using(SqlConnection conn=new SqlConnection(strConnection))
   {
    SqlCommand sqlCmd=new SqlCommand(strSql,conn);
    int i= sqlCmd.ExecuteNonQuery();
    conn.Close();
    return i;
   }
  }
  public static int executeNonQuery(SqlConnection conn,string strSql)
  {
   if (conn==null)
   {
    throw new ArgumentNullException( "SqlConnection" );
   }
   if (strSql==null || strSql.Length==0)
   {
    throw new ArgumentNullException( "strSql" );
   }
   using(SqlCommand sqlCmd=new SqlCommand(strSql,conn))
   {
    int i=sqlCmd.ExecuteNonQuery();
    conn.Close();
    return i;
   }
  }
 }
}

3)创建模板列的类(可以创建n种模板列)

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;

using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


namespace Webtest
{
 //DropDownList模板列
 public class DDListCol : ITemplate
 {
  string ID;
  public DDListCol(string id)
  {
   this.ID=id;
  }
  public void InstantiateIn(Control container)      
  {
   DropDownList dpl = new DropDownList();
   dpl.ID=this.ID ;
   container.Controls.Add(dpl);

  }
 }
 //CheckBox模板列
 public class CheckBoxCol : ITemplate
 {
  string ID;
  public CheckBoxCol(string id)
  {
   this.ID=id;
  }
  public void InstantiateIn(Control container)      
  {
   CheckBox checkbox = new CheckBox();
   checkbox.ID=this.ID ;
   container.Controls.Add(checkbox);
  }
 }
}

本文关键:手工创建datagrid数据列/模板列/按钮事件+简单的数据操作类(asp.net)
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top