简体   繁体   English

C#清除绑定到数据源的datagridview

[英]c# clear datagridview bound to datasource

I have a datagridview bound to a datasource, all headers are added to the columns collection with a datapropertyname set. 我有一个绑定到数据源的datagridview,所有标头都添加到具有datapropertyname设置的列集合中。

When I clear the datagridview using 当我使用清除datagridview时

DataGridView1.DataSource = null;

The headers disappear also and when I fill the datagridview again the header texts are the database column names. 标头也消失了,当我再次填充datagridview时,标头文本就是数据库列名。 How do I clear a bound datagridview without removing the headers? 如何在不删除标题的情况下清除绑定的datagridview?

You can use this 你可以用这个

if (dataGridViewNotas.DataSource != null)
    ((DataTable) dataGridViewNotas.DataSource).Rows.Clear();

One of the approach to handle this issue is to use collection as data source, 解决此问题的方法之一是使用集合作为数据源,

Create a class with properties representing the data source (Each property would represent a column in the database) 创建一个具有代表数据源的属性的类(每个属性将代表数据库中的一列)

public class Student
{
   public string Name { get; set; }
   public string Address { get; set; }
}

You need Add column to datagridview manually and set relevant DataPropertyName for each column and set the HeaderText . 您需要手动将列添加到datagridview并为每列设置相关的DataPropertyName并设置HeaderText When you load the data from database first fill this data into a List. 从数据库加载数据时,首先将此数据填充到列表中。 So you will have a List<Student> . 因此,您将拥有一个List<Student>

List<Student> studentDetail = new List<Student>();

Set this as the data source of the datagridview. 将此设置为datagridview的数据源。

dataGridView1.DataSource = studentDetail;

Clearing Data source 清除数据源

To clear the data source of the Grid just create a empty Student list and set it as data source again. 要清除网格的数据源,只需创建一个空的Student列表,然后再次将其设置为数据源。 When you set like this header of each column will be retained. 当您进行类似设置时,将保留每列的标题。

List<Student> emptyStudentDetail = new List<Student>();
dataGridView1.DataSource = emptyStudentDetail;

If you do not want to use collection of objects and still refresh your data source using this.dataGridView1.DataSource = null; 如果您不想使用对象集合,而仍然使用this.dataGridView1.DataSource = null;刷新数据源this.dataGridView1.DataSource = null; then try this approach: 然后尝试这种方法:

Assuming you are using for data source either DataSet or local database. 假设您正在使用数据集DataSet或本地数据库。 Each time before you bind the dataGridView with new data source, create unbound columns with the same names as the names of your data source. 每次将dataGridView与新数据源绑定之前,请创建未绑定的列,这些列的名称应与数据源的名称相同。 Once they are created, you should hide them, because they will be needed when you refresh dataGridView's data source. 创建它们之后,您应该隐藏它们,因为刷新dataGridView的数据源时将需要它们。 The following sample code is aware of the data source columns names and therefore they are hard coded, but you can loop each time through the data source and create new collection of unbound columns, if it is necessary. 下面的示例代码知道数据源列的名称,因此它们是硬编码的,但是如果有必要,您可以每次遍历数据源并创建未绑定列的新集合。

    private void Form1_Load(object sender, EventArgs e)
    {
        this.dataGridView1.DataSource = GetDataSet();
        this.dataGridView1.DataMember = "Students";
        this.dataGridView1.Columns.Add("unboundColumn1", "ID");
        this.dataGridView1.Columns.Add("unboundColumn2", "Name");
        this.dataGridView1.Columns["unboundColumn1"].Visible = false;
        this.dataGridView1.Columns["unboundColumn2"].Visible = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.dataGridView1.Columns["unboundColumn1"].Visible = true;
        this.dataGridView1.Columns["unboundColumn2"].Visible = true;
        this.dataGridView1.DataSource = null;
    }

    private DataSet GetDataSet()
    {
        DataSet dataSet = new DataSet();
        dataSet.Tables.Add("Students");
        dataSet.Tables["Students"].Columns.Add("ID", typeof(int));
        dataSet.Tables["Students"].Columns.Add("Name", typeof(string));
        dataSet.Tables["Students"].Rows.Add(1, "John Joy");
        dataSet.Tables["Students"].Rows.Add(2, "Ivan Nova");
        dataSet.Tables["Students"].Rows.Add(3, "Michael German");
        return dataSet;
    }

Struggled with this for 24 hrs. 奋斗了24小时。 Not sure why it works, but the solution that did not produce a runtime error for me is to dispose of the table adapter associated with the datagridview: 不知道为什么它能工作,但是没有对我产生运行时错误的解决方案是处置与datagridview关联的表适配器:

if (this.dataTableTableAdapter != null) 
{
    this.dataTableTableAdapter.Dispose();
}

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

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