DataGrid动态创建删除列,可以删除记录

private void Page_Load(object sender, System.EventArgs e)
  {
   //建立删除按钮列
   ButtonColumn bc = new ButtonColumn();
   bc.ButtonType = ButtonColumnType.PushButton;
   bc.Text = "删 除";
   bc.CommandName = "del";
   bc.HeaderText = "删 除";
   DataGrid1.Columns.Add(bc);
   LoadData();
  }

/// <summary>
  /// 加载数据,绑定到DataGrid
  /// </summary>                                                                                                                                                                   private void LoadData()
  {
   string strCmd = "";
   if(Session["sorting"] == null)
   {
    strCmd = "SELECT * FROM Jxt_DeviceManage_DeviceBasicData";
   }
   else
   {
    strCmd = "SELECT * FROM Jxt_DeviceManage_DeviceBasicData ORDER BY "+Session["sorting"].ToString()+" "+Session["sortDirection"].ToString();
   }
   conn = new SqlConnection(strConn);
   adapter = new SqlDataAdapter(strCmd,conn);
   ds = new DataSet();
   adapter.Fill(ds);
   DataTable dt = ds.Tables[0];
   DataColumn dataCol = new DataColumn("RowNumber",Type.GetType("System.Int16"));
   dt.Columns.Add(dataCol);
   for(int i=0;i<dt.Rows.Count;i++)
   {
    if(0 == i)
    {
     dt.Rows[i][dataCol] = 1;
    }
    else
    {
     dt.Rows[i][dataCol] = Convert.ToInt16(dt.Rows[i-1][dataCol])+1;
    }
   }
   DataView dv = new DataView(dt);
   DataGrid1.DataSource = dv;
   DataGrid1.DataKeyField = "DBID";
   this.DataGrid1.DataBind();
   if(this.DataGrid1.Items.Count < 1)
   {
    this.DataGrid1.Visible = false;
    Response.Write("没有任何设备");
   }
  }

/// <summary>
  /// 删除记录
  /// </summary>
  /// <param name="itemIndex">int 索引,获取DataGrid1.DataKeyField</param>
  private void DeleteRecord(int itemIndex)
  {
   conn = new SqlConnection(strConn);
   string strCmdBasic = "DELETE FROM Jxt_DeviceManage_DeviceBasicData WHERE DBID='"+DataGrid1.DataKeys[itemIndex]+"'";
   string strCmdExtra = "DELETE FROM Jxt_DeviceManage_DeviceExtraData WHERE DBID='"+DataGrid1.DataKeys[itemIndex]+"'";
   SqlCommand cmdBasic = new SqlCommand(strCmdBasic,conn);
   SqlCommand cmdExtra = new SqlCommand(strCmdExtra,conn);
   conn.Open();
   SqlTransaction myTrans = conn.BeginTransaction();
   cmdBasic.Transaction = myTrans;
   cmdExtra.Transaction = myTrans;
   try
   {
    cmdBasic.ExecuteNonQuery();
    cmdExtra.ExecuteNonQuery();
    myTrans.Commit();
   }
   catch(SqlException ex)
   {
    myTrans.Rollback();
    System.Diagnostics.Debug.Write(ex.ToString());
   }
   finally
   {
    cmdBasic.Dispose();
    cmdExtra.Dispose();
    conn.Close();
    conn.Dispose();
   }
  }

/// <summary>
  /// 调用删除函数,删除记录
  /// </summary>
  /// <param name="source"></param>
  /// <param name="e"></param>
  private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   if(e.CommandName=="del")
   {
    DeleteRecord(e.Item.ItemIndex);
    LoadData();
   }
  }

 

 

/// <summary>
  /// 这是一个关于动态添加的函数
  /// </summary>
  /// <param name="DataFieldValue">string DataField</param>
  /// <param name="HeaderTextValue">string Header Text</param>
  /// <returns>BoundColumn</returns>
  BoundColumn   CreateBoundColumn(String DataFieldValue,String HeaderTextValue)
  {
   //   This version of the CreateBoundColumn method sets only the DataField and HeaderText properties.Create a BoundColumn.
   BoundColumn column = new BoundColumn();

   //   Set   the   properties   of   the   BoundColumn.
   column.DataField = DataFieldValue;
   column.HeaderText = HeaderTextValue;
   //   column.ItemStyle.Width = "width:";
   return column;
  }

阅读更多
个人分类: ASP.NET
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

DataGrid动态创建删除列,可以删除记录

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭