for(int c =1;c<=author.books.count;c++)
{
string author = author.name.ToString();
string author = author.book_name.ToString();
string author = author.published_year.ToString();
string[] row = new string[]{author, book_name, published_year};
dataGridView1.Rows.Add(row);
}
这是我的代码(类似)........我正在从一个 XML 文件获取数据。我的问题是,当我选择一位作者时,它会在 windows_form 的 gridview 中提供数据。之后,如果我再选择一位作者,则数据会附加到预览数据中。我只想显示当前选定的作者数据。
我尝试过:
dataGridView1.Rows.Clear();
当我在 for 循环之前将此代码放入 pgm 中时,它甚至没有在整个 pgm 中提供“GridView”中的任何数据。
我尝试过:
if (this.dataGridView1.DataSource != null)
{
this.dataGridView1.DataSource = null;
}
else
{
this.dataGridView1.Rows.Clear();
}
与上面一样,它不显示任何数据。
请帮助我......
最佳答案
如果您的datasource = null
,您将清除行。这意味着如果已经分配了某些数据源,您将不会清除行。这是你的情况。
所以你应该这样做。不需要 if 语句。
删除此语句
if (this.dataGridView1.DataSource != null)
你的代码应该是这样的
this.dataGridView1.DataSource = null;
this.dataGridView1.Rows.Clear();
这是清除行的正确方法。
关于c# - 如何在动态添加现在数据之前清除 dataGridView 行以前的数据....?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10750012/