简体   繁体   English

调整表格大小以适合数据网格视图C#

[英]Resize the table to fit the data grid view C#

I want my table size to fit the data grid view. 我希望我的表大小适合数据网格视图。 The data grid view size is always the same but the table can change in amount of rows and columns. 数据网格视图的大小始终相同,但是表的行数和列数可以更改。

Here is how I populate the table: 这是我填充表格的方式:

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
        ListBox test = new ListBox();
        myTabPage.Controls.Add(test);
        table.Rows.Add(dr);
    }
    return table;
}

And here is how I create the datagridview: 这是我创建datagridview的方法:

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.BackgroundColor = Color.WhiteSmoke;

    RunTimeCreatedDataGridView.Size = new Size(995, 200);
    RunTimeCreatedDataGridView.Location = new Point(5, positionForTable);
    myTabPage.Controls.Add(RunTimeCreatedDataGridView);                   
}

I am getting an error when trying this code: 尝试以下代码时出现错误:

//DataGridViewColumn ID_Column = RunTimeCreatedDataGridView.Columns[0];
//ID_Column.Width = 200;

the error says that the: 该错误表明:

Index was out of range. 索引超出范围。 It may not be negative and must be smaller than the size 它可能不是负数,并且必须小于大小

If you add your datagrid to your tab control before get Columns[0] you don't get that error. 如果在获取Columns[0]之前将数据网格添加到选项卡控件中,则不会收到该错误。 Your code should be like that: 您的代码应如下所示:

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

EDIT: 编辑:

I add detailed lines to fill your DataGridView to it's container. 我添加了详细的行以将您的DataGridView填充到它的容器中。

private void createGridInForm(int rows, int columns)
{
    DataGridView RunTimeCreatedDataGridView = new DataGridView();
    RunTimeCreatedDataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
    myTabPage.Controls.Add(RunTimeCreatedDataGridView);
    RunTimeCreatedDataGridView.DataSource = createGridForForm(rows, columns);
    // fill the gridview to its container
    DataGridViewColumn ID_Column = RunTimeCreatedDataGridView.Columns[0];
    ID_Column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
    RunTimeCreatedDataGridView.Dock = DockStyle.Fill;
}

following line can be used to fill extra space with a specific column in a data grid view 下一行可用于在数据网格视图中的特定列填充额外的空间

RunTimeCreatedDataGridView .Columns["column_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

if you set this property of all column to DataGridViewAutoSizeColumnMode.Fill then all column takes same width 如果将所有列的此属性设置为DataGridViewAutoSizeColumnMode.Fill,则所有列的宽度相同

this is it 就是这个

for the width- 对于宽度

RunTimeCreatedDataGridView.AutoSizeColumnsMode=System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;

and for the height -- 为了身高

totalRowHeight = RunTimeCreatedDataGridView.ColumnHeadersHeight;
            foreach (DataGridViewRow row in RunTimeCreatedDataGridView.Rows)
                totalRowHeight += row.Height;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM