DataGrid DropDownList 相關處理函數

來源:互聯網
上載者:User

#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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.