hi i have a datagrid view that is filled with fields from a database. until filling this datagridview it is alright. in this form i have a button delete and a textbox. when i press this button it takes the number in the field which will be the primary key of the field(thats y i have a datagridview). the sql server database will also be updated and the field with the chosen primary key is deleted. the only problem i have is that i want to empty the datagridview from all data and refill it with the new fields(which excludes the deleted field). since now i have managed to refill the table again every time i delete something but it is not removing the fields i was seeing before... forexample

when i open the form i find:

id Name Surname
1 name1 surname1
2 name2 surname2

then when i fill the textbox with the number 2 and i press delete i get this

id Name Surname
1 name1 surname1
2 name2 surname2
1 name1 surname1

this means that the fields i had the first time i entered the form stood there but never deleted themselves...
i tried using

datagridview1.DataSource = null;

but it didnt work... anyone knows something how to empty data from a datagridview? or is it not possible?

thanks

Why would you delete whole dgv. You dont need to. If you want to delete the row, and the dgv is bound to a data source, you have to remove the row from this data source.
Then will surely work.

Mitja

im pretty new to this datagridview thing... can u give me the code to do this please? thanks

Use This code to refresh your datagridview.

datagridview1.Rows.Clear();
  datagridview1.Columns.Clear();

the

datagridview1.Rows.Clear();

is not working... i left the Columns.Clear only and the columns were no longer visible but when i went to call the filldatagridview which adds the column names and the rows i get an error telling me that the column already exists

problem solve. found out the error... i was doing datagridview.columns.Clear().u cant change the datagrid view as it is connected to the table. so i had to make dt.Rows.Clear() and it worked. thanks for the replies

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.