简体   繁体   中英

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.

When I clear the datagridview using

DataGridView1.DataSource = null;

The headers disappear also and when I fill the datagridview again the header texts are the database column names. How do I clear a bound datagridview without removing the headers?

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 . When you load the data from database first fill this data into a List. So you will have a List<Student> .

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

Set this as the data source of the 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. 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; then try this approach:

Assuming you are using for data source either DataSet or local database. 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. Once they are created, you should hide them, because they will be needed when you refresh dataGridView's data source. 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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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