WinCE下DataGrid设置自动宽度

试了很多DataGridTableStyle的方式,但好像通过SQL语句生成DataTable没有效果,千辛万苦,在国外网站codeguru发现解决方案

    public void SizeColumnsToContent(DataGrid dataGrid, int nRowsToScan) 
    {
      // Create graphics object for measuring widths.
      Graphics Graphics = dataGrid.CreateGraphics();
    
      // Define new table style.
      DataGridTableStyle tableStyle = new DataGridTableStyle();
    
      try
      {
        DataTable dataTable = (DataTable)dataGrid.DataSource;
      
        if (-1 == nRowsToScan)
        {
          nRowsToScan = dataTable.Rows.Count;
        }
        else
        {
          // Can only scan rows if they exist.
          nRowsToScan = System.Math.Min(nRowsToScan, dataTable.Rows.Count);
        }
              
        // Clear any existing table styles.
        dataGrid.TableStyles.Clear();
      
        // Use mapping name that is defined in the data source.
        tableStyle.MappingName = dataTable.TableName;
      
        // Now create the column styles within the table style.
        DataGridTextBoxColumn columnStyle;
        int iWidth;
      
        for (int iCurrCol = 0; iCurrCol < dataTable.Columns.Count; iCurrCol++)
        {
          DataColumn dataColumn = dataTable.Columns[iCurrCol];
        
          columnStyle = new DataGridTextBoxColumn();

          columnStyle.TextBox.Enabled = true;
          columnStyle.HeaderText = dataColumn.ColumnName;
          columnStyle.MappingName = dataColumn.ColumnName;
        
          // Set width to header text width.
          iWidth = (int)(Graphics.MeasureString(columnStyle.HeaderText, dataGrid.Font).Width);

          // Change width, if data width is wider than header text width.
          // Check the width of the data in the first X rows.
          DataRow dataRow;
          for (int iRow = 0; iRow < nRowsToScan; iRow++)
          {
            dataRow = dataTable.Rows[iRow];
          
            if (null != dataRow[dataColumn.ColumnName])
            {
              int iColWidth = (int)(Graphics.MeasureString(dataRow.ItemArray[iCurrCol].ToString(), dataGrid.Font).Width);
              iWidth = (int)System.Math.Max(iWidth, iColWidth);
            }
          }
          columnStyle.Width = iWidth + 4;
        
          // Add the new column style to the table style.
          tableStyle.GridColumnStyles.Add(columnStyle);
        }
        // Add the new table style to the data grid.
        dataGrid.TableStyles.Add(tableStyle);
      }
      catch(Exception e)
      {
        MessageBox.Show(e.Message);
      }
      finally
      {
        Graphics.Dispose();
      }
    }

通过调用如下代码使用

    private void button1_Click(object sender, System.EventArgs e)
    {
      SizeColumnsToContent(this.dataGrid1, -1);
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,242评论 25 708
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,765评论 18 399
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,597评论 0 4
  • 最近一直在读《双向养育》,是浩途的一位妈妈推荐的,也是因为她分享了其中的一段话,让我毫不犹豫地买下了这本书,并放在...
    紫衔阅读 365评论 0 1
  • 17.1勾股定理(第1课时 一、内容和内容解析 1.内容 勾股定理的探究、证明及简单应用. 2.内客解析 勾股定理...
    84084552c8c3阅读 257评论 0 0