简体   繁体   中英

How to clear data grid view rows and columns except the column headers

I have a quantity based information data grid view it has 3 columns of type text and 1 column of type check box, now this data grid view is filled with different item quantities and different expire dates for the same item from the inventory table whenever the user enters an item name.
The problem is whenever the item is selected and inserted in another data grid view using a button named as Add Item the tools used for data entry are cleared also the data grid view of quantity based information should be cleared, I tried this code but it erases the data grid view completely I just want the data retrieved from database only to be cleared and the headers of the data grid view still appear.
This codes are used in Add Item button

 DataRow r = dt.NewRow();
 r[0] = sBillItemBartxt.Text;
 r[1] = sBillItemNametxt.Text;
 r[2] = itemQuanttxt.Text;
 r[3] = itemPricetxt.Text;
 r[4] = itemTtlPricetxt.Text;
 r[5] = expire;
 dt.Rows.Add(r);
 sBilldgv.DataSource = dt;
 clearItemsText();
 sBillTtlCostlbl.Text = (from DataGridViewRow row in sBilldgv.Rows where row.Cells[4].FormattedValue.ToString() != string.Empty select Convert.ToDouble(row.Cells[4].FormattedValue)).Sum().ToString();
 sBillItemBartxt.ReadOnly = sBillItemNametxt.ReadOnly = false;
 dtRes.Clear();
 exDatedgv.DataSource = null;
 exDatedgv.Rows.Clear();
 exDatedgv.Columns.Clear();

They can be cleared nearly the same way as you add them (Without removing columns).

MyDataGridView.Rows.Clear();

This will target rows instead of columns.

I think your header is being cleared because of

exDatedgv.Columns.Clear();

Also if you only wish to remove the rows that are repeated, you might want to consider using a loop to check for the repeated row and remove that row only before adding a new one. I would also tell you to use a BindingList with your datagridview but since one of your columns is a checkbox im not really sure how you could achieve this with a BindingList

It is unclear from your code what you are trying to do. Since you are using as DataTable I am not sure why the DataTable dtRes.Clear(); does not work as this will clear all the data and KEEP the column headers. When you set exDatedgv.DataSource = null; this will clear the rows and columns so the next two lines are superfluous. To get the DataGridView to clear as you describe… simply Clear the DataTable associated with that DataGridView . This will leave the headers intact, however after this "Clear" there will be no data in the table, so if you try to re-bind the table you just cleared, obviously it will be empty. If you do not want to remove the data from the table, then you will have to either set the headers yourself, create an empty table for this purpose or simply (not the best solution) create a copy from a master. Below I made a data bound DataGridView , then cleared it as described above with button 2, then re-set (re-bind) the DataGridView to a copy of the original table. This clears the DataGridView leaving the headers when pressing button 2, then re-sets the DataGridView to the original data when pressing button 3. Hope this helps.

private void button2_Click(object sender, EventArgs e) {
  //dataGridView1.Columns.Clear(); <-- clears headers
  //dataGridView1.Rows.Clear(); <-- will crash if data is bound
  //dataGridView1.DataSource = null; <-- removes headers
  dt.Clear();  // <-- Clears data and leaves headers, removes data from table!
}

private void button3_Click(object sender, EventArgs e) {
  dt = masterTable.Copy();  // <-- Get a new table if it was cleared
  dataGridView1.DataSource = dt;
}

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