public class useDatrgrivView
{
string conn = null;
string sqlComm = null;
DataSet das = null;
DataGridView GridView = null; //初始化,绑定
public useDatrgrivView(DataGridView gridView, string Sconn, string sql)
{
conn = Sconn;
GridView = gridView;
sqlComm = sql;
das = SqlHelper.ExecuteDataset(conn, CommandType.Text, sqlComm);
//绑定
GridView.DataSource = das.Tables[];
} #region 冻结列
/// <summary>
/// 冻结列
/// </summary>
/// <param name="ColumnName">列名</param>
public void FrozenColumns(string ColumnName)
{
GridView.Columns[ColumnName].Frozen = true;
}
/// <summary>
/// 冻结列
/// </summary>
/// <param name="grid">表格控件</param>
/// <param name="ColumnName">列名</param>
public void FrozenColumns(DataGridView grid, string ColumnName)
{
grid.Columns[ColumnName].Frozen = true;
}
#endregion /// <summary>
/// 表格奇偶行变换颜色,利用循环实现
/// </summary>
/// <param name="color">颜色</param>
public void RowColorChange(Color color)
{
if (GridView.Rows.Count != )
{
for (int i = ; i < GridView.Rows.Count; )
{
GridView.Rows[i].DefaultCellStyle.BackColor = color;
i += ;
}
}
} #region 关键字查找
/// <summary>
/// 关键字查找 ,并显示第一个查找到的,关将焦点转移到此
/// </summary>
/// <param name="SearchKey">查找的关键字</param>
/// <returns>焦点的行</returns>
public int SearchByKey(string SearchKey)
{
int j;
for ( j = ; j != GridView.Rows.Count - ; j++)
{
DataGridViewRow dvr = GridView.Rows[j];
for (int i = ; i < GridView.ColumnCount; i++)
{
if (dvr.Cells[i].Value.ToString().StartsWith(SearchKey))
{
GridView.ClearSelection();
dvr.Selected = true;
GridView.CurrentCell = dvr.Cells[];
break;
}
}
}
return j;
} /// <summary>
/// 关键字查找 ,并显示第一个查找到的,关将焦点转移到此
/// </summary>
/// <param name="SearchStar">从哪行开始查找</param>
/// <param name="SearchKey">关键字</param>
/// <returns>焦点的行</returns>
public int SearchByKeyNex(int SearchStar, string SearchKey)
{
int j = SearchStar;
for (j = ; j != GridView.Rows.Count - ; j++)
{
DataGridViewRow dvr = GridView.Rows[j];
for (int i = ; i < GridView.ColumnCount; i++)
{
if (dvr.Cells[i].Value.ToString().StartsWith(SearchKey))
{
GridView.ClearSelection();
dvr.Selected = true;
GridView.CurrentCell = dvr.Cells[];
break;
}
}
}
return j;
} #endregion }
调用 :
udv.FrozenColumns("***");
//奇偶行颜色
udv.RowColorChange(System.Drawing.Color.Pink);
//关键字查找
udv.SearchByKey("**");