如何清除datagidview的所有值? [英] how to clear all the values of the datagidview?

查看:111
本文介绍了如何清除datagidview的所有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个数据网格,该网格基于搜索按钮显示数据.
第一次尝试显示的程序没有任何错误...但是当我下次单击搜索按钮并使用不同的属性进行搜索时...发生了问题...数据库中仍然存在私有值.
这是我的代码:

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:

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

推荐答案

让dataGridView附加到DataSet ds

只需使用语句ds.Clear();
Let the dataGridView is attached to the DataSet ds

Simply use the statment ds.Clear();


我知道最快的方法是在方法的最顶部执行此操作:

The fastest way I know of is to do this at the very top of your method:

dgvSearch.Rows.Clear();



为避免闪烁,您可能需要执行以下操作:



To avoid flickering, you might want to do this:

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

... your existing code...

dvgSearch.EndUpdate();



编辑(1票之后)=================

为什么对此进行1票表决(或对此事2票表决)?我告诉他如何清除行.执行搜索时,请清除现有的rowq,然后将它们与从搜索中获得的新数据重新添加在一起.即使他没有足够的经验来假设他每次搜索都返回相同的行数,这仍然是清除旧数据的最快方法,除非您正在使用数据绑定.重构经常是编程的本质.每个人越早承认这一点,每个人的生活就会越好.



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.


如你所说,它没有用.我认为您可能尚未更新GridView.

像这样

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



如果使用ASP.NET,请再添加一个类似的

GridViewX.DataBind();


肯定会工作..
祝你好运:)
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 :)


这篇关于如何清除datagidview的所有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆