简体   繁体   English

DataGridView变量格式

[英]DataGridView variable formatting

I have a DataGridView with data I extract from SQL Server . 我有一个DataGridView,其中包含从SQL Server提取的数据。 I never know how many records i'll have in it so I need the grid to be variable to the number of rows extracted. 我不知道我将拥有多少条记录,因此我需要将网格可变为提取的行数。

Exemple : 范例:

在此处输入图片说明

  • Picture 1 : What I have right now. 图片1:我现在所拥有的。 1 row was extracted so I have a big dark gray space where the could have been data. 提取了1行,因此我有一个很大的深灰色空间,该空间可能是数据。
  • Picture 2 : What I want to have with only 1 row extracted. 图片2:仅提取1行,我想要的内容。 The grid resizes automaticly depending on the number of rows. 网格会根据行数自动调整大小。
  • Picture 3 : What I would like to have with many rows. 图片3:我想拥有很多行。

Also, I would like to remove the empty row that is always added too, adding a new row won't be an option. 另外,我想删除总是添加的空行,添加新行将不是一种选择。

Is that possible ? 那可能吗 ?

You are looking for DataSet.Datatable.Rows.Count this will tell you the total rows returned from your Sql Server 您正在寻找DataSet.Datatable.Rows.Count这将告诉您从Sql Server返回的总行

Update this cannot be achieved by using the any properties of the DataGridView Control 更新这不能通过使用DataGridView控件的任何属性来实现

You need to set the AutoSizeColumns properties: 您需要设置AutoSizeColumns属性:

datagridview.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
datagridview.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);

Assuming you're filling the grid from a DataSet , you can use this to prevent empty rows from being added: 假设您是从DataSet填充网格,则可以使用它来防止添加空行:

foreach (DataRow row in dataset.Tables[0].Rows)
{
      if (!string.IsNullOrWhiteSpace(row[0].ToString()))
      {
            //add row
      }
}

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

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