Clear all rows in the datagridview (datagridview.datasource=dataset.tables(0))
-
25 aprilie 2012 07:42
Hi,
i am using a datagridview, i added columns with data property manually and i fill the datagridview using dataset
datagridview.datasource=ds.tables(0)
for clearing the rows i am using
datagridview.datasource=nothing
but it clearing the created columns also
i used datagridview.rows.clear but its not working i am getting error
i want to clear the rows but not the column
help me waiting for reply.....
Toate mesajele
-
25 aprilie 2012 10:50
i got one solution
DirectCast(datagridview1.DataSource, DataTable).Rows.Clear()
it is working
if any another way to clear please tell me
waiting for reply..............
- Marcat ca răspuns de Kumar Jalli 30 aprilie 2012 11:28
-
26 aprilie 2012 03:48Moderator
Hi Kumar Jalli,
There are serial ways for choice when you really want to clear the data of DataGridView Control.
1. Use for loop to clear the row data one by one.
for (int i = 0; i < dataGridView1.RowCount; i++) { dataGridView1.Rows.Remove(dataGridView1.Rows[0]); }2.Set DataSource property to NULL directly.
DataGridView.DataSource=null;
If you want to bind DataSet to this control, you’d better setting DataSet to NULL first.
DataSet DataSet1=new DataSet(); DataSet1.clear();
3. Above way may remove all of Tables inside in specified DataSet,so you'd better removing its Table separately.
DataSet ds = new DataSet(); DataTable dt = new DataTable(); dt.Columns.Add("val", typeof(int)); dt.Columns.Add("name", typeof(string)); dt.Rows.Add(1, "good"); ds.Tables.Add(dt); dt.TableName = "test"; dataGridView1.DataSource = ds.Tables[0];Hence, you can use following code to clear specified table.ds.Tables[0].Clear();
Sincerely,
Jason Wang
Jason Wang [MSFT]
MSDN Community Support | Feedback to us
- Editat de Jason Dot WangMicrosoft Contingent Staff, Moderator 26 aprilie 2012 04:27
- Marcat ca răspuns de Jason Dot WangMicrosoft Contingent Staff, Moderator 8 mai 2012 02:18
-
26 aprilie 2012 04:50
Jason Dot Wang:-)
Sorry but some of your answers have been duplicated for what the user has offered……And the man is using Nothing for VB.NET instead of C#,and this isn't right!So plz look at the issue then answer……
Now here's another way——Just use DataTable.Clone() to copy the structure and bind the cloned DataTable to the GridView:
DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(int)); dt.Rows.Add(1); dt = dt.Clone(); DataGridView dv = new DataGridView(); dv.Parent = this; dv.Dock = DockStyle.Fill; dv.AllowUserToAddRows = false;
dv.DataSource = dt;
【vb.net】
Dim dt As New DataTable() dt.Columns.Add("Id", GetType(Integer)) dt.Rows.Add(1) dt = dt.Clone() Dim dv As New DataGridView() dv.Parent = Me dv.Dock = DockStyle.Fill dv.AllowUserToAddRows = False dv.DataSource = dt
- Editat de ProgrammingVolunteerMVP 26 aprilie 2012 04:52
- Marcat ca răspuns de Jason Dot WangMicrosoft Contingent Staff, Moderator 8 mai 2012 02:18
-
30 aprilie 2012 11:40
the another way i got to clear datagridview
First take the dataset from control toolbox create a datatable in that with required columns
now add this dataset to datagriview means
datagridview.datasource=dataset
datagridview.datamember=datatable
now i f u want to clear the datagridview
just clear the dataset rows ds.Tables(0).rows.clear()
now the rows all clear in the datagridview and columns are remaining like that if you want to add records to datagridview just add the required rows in the dataset only then it automatically displays in datagridview
thank you..............
- Marcat ca răspuns de Kumar Jalli 30 aprilie 2012 11:40
