简体   繁体   English

C#WinForms:将行添加到dataGridView

[英]C# WinForms: Adding rows to a dataGridView

I've just started to use dataGridView, and some things seem strange to me: 我刚刚开始使用dataGridView,对我来说有些事情似乎很奇怪:

  • when there is a Columns property, in which you can add columns, why is there not a Rows property? 当有一个可以添加列的Columns属性时,为什么没有Rows属性? It seems you can only add rows programmatically. 看来您只能以编程方式添加行。 Or am I wrong? 还是我错了?
  • the 'star' icon to the left of the first row, can't it be removed? 第一行左侧的“星形”图标,不能将其删除吗?
  • is it possible to disable sorting? 是否可以禁用排序? I mean when you click on a column, the "sorting-arrow" appears, suggesting that the cells would be sorted. 我的意思是,当您单击列时,将出现“排序箭头”,表明将对单元格进行排序。

If it is only possible to add rows programmatically, I have this question: 如果只能以编程方式添加行,则我有以下问题:

  • I need a dataGridView with 1 column and x rows. 我需要一个具有1列和x行的dataGridView。 How to do this the quickest and easiest way? 如何最快最简单地做到这一点? The cells will be filled at runtime, programmatically. 单元将在运行时以编程方式填充。

update: About disabling sorting, I found out myself: there is a property to change this if you open the (Collections) of the columns. 更新:关于禁用排序,我发现自己:如果打开列的(集合),则可以更改此属性。

You can add Rows by calling dataGridView1.Rows.Add(); 您可以通过调用dataGridView1.Rows.Add();添加行dataGridView1.Rows.Add(); . For multiple rows there is an overload for the same available. 对于多行,相同的可用过载。 dataGridView1.Rows.Add(5);

Now to fill the dataGridView1 rows you can either assign a DataSource and set the DataPropertyName for the Column. 现在要填充dataGridView1行,您可以分配一个DataSource并设置Column的DataPropertyName Else loop through and fill the data cell wise like dataGridView1[columnindex,rowindex].Value =something 否则,像dataGridView1[columnindex,rowindex].Value =something那样循环遍历并填充数据单元格dataGridView1[columnindex,rowindex].Value =something

The star icon shows the current row which is being edited, you can choose to hide that cell by setting the RowHeadersVisible to false. 星号图标显示正在编辑的当前行,您可以通过将RowHeadersVisible设置为false来选择隐藏该单元格。

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

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