首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

GridView单元格合并有关问题 异常提示:必须在顶级静态类中定义扩展方法;GridViewExtensions 为嵌套类

2013-04-02 
GridView单元格合并问题 错误提示:必须在顶级静态类中定义扩展方法;GridViewExtensions 为嵌套类 public s

GridView单元格合并问题 错误提示:必须在顶级静态类中定义扩展方法;GridViewExtensions 为嵌套类
 public static class GridViewExtensions
    {
public static GridView RowSpan(this GridView gridView, object field)
        {
Dictionary<string, string> rowDictionary = ObjectLoadDictionary(field);
            int columnIndex = int.Parse(rowDictionary["ColumnIndex"]);
            string columnName = rowDictionary["ColumnControlID"];
            string propertyName = rowDictionary["PropertyName"];
            string columns = rowDictionary["Columns"];
            for (var i = 0; i < gridView.Rows.Count; i++)
            {

                int rowSpanCount = 1;
                for (int j = i + 1; j < gridView.Rows.Count; j++)
                {
                    //绑定行合并处理
                    if (string.IsNullOrEmpty(columnName))
                    {
                        //比较2行的值是否相同
                        if (gridView.Rows[i].Cells[columnIndex].Text == gridView.Rows[j].Cells[columnIndex].Text)
                        {
                            //合并行的数量+1
                            rowSpanCount++;
                            //隐藏相同的行
                            gridView.Rows[j].Cells[columnIndex].Visible = false;
                            if (!string.IsNullOrEmpty(columns))
                            {
                                columns.Split(',').ToList<string>().ForEach(c => gridView.Rows[j].Cells[int.Parse(c)].Visible = false);


                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        //模板行的合并处理
                        if (GetPropertyValue(gridView.Rows[i].Cells[columnIndex].FindControl(columnName), propertyName).ToString() == GetPropertyValue(gridView.Rows[j].Cells[columnIndex].FindControl(columnName), propertyName).ToString())
                        {
                            rowSpanCount++;
                            //隐藏相同的行
                            gridView.Rows[j].Cells[columnIndex].Visible = false;
                            if (!string.IsNullOrEmpty(columns))
                            {

                                columns.Split(',').ToList<string>().ForEach(c => gridView.Rows[j].Cells[int.Parse(c)].Visible = false);
                            }
                        }
                        else
                        {
                            break;


                        }
                    }
                }
                if (rowSpanCount > 1)
                {
                    //行合并
                    gridView.Rows[i].Cells[columnIndex].RowSpan = rowSpanCount;
                    //判断是否有额外的行需要合并
                    if (!string.IsNullOrEmpty(columns))
                    {
                        //额外的行合并
                        columns.Split(',').ToList<string>().ForEach(c => gridView.Rows[i].Cells[int.Parse(c)].RowSpan = rowSpanCount);
                    }
                    i = i + rowSpanCount - 1;
                }


            }
            return gridView;
        }

        private static Dictionary<string, string> ObjectLoadDictionary(object fields)
        {
            Dictionary<string, string> resultDictionary = new Dictionary<string, string>();
            PropertyInfo[] property = fields.GetType().GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.GetProperty);
            foreach (PropertyInfo tempProperty in property)
            {
                resultDictionary.Add(tempProperty.Name, tempProperty.GetValue(fields, null).ToString());
            }
            //指定默认值
            if (!resultDictionary.Keys.Contains("ColumnIndex"))
            {
                throw new Exception("未指定要合并行的索引 ColumnIndex 属性!");


            }
            if (!resultDictionary.Keys.Contains("ColumnControlID"))
            {
                resultDictionary.Add("ColumnControlID", null);
            }

            if (!resultDictionary.Keys.Contains("PropertyName"))
            {
                resultDictionary.Add("PropertyName", "Text");
            }

            if (!resultDictionary.Keys.Contains("Columns"))
            {
                resultDictionary.Add("Columns", null);
            }




            return resultDictionary;
        }

        /// <summary>
        ///  获取一个对象的一个属性..
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="PropertyName">属性名称</param>
        /// <returns>属性的值,  如果无法获取则返回null</returns>
        private static object GetPropertyValue(object obj, string PropertyName)
        {
            PropertyInfo property = obj.GetType().GetProperty(PropertyName);
            return property.GetValue(obj, null);
        }
    }
[解决办法]
编译器说你需要把类单独拿出来,你这个类GridViewExtensions应该包含在另外一个类中了。

热点排行