DataGrid DropDownList 相关处理函数

#region DataGrid 相关处理函数

  /// <summary>
  /// 根据绑定的列的名字,获得在DataGrid中的Index值
  /// </summary>
  /// <param name="dgcc">DataGridColumnCollection</param>
  /// <param name="FieldName">绑定列的字段名</param>
  /// <returns>Int >0 在DataGrid中的Index,-1:没有找到</returns>
  public static int DataGrid_FindColumnIndex(DataGridColumnCollection dgcc, string FieldName)

  {
   string temp;
   DataGridColumn column;
   BoundColumn boundColumn;
   int i;
   FieldName = FieldName.ToUpper();
   for(i=0;i<dgcc.Count;i++)
   {
    column = dgcc[i];
    if(column is BoundColumn)
    {
     boundColumn = (BoundColumn)column;
     temp = boundColumn.DataField.ToUpper();
     if(temp==FieldName)
      return i;
    }
   }
   return -1;
  }

  /// <summary>
  /// 根据绑定的列的名字,获得在DataGrid中的Index值
  /// </summary>
  /// <param name="dg">DataGrid</param>
  /// <param name="FieldName">绑定列的字段名</param>
  /// <returns>Int >0 在DataGrid中的Index,-1:没有找到</returns>
  public static int DataGrid_FindDataGridColumnIndex(DataGrid dg, string FieldName)
  {
   string temp;
   DataGridColumn column;
   BoundColumn boundColumn;
   int i;
   FieldName = FieldName.ToUpper();
   for(i=0;i<dg.Columns.Count;i++)
   {
    column = dg.Columns[i];
    if(column is BoundColumn)
    {
     boundColumn = (BoundColumn)column;
     temp = boundColumn.DataField.ToUpper();
     if(temp==FieldName)
      return i;
    }
   }
   return -1;
  }

  //当DataGrid中记录数发生改变时,避免越界
  /// <summary>
  /// 获得DataGrid的当前页数
  /// </summary>
  /// <param name="currentPageIndex">原来DataGrid的当前页码</param>
  /// <param name="pageSize">DataGrid每页显示的记录数</param>
  /// <param name="rowCount">DataGrid的记录数</param>
  /// <returns>返回处理后的页码</returns>
  public static int DataGrid_GetCurrentPageIndex(int currentPageIndex,int pageSize,int rowCount)
  {
   int pageIndex;
   if(pageSize==0) return 1;

   pageIndex=(rowCount-1)/pageSize;
   if(pageIndex < 0) return 0; //当rowCount=0时,会返回-1,页面出错
   if(currentPageIndex > pageIndex)
    return pageIndex;
   else
    return currentPageIndex;
  }
  /*
   * 使用示例代码
   *
   * /// <summary>
  /// 邦定列表
  /// </summary>
  private void BindDataSource()
  {                                               
   try
   {
    
    IDataGrid1.DataSource = DS;
    IDataGrid1.PageSize = 10;
    IDataGrid1.CurrentPageIndex = DataGrid_GetCurrentPageIndex(IDataGrid1.CurrentPageIndex,IDataGrid1.PageSize,DS.Tables[0].Rows.Count);
    IDataGrid1.DataBind();   
   }
   catch
   {;}
  }
  
  /// <summary>
  /// 分页操作
  /// </summary>
  /// <param name="source"></param>
  /// <param name="e"></param>
  private void IDataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
  {
   IDataGrid1.CurrentPageIndex = e.NewPageIndex;
   BindDataSource(); 
  }

  /// <summary>
  /// 选择操作
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void IDataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   int idIndex = DataGrid_FindColumnIndex(IDataGrid1.Columns,"ID");
   
   if(idIndex >= 0 )
   {
    Response.Redirect("Default.aspx?ID=" + IDataGrid1.SelectedItem.Cells[idIndex].Text,true);
   }
  }
   *
   * */
  #endregion

  #region DropDownList 相关处理函数

  /// <summary>
  /// 根据绑定的列的Value值,获得在DropDownList中的Index值
  /// </summary>
  /// <param name="lic">ListItemCollection</param>
  /// <param name="bandValue">绑定列的字段名</param>
  /// <returns>Int >0 在DropDownList中的Index,-1:没有找到</returns>
  public static int DropDownList_FindListItemIndex(ListItemCollection lic, string bandValue)
  {
   string temp;
   bandValue = bandValue.ToUpper();
   for(int i=0;i<lic.Count;i++)
   {
    temp = lic[i].Value.ToUpper();
    if(temp == bandValue)
    {
     return i;
    }
   }
   return -1;
  }

  /*
   * 使用示例
   *
   * /// <summary>
  /// 获取DropDownList列表SelectIndex
  /// </summary>
  /// <param name="listItemCol"></param>
  /// <param name="sValue"></param>
  /// <returns></returns>
  private int BindDropDownListSelectIndex(ListItemCollection listItemCol, string sValue)
  {
   int DDLSelectIndex = DropDownList_FindListItemIndex(listItemCol,sValue);
   if(DDLSelectIndex != -1)
   {
    return DDLSelectIndex;
   }
   else
   {
    return 0;
   }
  }
   *
   * */
  #endregion

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值