Clear Datagridview

Sha1395
Sha1395 used Ask the Experts™
on
Hi All,

I was trying to clear the datagridview everytime the button click and load the new data. most of the blog suggest to use rows.clear but it throwing error for me.

here is my code

 private void button9_Click(object sender, EventArgs e)
        {
            //int n = dataGridView1.Rows.Add();

            dataGridView1.Rows.Clear();
            //dataGridView1.Rows[n].Cells[0].Value = "title";
            System.Messaging.Message mes;
            string m;
            int n = dataGridView1.Rows.Add();
            
            try
            {
               
                
                int len = mq.GetAllMessages().Length;
                
                if (len != 0)
                {
                    for (int i = 0; i < len; i++)
                    {
                        int j = 1;
                        j = j + i;

                        mes = mq.Receive(new TimeSpan(0, 0, 3));
                        mes.Formatter = new XmlMessageFormatter(new string[] { "System.String,mscorlib" });
                        m = mes.Body.ToString();
                       dataGridView1.Rows[j].Cells[4].Value = m.ToString();
                        //   listBox1.Items.Add(m.ToString());

                    }
                }
                else
                {
                    //listView1.Items.Clear();
                 //   dataGridView1.RowCount = 1;
                   // dataGridView1.Rows.Clear();
                    m = "No Error Queue Message";
                    //listView1.Items.Add(m.ToString());
                    
                    dataGridView1.Rows[1].Cells[0].Value = m.ToString();
                    
                }

            }
            catch
            {
              //  dataGridView1.Rows.Clear();
                m = "no Message";
                //listView1.Items.Add(m.ToString());
                dataGridView1.Rows[1].Cells[0].Value = m.ToString();
            }
            
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
dataGridView1.datasource=null;
dataGridView1.databind();

Author

Commented:
am not using datasource and am not using datagrid this is datagridview.
int n = dataGridView1.Rows.Add();->Add only a row, you must add n row before set data for it
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:
thanks minhvc,

i was adding before adding data in the column but after modify the code it straight a way going to catch exception and throw "Make sure the arguments to this method have valid values".
Its fine you are not using Datasource
but for clearing items uyou can use this

dataGridView1.datasource=null;
dataGridView1.databind();

Datagridview also have Datasource Property

Author

Commented:
Thanks all for your help finally figured out

below code do the job for me

 dataGridView1.Rows.Clear();
                    m = "No Error Queue Message";
                    int n = dataGridView1.Rows.Add();
                    dataGridView1.Rows[1].Cells[0].Value = m.ToString();

Thanks again
Commented:
the code below worked for me

((DataTable)datagridview1.DataSource).Rows.Clear();

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial