简体   繁体   English

如何从winForm c#中的datagridview获取用户输入的数据?

[英]How to get the data the user has input from a datagridview in winForm c#?

I have an empty datagridview which I am using to collect data from the user. 我有一个空的datagridview,我用它来收集用户的数据。 When the user moves onto the next tab I want to save the data entered. 当用户移动到下一个选项卡时,我想保存输入的数据。 But according to the datagridview there is no rows. 但根据datagridview,没有行。 Yet when I go back to that tab the data still there. 然而,当我回到那个标签时,数据仍然存在。

I've tried this.. 我试过这个..

int i_rows = dataGridView1.RowCount;
for (int i = 0; i < i_rows; i++)
{
  var colValue = dataGridView1.Rows[i].Cells[0].Value;
}

and this.. 和这个..

foreach (DataRow row in dataGridView1.Rows)
{
  var colValue = dataGridView1.Rows[i].Cells[0].Value;
}

And I added an event to when UserAddRow which is triggered but still no rows in the dataGridView. 我添加了一个事件,当UserAddRow被触发但仍然没有dataGridView中的行。

Have you tried the "CellValueChanged" event instead of "UserAddRow" event. 您是否尝试过“CellValueChanged”事件而不是“UserAddRow”事件。

The UserAddRow event is triggered when the last row goes into edit mode. 当最后一行进入编辑模式时,将触发UserAddRow事件。 At this point the cells in the last row hasn't registered any value, the edited cell register the value when the cell value changed event is triggered. 此时,最后一行中的单元格未注册任何值,编辑的单元格在触发单元格值更改事件时注册该值。 If you use the "CellValuechanged" event it is most likely that you will get the value of the cell. 如果您使用“CellValuechanged”事件,则很可能会获得该单元格的值。

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

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