We help IT Professionals succeed at work.

How to clear a datagridview

ANINDYA
ANINDYA asked
on
Experts
I have a datagridview which is displaying data already by the following code.

private void show_valueInDGVItems()
        {
            Int32 CustomerID;
            CustomerID = Convert.ToInt32(label_Custmer_ID.Text);
            string query3 = "select * from Table_InvoiceItem WHERE CustomerID=@parameter1";
            using (SqlConnection con3 = new SqlConnection("Data source=INVENTOR-6FBADA\\SQLEXPRESS;Initial Catalog=AnindyaCD;Integrated Security=SSPI"))
            {
                using (SqlCommand cmd3 = new SqlCommand(query3, con3))
                {
                    cmd3.Parameters.AddWithValue("@parameter1", CustomerID);
                    using (SqlDataAdapter ad = new SqlDataAdapter(query3, con3))
                    {
                        ad.SelectCommand = cmd3;
                        DataSet ds = new DataSet();
                        ad.Fill(ds, "Table_InvoiceItem");
                        dataGridView_showItemtoSell.DataSource = ds.Tables[0];
                    }
                }
            }
        }
Now my question is how to clear the datagridview by a checkbox event of checkchanged.
private void show_valueInDGVItems()
        {
            Int32 CustomerID;
            CustomerID = Convert.ToInt32(label_Custmer_ID.Text);
            string query3 = "select * from Table_InvoiceItem WHERE CustomerID=@parameter1";
            using (SqlConnection con3 = new SqlConnection("Data source=INVENTOR-6FBADA\\SQLEXPRESS;Initial Catalog=AnindyaCD;Integrated Security=SSPI"))
            {
                using (SqlCommand cmd3 = new SqlCommand(query3, con3))
                {
                    cmd3.Parameters.AddWithValue("@parameter1", CustomerID);
                    using (SqlDataAdapter ad = new SqlDataAdapter(query3, con3))
                    {
                        ad.SelectCommand = cmd3;
                        DataSet ds = new DataSet();
                        ad.Fill(ds, "Table_InvoiceItem");
                        dataGridView_showItemtoSell.DataSource = ds.Tables[0];
                    }
                }
            }
        }

Open in new window

Comment
Watch Question

Commented:
Insert the checkbox to the screen and the onclick event will change the value, so just put dataGridView_showItemtoSell.Rows.Clear(); in the click event.
Shahan AyyubSenior Software Engineer

Commented:
1) Insert a checkbox
2) On this event CheckBox1_CheckStateChanged

     write:  

        If (CheckBox1.Checked == True)
            dataGridView_showItemtoSell.DataSource = Nothing


Author

Commented:
Master w00te
there is an error is appearing .
Can you please help.
for more information please see the image attached.
Thanking you,
Anindya
error.JPG
Shahan AyyubSenior Software Engineer

Commented:
replace Nothing with null
Shahan AyyubSenior Software Engineer

Commented:
Try:

1) Insert a checkbox
2) On this event CheckBox1_CheckStateChanged

      write:  

        If (CheckBox1.Checked == True)
             dataGridView_showItemtoSell.DataSource  = null

Author

Commented:
Expert shahan developer
the nothing option is not coming in the intellisense .
So what to do Sir.
Thanking you
Anindya
error.JPG
Shahan AyyubSenior Software Engineer

Commented:
Solution is provided in this  ID: 32988357, I think you didn't see the updated post.

Author

Commented:
Expert ShahanDeveloper
thanks for answering . But I have some problem.
Please see the attached image.
Thanking you
Anindya
error.JPG
Senior Software Engineer
Commented:
try this:

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                for(int i=0;i<dataGridView1.Rows.Count-1;i++)
                  dataGridView_showItemtoSel.Rows.RemoveAt(i);  
             
        }

Commented:
dataGridView1.DataSource = null;

Commented:
private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
        }
Shahan AyyubSenior Software Engineer

Commented:
Hi! Anindya

the code I provided you:

       If (CheckBox1.Checked == True)
              dataGridView_showItemtoSell.DataSource  = null

will work. you are getting value back in table because you are inserting value and table re-fill with all the data and you are thinking that Grid nothing removed.

Commented:
dataGridView1.DataSource = null;

or

dataGridView_showItemtoSell.DataSource  = null;


//yes it is tested, it clears datagrid
Shahan AyyubSenior Software Engineer

Commented:
In a more clear way when you wrote the code:

      If (CheckBox1.Checked == True)
               dataGridView_showItemtoSell.DataSource  = null

Your DataGridView got Clear, but TABLES are not changed they still have those records.
Then again you inserted value, and invoke DataGridView to display records. It picked data from TABLE which contain those records as well which are not in DataGridView now. So, you are thinking that DataGridView does not Clear.

Hope it makes you clear now.

Author

Commented:
Expert Shahan Developer
I am ecstatic to see the kind of help you rendered towards me.
thank you again.
It is 3AM in the morning and you have been giving me codes.I do not have a proper word in English dictionary to express my gratitude.
anindya Chatterjee
Bangalore
India
   
Shahan AyyubSenior Software Engineer

Commented:
Glad to help you. No problem! :)