Click here to Skip to main content
15,792,510 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to empty specific column of numbers from datagridview? (i mean erase the values without header, not delete the column).

ERASE datagridview calumn values, so i dont need to do it manually and continue writing new ones.

How i created the datagridview:

i created a database table, with 5 ID's all of datatype "int", i connected the datagridview through DataSource, bindingsource. And when i put values in the table and press button:(button code below)


It will save the values into DataTable and draw a chart.

For example the first ID in the table is : "x1"

chart1.Series["x1"].YValueMembers = "x1";
chart1.Series["x2"].YValueMembers = "x2";
chart1.Series["x3"].YValueMembers = "x3";
chart1.Series["x4"].YValueMembers = "x4";
chart1.Series["x1"].XValueMember = "y1";
chart1.Series["x2"].XValueMember = "y1";
chart1.Series["x3"].XValueMember = "y1";
chart1.Series["x4"].XValueMember = "y1";

chart1.DataSource = database1DataSet2.Table;

What i think is neccesery is to actually delete the values from DataTable but i dont how to do it.

What I have tried:

I tried making the columns "null" but didnt solve anything.

I think at some point i managed to empty the column, i dont even remember how i did it, it was weeks ago, but when i did erase it and then write new numbers the chart didnt draw the lines. What i did was i deleted the column and added a new one. But as i mentioned, it didnt work. Maybe i could just delete the column of numbers from datatable, somehow.
Updated 8-Mar-22 7:23am
Maciej Los 8-Mar-22 12:05pm    

1 solution

If you would like to clear/empty column, without removing it, you need to clear/empty all rows belonging to that column.

In a pseudo-code:
foreach(row in datatable.rows)
  row[columnname] = DBNull.Value;
Share this answer
Richard MacCutchan 8-Mar-22 14:16pm    
+5. I had a feeling that was the answer, but my DataTable skills are a little "newbie".
Maciej Los 8-Mar-22 14:23pm    
Thank you, Richard. :D
123 123 2022 8-Mar-22 14:46pm    
how did you do it?
123 123 2022 8-Mar-22 14:44pm    
its not working for me, i tried
DataTable dataTable = new DataTable();   
foreach (DataRow dr in dataTable.Rows)               dr["Konduktivita1[µS_cm-1]"] = DBNull.Value;

I had to create a new datatable because if i dont, its just not wont let me put any datatable
Maciej Los 8-Mar-22 14:46pm    
A new datatable does not contain any columns and rows :)
If you want to copy one datatable into another, use:
DataTable dtCopy = existingDt.Clone(); //copies only the structure
//DataTable dtCopy = existingDt.Copy(); //copies the structure and data

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900