点击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赋值的方法吗
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赋值的方法吗
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;
}