I can clear the content of the datagridview by clearing the dataset

ds.Clear();

Then reset the datasource

datagridview.datasource = ds.Tables[0];

Now I will do a select statement that has different columns from the current datagridview.
What happens is, the column from the previous datasource is still intact. As if the clearing property is just to clear the content of the dataset but not the table itself, so the headers are still present.

How could I do a full reset of dataset or maybe the gridview ?

Dude... you're killing me here :twisted:

Try:

datagridview.Rows.Clear();

doesn't work

SqlDataAdapter da = new SqlDataAdapter("SELECT BookTitle FROM Book", con);
            dataGridView1.Rows.Clear();
            ds.Clear();
            da.Fill(ds);
            
            if (ds.Tables[0].Rows.Count > 0)
            {
                dataGridView1.DataSource = ds.Tables[0];
            }

I have this in my page load

SqlDataAdapter da = new SqlDataAdapter("SELECT MAX(Transaction_No) AS Transaction_No FROM Transactions", con);
            da.Fill(ds);
            DataTable dt = new DataTable();
            dt = ds.Tables[0];

            if (dt.Rows.Count > 0)
            {
                textBox1.Text = (Convert.ToInt32(dt.Rows[0]["Transaction_No"]) + 1).ToString();
            }

Ok, well... clearing the rows on the dataGridView isn't going to help you if you don't re-populate the dgv with new content afterward. But your question was how to clear the dgv and that's where dataGridView1.Rows.Clear() comes in :)

Then how could I do this?

I already re-populate the datagridview by clearing the dataset then setting the datasource of datagridview to dataset.

But the thing is, the previous columns are still there, how can i remove it?

Ok, I misread what your intent was here...

dataGridView1.Columns.Remove("columnName");

Will remove any columns that are you don't want in place.

dataGridView1.Columns.Add("newColumnName");

Will add new columns with chosen names. From there it's a matter of ensuring that the new column names match to the data you're using with your new databinding.

Beyond that, my oncoming headache is putting me out of commission for a few hours so I wish you luck.

Alternately, you COULD just use 2 different dgvs if you're using separate data sources and have the appropriate dgv visible while the other is invisible (using the visible property) depending on which dgv you need to present to the user. For win-forms apps you just place the one over top of the other to make it "appear" as if it's the same view in the same place but that way you don't have to muck around with adding and removing columns on-the-fly.

It's only one table. The difference is the SQL Select statements which selects different columns.

Yes, it's one table... but... it's DIFFERENT information being selected and DIFFERENT columns/content being displayed to the user.

Which is why I'm saying that since it's effectively 2 separate subsets of data, maybe you should use 2 separate dgv's :)

>How could I do a full reset of dataset or maybe the gridview ?

dataGridView1.DataSource = null;

adapost was correct.

dataGridView1.DataSource = null;

This would completely clear the data grid and the values it has, if u populate it again, u ll get a all new set of datas

Already tried that before but no good.

Already tried that before but no good.

Did you even try my suggestion? :twisted: after 2-3 days of trying to clear and rebuild 1 dgv you'd have been able to stack 10 of them with my suggestion by now :P

dataGridView1.DataSource = new DataTable;

use the above code.. am sure, u ll get it cleared this time around.

Remember, mark it solved...

had the same issue with vb 2008 express ended up using
DataGridView1.Columns.Clear()
datagridview1.rows.clear()
worked fine for me. Actully importing excel into a datagrid, so need to wipe cod data and columsn to replace with new sets.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.