datagrid视图行在所有行中重复数据 [英] datagrid view rows repeat data in all rows

查看:100
本文介绍了datagrid视图行在所有行中重复数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网格视图中,当我用数据填充第一行并移动到第二行并编辑第二行并将行数据更改为与上一行相同时,我的网格视图中出现了一个奇怪的问题所以在所有行上编辑更改到上一行我怎么能解决这个问题这个问题发生在我用桌子移动时输入键盘箭头





编辑:来自评论



这是我的代码

hi every body i have a strange problem in my grid view when i fill my first row with data and move to the second row and after edit the second row and leave the row the data changed to the same as the previous row and so on all row which is edit change to the previous row how can i solve this problem this problem happened when i move with table enter arrows of keyboard


from comments

this my my code

if (dataGridView1.Rows[e.RowIndex].Cells[1].Value != null)
{
  string cellValue = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
  if (!string.IsNullOrEmpty(cellValue))
  {
    _bar = cellValue;
    PopulateColumns();
    dataGridView1.Rows[e.RowIndex].Cells[2].Value = _proname;
    dataGridView1.Rows[e.RowIndex].Cells[3].Value = _price;
    dataGridView1.Rows[e.RowIndex].Cells[4].Value = _unit;
  }
}



i use PopulateColumns(); 从数据表访问数据和将它传递给网格视图,它等于 dataGridView1.Rows [e.RowIndex] .Cells [1] .Value 。当我使用断点时它工作正确如果我停止断点它在每行重复相同的数据,尽管值 dataGridView1.Rows [e.RowIndex] .Cells [1] .Value 不一样?


i use PopulateColumns(); to access data from data table and pass it to the grid view where it equal dataGridView1.Rows[e.RowIndex].Cells[1].Value . when i use break-point it works correct if i stop break point it repeat the same data in each row despite the value of dataGridView1.Rows[e.RowIndex].Cells[1].Value is not the same ?

推荐答案

你需要在每个循环中初始化新行:



You need to initialize the new row in every loop:

DataRow dr;

foreach(var row in myEnumerable)
{
   //Do this in every loop
   dr = new DataRow();

   //...Set column values

   dataTable.Rows.Add(dr);
}


这篇关于datagrid视图行在所有行中重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆