【问题标题】:Adjust form height according to total row count in datagridview?根据datagridview中的总行数调整表单高度?
【发布时间】:2014-06-05 17:18:44
【问题描述】:
void PopulateApplications()
{
    dataGridView1.Rows.Clear();
    foreach (Process p in Process.GetProcesses())
    {
        if (p.MainWindowTitle.Length > 1)
        {
            var icon = Icon.ExtractAssociatedIcon(p.MainModule.FileName);
            ima = icon.ToBitmap();
            ima = resizeImage(ima, new Size(25, 25));
            ima.Save(@"c:\temp\ima.jpg");
            String status = p.Responding ? "Running" : "Not Responding";
            dataGridView1.Rows.Add(ima, p.ProcessName, status);
        }
    }
    int rc = dataGridView1.Rows.Count * ROW_SIZE;
}

在这种情况下,在第一次迭代后添加了 10 行,我已经知道每行高度为 22。 所以 rc = 220。

现在在设计器中,form1 的大小为:648、332 我之前手动检查过,648、332 适合行高,但假设我在设计器中更改了 form1 的大小并从:648、200 开始,有 5 行或 20 行。所以我想计算所有行的高度并设置form1的大小以适应所有的行。

导致每个用户在运行程序时同时运行其他数量的进程。所以不是每个人都适合648,332是form1的好尺寸。

【问题讨论】:

    标签: c# .net winforms datagridview


    【解决方案1】:

    这可能会解决您的问题。

    void PopulateApplications()
    {
        dataGridView1.Rows.Clear();
        foreach (Process p in Process.GetProcesses())
        {
            if (p.MainWindowTitle.Length > 1)
            {
                var icon = Icon.ExtractAssociatedIcon(p.MainModule.FileName);
                ima = icon.ToBitmap();
                ima = resizeImage(ima, new Size(25, 25));
                ima.Save(@"c:\temp\ima.jpg");
                String status = p.Responding ? "Running" : "Not Responding";
                dataGridView1.Rows.Add(ima, p.ProcessName, status);
            }
        }
    
        int totalRowSize = dataGridView1.Rows.Count * 22;
        int formHeight = this.Size.Height;
    
        if (totalRowSize > formHeight-30)
            this.Height = totalRowSize + 30;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-22
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多