【问题标题】:Setting column width in Data grid view for my table在我的表格的数据网格视图中设置列宽
【发布时间】:2015-01-12 08:37:06
【问题描述】:

当我尝试从 datagridview 中的表中获取特定列时出现错误。

这是我填充表格的方式----

public DataTable createGridForForm(int rows, int columns)
        {

            // Create the output table.
            DataTable table = new DataTable();


                   for (int i = 1; i <= columns; i++)
            {
                table.Columns.Add("column " + i.ToString());
            }


            for (int i = 1; i < rows; i++)
            {
                DataRow dr = table.NewRow();
                // populate data row with values here
                table.Rows.Add(dr);
            }    

            return table;
        }

这就是我创建数据网格视图的方式------

private void createGridInForm(int rows, int columns)
        {

            DataGridView RunTimeCreatedDataGridView = new DataGridView();
            RunTimeCreatedDataGridView.DataSource = createGridForForm(rows, columns);
            DataGridViewColumn ID_Column = RunTimeCreatedDataGridView.Columns[0];
        ID_Column.Width = 200;

            int positionForTable = getLocationForTable();
            RunTimeCreatedDataGridView.Size = new Size(800, 200);
            RunTimeCreatedDataGridView.Location = new Point(5, positionForTable);
            myTabPage.Controls.Add(RunTimeCreatedDataGridView);

        }

我得到的错误是索引超出范围。它可能不是负数,并且必须小于大小。我想要做的是我从文本文件中获取一个表格,然后在运行时我在我的表单中显示它,但是表格的大小与我的数据网格视图不匹配,它看起来不好的。所以我想让表格适合数据网格视图。

【问题讨论】:

  • 如果第 0 列超出范围,则 DGV 必须为空,这意味着 DataTable 必须不返回任何内容。我会在你的 createGridForForm 方法中添加一些断点来检查发生了什么。例如,如果您调用 createGridForForm(1,1),则会添加一列但会添加零行。

标签: c# datagridview


【解决方案1】:

试试-

DataGridViewColumn ID_Column = dataGridView1.Columns[0];
ID_Column.Width = 200;

【讨论】:

  • 不,它不起作用:(。错误是一样的----mscorlib.dll中发生了'System.ArgumentOutOfRangeException'类型的未处理异常
猜你喜欢
  • 2012-11-27
  • 1970-01-01
  • 2014-06-14
  • 1970-01-01
  • 2012-08-17
  • 1970-01-01
  • 2021-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多