• C# DatrgridView表格控件的一些用法


    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[0];
            }
    
            #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 != 0)
                {
                    for (int i = 0; i < GridView.Rows.Count; )
                    {
                        GridView.Rows[i].DefaultCellStyle.BackColor = color;
                        i += 2;
                    }
                }
            }
    
            #region 关键字查找
            /// <summary>
            /// 关键字查找 ,并显示第一个查找到的,关将焦点转移到此
            /// </summary>
            /// <param name="SearchKey">查找的关键字</param>
            /// <returns>焦点的行</returns>
            public int SearchByKey(string SearchKey)
            {
                int j;
                for ( j = 0; j != GridView.Rows.Count - 1; j++)
                {
                    DataGridViewRow dvr = GridView.Rows[j];
                    for (int i = 0; i < GridView.ColumnCount; i++)
                    {
                        if (dvr.Cells[i].Value.ToString().StartsWith(SearchKey))
                        {
                            GridView.ClearSelection();
                            dvr.Selected = true;
                            GridView.CurrentCell = dvr.Cells[1];
                            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 = 0; j != GridView.Rows.Count - 1; j++)
                {
                    DataGridViewRow dvr = GridView.Rows[j];
                    for (int i = 0; i < GridView.ColumnCount; i++)
                    {
                        if (dvr.Cells[i].Value.ToString().StartsWith(SearchKey))
                        {
                            GridView.ClearSelection();
                            dvr.Selected = true;
                            GridView.CurrentCell = dvr.Cells[1];
                            break;
                        }
                    }
                }
                return j;
            }
            
            #endregion
    
    
           
        }

    调用 :

     udv.FrozenColumns("***");
                //奇偶行颜色
                udv.RowColorChange(System.Drawing.Color.Pink);
                //关键字查找
                udv.SearchByKey("**");
  • 相关阅读:
    关于大文本(txt)导入sqlserver2008数据库的一点感想
    欲则不达
    记录下马上两年的大学吧
    osgi使用 equnix框架 Bridge方式搭建方法(部分来源于ibm网站)
    mavan deploy不出repository.xml
    在项目中使用maven私服
    playbook 若干问题
    Maven 3 Felix 4 Eclipse 的搭建与部署(部分转载自别人文章)
    wallwd
    很好的面试记录
  • 原文地址:https://www.cnblogs.com/chcong/p/4315421.html
Copyright © 2020-2023  润新知