简体   繁体   English

如何使用dataGridView显示全屏网格

[英]How to show full screen grid with dataGridView

I am reading an XML database and storing the entries in dataSet. 我正在读取XML数据库并将条目存储在dataSet中。 For UI, I am using dataGridView. 对于UI,我正在使用dataGridView。 So, all my database entries will be shown in dataGridView. 因此,我所有的数据库条目都将显示在dataGridView中。 Currently, if there are two entries in database, the grid is shown for two entries and all the other rows are shown blank. 当前,如果数据库中有两个条目,则为两个条目显示网格,而所有其他行显示为空白。 I want to show full screen grid with dataGridView. 我想用dataGridView显示全屏网格。 If I have two entries in database, I sholuld see two entries and all other rows/columns with empty cells but with grid. 如果我在数据库中有两个条目,则应该看到两个条目以及所有其他行/列都带有空单元格但带有网格。 No part of datagridView should be blank. datagridView的任何部分都不应为空。 Please help for solving the problem. 请帮助解决问题。 I tried using properties such as Dock, AutoSizeColumnsMode, AutoSizeRowsMode etc of dataGridView but was not able to achieve this. 我尝试使用dataGridView的Dock,AutoSizeColumnsMode,AutoSizeRowsMo​​de等属性,但无法实现此目的。

Here's the code for reading data. 这是读取数据的代码。

private System.Windows.Forms.DataGridView dataGridView1;
DataSet ds = new DataSet();
DataTable dtAll = new DataTable();
XmlReader xmlFile; 
xmlFile = XmlReader.Create("Filepath");
ds.ReadXml(xmlFile);
dtTable = ds.Tables[0].Copy();
dataGridView1.DataSource = ds.Tables["entry"];

Use a from, where you place your grid. 使用起始位置,放置网格。 The Form should have no border and maximized. 表格应无边界且最大化。 This will show up your form full screen. 这将全屏显示您的表单。

this.BorderStyle = BorderStyle.None;
this.WindowState = FormWindowState.Maximized;

您检查一下一次网格显示多少行,例如,如果网格支持20行显示而没有滚动显示,而您只有5行,则必须向表ds.Tables [“ entry”]中添加15空行。

Going off the suggestion by Manoj Extreme , you can fill the sourced DataTable with dummy rows to fill the height of the DataGridView . 根据Manoj Extreme的建议,您可以用虚拟行填充源DataTable ,以填充DataGridView的高度。 For the width, just set AutoSizeColumnsMode to Fill , or more specifically, only set AutoSizeMode for the column containing the largest data. 对于宽度,只需将AutoSizeColumnsMode设置为Fill ,或更具体地说,仅将AutoSizeMode设置为包含最大数据的列。 For example: 例如:

DataTable dt = ds.Tables["entry"];

while (dt.Rows.Count * dataGridView1.RowTemplate.Height + dataGridView1.ColumnHeadersHeight < dataGridView1.Height)
{
    dt.Rows.Add(string.Empty);
}

dataGridView1.DataSource = dt;
dataGridView1.Columns["TheBigDataColumnName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

Note: There is a downside to this method. 注意:此方法有一个缺点。 If you allow the user to change the row's heights, grow the Form's height, or delete rows then you will have to do something similar afterwards to fix the resulting non-filled space. 如果允许用户更改行的高度,增大窗体的高度或删除行,则之后必须执行类似的操作来修复生成的未填充空间。

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

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