C# ListView 編寫 ListView 實體類

OK,因爲Android 上有ListView 。後來查了查文檔,果真在C#中有ListView,這種自定義的控件。c#

因爲C#上面能夠拖動組件到面板上,但這種感受不怎麼好。工具

那麼今天就繼承這個 來編寫一個工具類。this


class MyListView : ListView
    {

        /// <summary>
        /// 增長列名
        /// </summary>
        /// <param name="names">列名集合</param>
        /// <returns>成功 true  失敗 false</returns>
        public Boolean addColumns(params string[] names)
        {
            try
            {
                if (this.Columns.Count == 0) {
                    for (int i = 0; i < names.Length;i++ )
                    {
                        this.Columns.Add(names[i]);
                    }
                    return true;
                }
                return false;
            }
            catch (Exception e) {
                //e.Message();
                return false;
            }
        }
        /// <summary>
        /// 追加列
        /// </summary>
        /// <param name="colName">列名</param>
        /// <returns>成功 true 失敗 false</returns>
        public Boolean addCol(string colName) {
            try
            {
                this.Columns.Add(colName);
                return true;
            }
            catch (Exception e)
            {
                //e.Message();
                return false;
            }
        }
        /// <summary>
        /// 刪除全部數據
        /// </summary>
        /// <returns>成功 true 失敗 false</returns>
        public Boolean removeAll() {
            try
            {
                this.Clear();
                return true;
            }
            catch (Exception e) {
                return false;    
            }

        }
        /// <summary>
        /// 刪除列
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public Boolean removeCol(int index) {
            try
            {
                this.Columns.RemoveAt(index);
                return true;
            }
            catch (ArgumentOutOfRangeException e) {
                return false;
            }
        }
        /// <summary>
        /// 刪除列
        /// </summary>
        /// <param name="colName"></param>
        /// <returns></returns>
        public Boolean removeCol(String colName)
        {
            try
            {
                this.Columns.RemoveByKey(colName);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        /// <summary>
        /// 刪除行
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public Boolean removeRow(int index) {
            try
            {
                this.Items.RemoveAt(index);
                return true;
            }
            catch (ArgumentOutOfRangeException e)
            {
                return false;
            }    
        }
        /// <summary>
        /// 刪除行
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Boolean removeRow(String key)
        {
            try
            {
                this.Items.RemoveByKey(key);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        /// <summary>
        /// 在最後一行追加一列
        /// </summary>
        /// <param name="rows">數據集</param>
        /// <returns>成功 true  失敗 false</returns>
        public Boolean addRows(params string[] rows)
        {
            try
            {
                this.Items.Add(rows[0]);
                for (int i = 1; i < rows.Length; i++)
                {
                    this.Items[this.Items.Count - 1].SubItems.Add(rows[i]);
                }
                return true;
            }
            catch (Exception e) {
                return false;
            }
        }
        /// <summary>
        /// 在最後一行追加一列
        /// </summary>
        /// <param name="rows">數據集</param>
        /// <returns>成功 true  失敗 false</returns>
        public Boolean addRows(int index,params string[] rows)
        {
            try
            {
                ListViewItem item = new ListViewItem();
                for(int i = 0 ; i < rows.Length ;i ++){
                    item.SubItems.Add(rows[i]);
                }
                this.Items.Insert(index,item);
                return true;
            }
            catch (ArgumentOutOfRangeException e)
            {
                return false;
            }
        }
        /// <summary>
        /// 在指定行追加內容
        /// </summary>
        /// <param name="rows">數據集</param>
        /// <returns>成功 true  失敗 false</returns>
        public Boolean addRow(int rowIndex, params string[] rows)
        {
            try
            {
                for (int i = 0; i < rows.Length;i++ )
                {
                    this.Items[rowIndex].SubItems.Add(rows[i]);
                }
                return true;
            }
            catch (ArgumentOutOfRangeException e)
            {
                return false;
            }
        }
        /// <summary>
        /// 返回有多少行
        /// </summary>
        /// <returns></returns>
        public int rowSize() {
            return this.Items.Count;
        }

        /// <summary>
        /// 返回有多少列
        /// </summary>
        /// <returns></returns>
        public int colSize() {
            return this.Columns.Count;
        }
}

這樣作能夠爲後期開發作準備~~~

code

相關文章
相關標籤/搜索