您的位置:首页 > 运维架构

DataGrid DropDownList 相关处理函数

2006-04-21 11:11 375 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: