【问题标题】:How to adjust width of columns created with "xy.Columns.Add"?如何调整使用“xy.Columns.Add”创建的列的宽度?
【发布时间】:2014-03-18 17:02:08
【问题描述】:

我的表单的固定大小为 1024x600(dataGridView 的宽度为 1022)。 当我尝试使用此代码读取 XML 时,每个 Column 具有相同的宽度,并且第 10、11、12 和 13 列甚至没有显示,因为它们在外面。

private void btLoadXML_Click(object sender, EventArgs e)
    {
    DataTable dt = new DataTable("itemstable");

    dt.Columns.Add("Datum", typeof(System.String));
    dt.Columns.Add("1", typeof(System.String));
    dt.Columns.Add("2", typeof(System.String));
    dt.Columns.Add("3", typeof(System.String));
    dt.Columns.Add("4", typeof(System.String));
    dt.Columns.Add("5", typeof(System.String));
    dt.Columns.Add("6", typeof(System.String));
    dt.Columns.Add("7", typeof(System.String));
    dt.Columns.Add("8", typeof(System.String));
    dt.Columns.Add("9", typeof(System.String));
    dt.Columns.Add("10", typeof(System.String));
    dt.Columns.Add("11", typeof(System.String));
    dt.Columns.Add("12", typeof(System.String));
    dt.Columns.Add("13", typeof(System.String));

    //Read XML File And Display Data in GridView
    dt.ReadXml("items2.xml");
    dataGridView1.DataSource = dt;
    }

如何手动调整每列的宽度,让所有内容都适合?

【问题讨论】:

  • My Form has a fixed size of 1024x600 - 当您的最终用户尝试以 2560 x 1600 的分辨率运行您的应用程序时,我想看看他们的面孔

标签: c# datagridview width readxml


【解决方案1】:

试试这个:

dataGridView1.Columns[0].Width = 200;

【讨论】:

  • 请记住,“0”是列号,“200”是该列的固定宽度。您可以将 200 更改为适合您的任何内容。
【解决方案2】:

您应该像这样在 dataGridview 中设置它们的宽度:

dataGridView1.Columns[index].Width = /*insert number here*/;

通常,我喜欢将 dataTable 和 dataGridView 分开,前者更多用于数据操作,后者用于表格渲染。

【讨论】:

    【解决方案3】:

    使用Grid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

    对于自动创建的网格,我可以强制列从正确的宽度开始而不响应拥有控件中的事件的唯一方法是:

    public static void InitGrid(DataGridView Grid) {
        Grid.HandleCreated+=new System.EventHandler(DoResizeColumnsEvent);
    }
    
    static void DoResizeColumnsEvent(object sender,EventArgs e) {
                ((DataGridView)sender).AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);         
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 2016-01-31
      • 2017-07-04
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 2010-10-22
      相关资源
      最近更新 更多