点击button后,读取数据后,然后执行下面的代码,用这个把dataTable里面的数据填入DataGridView中
dataGridView1.Rows.Add(dt.Rows.Count);
for (int i = 0; i < dt.Rows.Count0; i++)
{
    dataGridView1["clmBookNum", i].Value = dt.Rows[i]["Id"];
    dataGridView1["clmBookName", i].Value = dt.Rows[i]["Name"];
    dataGridView1["clmBookType", i].Value = dt.Rows[i]["Type"];
    dataGridView1["clmBookPrice", i].Value = dt.Rows[i]["Price"];
}
发现以前存在的行都会被覆盖,请问有什么解决方法或者更好的给dataGridview赋值的方法吗

解决方案 »

  1.   

    你原来的数据是绑定的么?如果是就直接在绑定的table直接合并或用NewRow方法加到表中
      

  2.   

    就是我1楼的那种方法给datagridview赋值的,能说的具体点吗,没看明白
      

  3.   

    为什么不直接绑定:   dataGridView1.DataSource=dataTable;
      

  4.   

    一般就是这样弄得dataGridView1.Rows.Add(dt.Rows.Count);
    for (int i = 0; i < dt.Rows.Count0; i++)
    {
      dataGridView1["clmBookNum", i].Value = dt.Rows[i]["Id"];
      dataGridView1["clmBookName", i].Value = dt.Rows[i]["Name"];
      dataGridView1["clmBookType", i].Value = dt.Rows[i]["Type"];
      dataGridView1["clmBookPrice", i].Value = dt.Rows[i]["Price"];
    }
    你这个应该是button里面的代码吧,你每次点击都是都是将DataGridView整个赋值啊(每次从第0行开始)发现以前存在的行都会被覆盖,想不被覆盖,应该定义一个private int DataGridViewRowCount=0;button里面
    {
    int i=DataGridViewRowCount;
    for (; i < dt.Rows.Count0+DataGridViewRowCount; i++)
    {
      dataGridView1["clmBookNum", i].Value = dt.Rows[i]["Id"];
      dataGridView1["clmBookName", i].Value = dt.Rows[i]["Name"];
      dataGridView1["clmBookType", i].Value = dt.Rows[i]["Type"];
      dataGridView1["clmBookPrice", i].Value = dt.Rows[i]["Price"];
    }
    DataGridViewRowCount +=dt.Rows.Count0;
    }