Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
i am making a datagrid that displays the data on the basis of a search button..
for first try the program displayed without any errors...but when i clicked the search button next time with the different attribute to search on...the problem occured...the privious value was still there in the database.
here is my code:

C#
private void btnSearch_Click(object sender, EventArgs e)
       {

           int name = Convert.ToInt32(cboxJewlName.SelectedValue);
           int material=Convert.ToInt32(cboxJewlMaterial.SelectedValue) ;
          DataTable table = Bllcombohelper.getAllSelectedJewl(name, material);
          int roows=table.Rows.Count;
           int j=0;
           while (j < roows)
           {
               if (j > 14)
               {


                   dgvSearch.Rows.Add(1);
                  dgvSearch.ScrollBars = ScrollBars.Vertical;


               }
               dgvSearch.Rows[j].Cells[0].Value = table.Rows[j].ItemArray[0].ToString();
               dgvSearch.Rows[j].Cells[1].Value = table.Rows[j].ItemArray[5].ToString();
               dgvSearch.Rows[j].Cells[2].Value = table.Rows[j].ItemArray[6].ToString();
               dgvSearch.Rows[j].Cells[3].Value=table.Rows[j].ItemArray[7].ToString()+"/-";
               dgvSearch.Rows[j].Cells[4].Value = table.Rows[j].ItemArray[4].ToString();

               j = j + 1;


           }
 }



i don't know why on clicking on the search button next time....the old data in the data grid is not flushed or cleared..
please help me with this
Posted
Updated 15-Aug-11 8:36am
v3

Let the dataGridView is attached to the DataSet ds

Simply use the statment ds.Clear();
 
Share this answer
 
Comments
amsainju 16-Aug-11 0:32am    
i tried dgvSearch.Clear() too but it didn't helped...i am still nowhere.
The fastest way I know of is to do this at the very top of your method:

C#
dgvSearch.Rows.Clear();


To avoid flickering, you might want to do this:

C#
dvgSearch.BeginUpdate()
dvgSearch.Rows.Clear();

... your existing code...

dvgSearch.EndUpdate();


EDIT (after the 1-vote) ==================

Why was this 1-voted (or 2-voted for that matter)? I told him how to clear the rows. When you perform a search, clear the existing rowqs, and then re-add them with the new data you got back from your search. Even if he were inexperienced enough to assume he'd get back the same number of rows for each search, this is STILL the fastest way to clear old data unless you're using data binding. More often than not, refactoring is the nature of programming. The sooner everyone admits this to themselves, the better off everyone will be.
 
Share this answer
 
v4
Comments
amsainju 15-Aug-11 14:42pm    
i tried to use that but when i executed the program it showed this problem


Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

in this part of the code:

dgvSearch.Rows[j].Cells[0].Value =table.Rows[j].ItemArray[0].ToString();
#realJSOP 15-Aug-11 14:46pm    
After clearing the rows in the control, you would of course have to add them back with the new data. Just because your app needs to be re-architected doesn't mean you have to vote my answer a 2.
Simon_Whale 15-Aug-11 16:40pm    
Non flicker addition is a great tip to pass on +5
amsainju 16-Aug-11 2:12am    
hey sorry john i was just testing what these voting does.
#realJSOP 16-Aug-11 7:31am    
Don't do that - it annoys the ogres (me). :)
as u said, it didn't worked. I think you might not have updated the GridView.

Do like this

ds.Tables["Table-Name"].Rows.Clear();
GridViewX.DataSource = ds.Tables["Table-Name"];



If using ASP.NET, add one more like

GridViewX.DataBind();


It will definitely work..
best of Luck :)
 
Share this answer
 
if (dgvSearch.Rows.Count > 0)
{
dgvSearch.Rows.Clear();
dgvSearch.Rows.Add(15);
}

i had forgetten to use dgvSearch.Rows.Add(15); in my code
the error was shown because dgvSearch.Rows.Clear() clears or say deletes all the empty rows that i used for a better design...
thank u all for your help
 
Share this answer
 
Comments
nagendrathecoder 16-Aug-11 8:21am    
You can edit your question, no need to post an answer for updating your question.

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