silverlight 后台代码生成gridview

        /// <summary>
        /// 后台代码生成gridview
        /// </summary>
        /// <param name="columnList">依次存 name txt width 3项的数组,分别是field,标题,宽度(宽度为0则自己计算宽度)</param>
        /// <param name="dataGridName">生成的datagrid的id</param>
        /// <returns></returns>
        private DataGrid CreateDataGrid(List<string[]> columnList, int byteWidth)
        {

            //建立datagrid 并设置datagrid属性
            DataGrid dgrid = new DataGrid();
            dgrid.HorizontalAlignment = HorizontalAlignment.Left;
            dgrid.VerticalAlignment = VerticalAlignment.Top;
            dgrid.AutoGenerateColumns = false;
            dgrid.Margin = new Thickness(5,5,5,5);
            dgrid.Width = 900;
            dgrid.Height = 340;
            dgrid.IsReadOnly = false;

            dgrid.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
            dgrid.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;



            //获取所有的Header字符
            StringBuilder strHeadersNoWidth = new StringBuilder();
            StringBuilder strHeadersWithWidth = new StringBuilder();
            int WidthSet = 0;
            foreach (string[] col in columnList)
            {

                //第三项为宽度,如果宽度==0那么程序自己搞,如果宽度>0那么按照传入的进行
                if (col[2].ToString() == "0" || col[2].ToString().Trim() == "")
                {
                    strHeadersNoWidth.Append(col[1]);
                }
                else
                {
                    WidthSet = WidthSet + int.Parse(col[2]);
                }
            }

            //计算所有的Header字符总长需要占用多少像素,然后用DataGrid的宽度减去这个长度,得到的结果除以总的DataGrid列数即为每列可以获得的列数宽度的增量。

            double Sub = dgrid.Width - WidthSet - StrLength(strHeadersNoWidth.ToString()) * byteWidth -5;
            

            if (Sub<0) { Sub = 0;  }

            double extendWidth = Sub / columnList.Count;

            try
            {

                DataGridCheckBoxColumn checkCol = new DataGridCheckBoxColumn();
                //checkCol.Header = new CheckBox();
                checkCol.Width = new DataGridLength(5);
                dgrid.Columns.Add(checkCol);
            }
            catch (Exception ex)
            {
                
                throw ex;
            }



            //循环添加模板列
            foreach (string[] col in columnList)
            {
                //如果设置的宽度是0或者没有设置,那么则通过计算获得
                if (col[2] == "0" || col[2].Trim() == "")
                {

                    dgrid.Columns.Add(CreateDataGridTextColumn(col[0], col[1], extendWidth + StrLength(col[1]) * byteWidth));
                }
                else
                {
                    dgrid.Columns.Add(CreateDataGridTextColumn(col[0], col[1], StrLength(col[1]) * byteWidth));
                }

            }

            return dgrid;

        }


        /// 创建DataGridTextColumn模板列
        ///</summary>
        ///<param name="columnBindName">需要绑定的字段名</param>
        ///<param name="columnHeaderName">模板列的Header</param>
        ///<param name="width">模板列的宽度</param>
        ///<returns></returns>
        private DataGridTextColumn CreateDataGridTextColumn(string columnBindName, string columnHeaderName, double width)
        {
            DataGridTextColumn dgtextColumn = new DataGridTextColumn();
            dgtextColumn.Binding = new Binding(columnBindName.Trim());
            dgtextColumn.Header = columnHeaderName.Trim();
            dgtextColumn.IsReadOnly = false;
            dgtextColumn.Width = new DataGridLength(width);
            return dgtextColumn;
        }


        /// 字符串长度(按字节算)
        ///</summary>
        ///<param name="str">输入一个列名</param>
        ///<param name="byteWith">设定的一个字节的宽度(假设为6)</param>
        ///<returns>返回一个列字节数</returns>
        private static int StrLength(string str)
        {
            int len = 0;
            byte[] b;

            for (int i = 0; i < str.Length; i++)
            {
                b = Encoding.UTF8.GetBytes(str.Substring(i, 1));
                if (b.Length > 1)
                    len += 2;
                else
                    len++;
            }

            return len;
        }

        

 

posted on 2013-11-04 11:34  中子持心  阅读(310)  评论(0编辑  收藏  举报

导航