Thread: How To Clear A DataGridView? - VBForums

  • Register
  • Help
  • Remember Me?
VBForums - Visual Basic and VB .NET Discussions and More!
  • Advanced Search
  • Home
  • VBForums
  • Visual Basic
  • Visual Basic .NET
  • How to clear a DataGridView?
Results 1 to 22 of 22 Thread: How to clear a DataGridView? share-icon
  • Thread Tools
    • Show Printable Version
  • Display
    • Linear Mode
    • Switch to Hybrid Mode
    • Switch to Threaded Mode
  1. Apr 9th, 2007, 11:06 AM #1 dogboydelta
    • View Profile
    • View Forum Posts
    dogboydelta is offline

    Thread Starter Fanatic Member Join Date Jun 2005 Posts 625

    How to clear a DataGridView?

    How do I clear a DataGridView? Basically I want the user to be able to click the button again that fills it and it will erase everything and refresh the data.
    Reply With Quote Reply With Quote
  2. Apr 9th, 2007, 11:21 AM #2 Dean_Reedy
    • View Profile
    • View Forum Posts
    Dean_Reedy is offline Lively Member Join Date May 2000 Location Iowa USA Posts 118

    Re: How to clear a DataGridView?

    Try: myGrid.Rows.Clear
    Reply With Quote Reply With Quote
  3. Apr 9th, 2007, 11:33 AM #3 dogboydelta
    • View Profile
    • View Forum Posts
    dogboydelta is offline

    Thread Starter Fanatic Member Join Date Jun 2005 Posts 625

    Re: How to clear a DataGridView?

    Got an exception: Cannot clear this list .. ?
    Reply With Quote Reply With Quote
  4. Apr 9th, 2007, 11:38 AM #4 GaryMazzone
    • View Profile
    • View Forum Posts
    GaryMazzone is offline A SQL Server fool GaryMazzone's Avatar Join Date Aug 2005 Location Dover,NH Posts 7,493

    Re: How to clear a DataGridView?

    Try DataGridView.DataSource = Nothing
    Sometimes the Programmer Sometimes the DBA Mazz1
    Reply With Quote Reply With Quote
  5. Apr 9th, 2007, 11:44 AM #5 dogboydelta
    • View Profile
    • View Forum Posts
    dogboydelta is offline

    Thread Starter Fanatic Member Join Date Jun 2005 Posts 625

    Re: How to clear a DataGridView?

    Didn't work.
    Reply With Quote Reply With Quote
  6. Apr 9th, 2007, 11:52 AM #6 GaryMazzone
    • View Profile
    • View Forum Posts
    GaryMazzone is offline A SQL Server fool GaryMazzone's Avatar Join Date Aug 2005 Location Dover,NH Posts 7,493

    Re: How to clear a DataGridView?

    Was there an error? What was it if so.
    Sometimes the Programmer Sometimes the DBA Mazz1
    Reply With Quote Reply With Quote
  7. Apr 9th, 2007, 12:05 PM #7 dogboydelta
    • View Profile
    • View Forum Posts
    dogboydelta is offline

    Thread Starter Fanatic Member Join Date Jun 2005 Posts 625

    Re: How to clear a DataGridView?

    no error, just didn't clear anything.
    Reply With Quote Reply With Quote
  8. Apr 9th, 2007, 12:13 PM #8 GaryMazzone
    • View Profile
    • View Forum Posts
    GaryMazzone is offline A SQL Server fool GaryMazzone's Avatar Join Date Aug 2005 Location Dover,NH Posts 7,493

    Re: How to clear a DataGridView?

    This is what I do to clear and set a DataGridView with data: vb Code:
    1. Public Sub getComments()
    2. Me.dgComments.DataSource = Nothing
    3. Dim oDS As System.Data.DataSet
    4. Dim strSQL As String = String.Empty
    5. strSQL = "Select a.CommentPK,a.IssuePK,a.ContactPK,b.FirstName & "" "" & b.LastName AS [Comment By],a.CommentDate As [Date of Comment],a.Comments "
    6. strSQL &= "From CommentTable a, Contacts b "
    7. strSQL &= "Where a.ContactPK = b.ContactPK And "
    8. strSQL &= "a.IssuePK = " & intIssuePK.ToString() & " "
    9. strSQL &= "Order By CommentDate Desc"
    10. da = New clsDataAccess
    11. oDS = da.Return_DataSet(strSQL)
    12. If Not oDS Is Nothing Then
    13. Me.dgComments.DataSource = oDS.Tables(0)
    14. oDS.Dispose()
    15. da.Dispose()
    16. Me.dgComments.Columns(0).Visible = False
    17. Me.dgComments.Columns(1).Visible = False
    18. Me.dgComments.Columns(2).Visible = False
    19. Me.dgComments.Columns(3).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
    20. Me.dgComments.Columns(4).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
    21. Me.dgComments.Columns(5).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
    22. End If
    23. If mdlGeneral.errMessage.intErrNum <> 0 Then
    24. MessageBox.Show(mdlGeneral.errMessage.strMess.Trim(), "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    25. End If
    This is called from and other sub
    Sometimes the Programmer Sometimes the DBA Mazz1
    Reply With Quote Reply With Quote
  9. Apr 9th, 2007, 03:06 PM #9 Dean_Reedy
    • View Profile
    • View Forum Posts
    Dean_Reedy is offline Lively Member Join Date May 2000 Location Iowa USA Posts 118

    Re: How to clear a DataGridView?

    Since you are using a bound grid, have you tried to call your loading function twice and verified that the data is not doubling? Since it is bound, loading twice ought to not duplicate the data.
    Reply With Quote Reply With Quote
  10. Apr 9th, 2007, 07:51 PM #10 jmcilhinney
    • View Profile
    • View Forum Posts
    • Visit Homepage
    jmcilhinney is offline Super Moderator jmcilhinney's Avatar Join Date May 2005 Location Sydney, Australia Posts 110,796

    Re: How to clear a DataGridView?

    Some people are so forthcoming with information. Is this grid bound to a DataSource or not? If it is then what is it bound to, a DataTable or something else?
    Why is my data not saved to my database? | MSDN Data Walkthroughs VBForums Database Development FAQ My CodeBank Submissions: VB | C# My Blog: Data Among Multiple Forms (3 parts) Beginner Tutorials: VB | C# | SQL
    Reply With Quote Reply With Quote
  11. Apr 9th, 2007, 10:35 PM #11 steve_rm
    • View Profile
    • View Forum Posts
    steve_rm is offline Frenzied Member Join Date Dec 2001 Posts 1,331

    Re: How to clear a DataGridView?

    Hello, I think clearing the data source and not the datagrid would be a better idea. Steve
    steve
    Reply With Quote Reply With Quote
  12. Nov 5th, 2008, 11:35 PM #12 praveenbhosage
    • View Profile
    • View Forum Posts
    praveenbhosage is offline New Member Join Date Nov 2008 Posts 1

    Re: How to clear a DataGridView?

    BindingSource.Datasource=Empty Dataset; Datagridview.Datasource=BindingSource; Cheers, The Wolverine
    Reply With Quote Reply With Quote
  13. Oct 6th, 2011, 01:06 PM #13 A_hemati
    • View Profile
    • View Forum Posts
    A_hemati is offline New Member Join Date Oct 2011 Posts 1

    Re: How to clear a DataGridView?

    Just clear your dataset like this: dataset1.Clear()
    Reply With Quote Reply With Quote
  14. Oct 24th, 2011, 06:09 PM #14 Svilen Zhelev
    • View Profile
    • View Forum Posts
    Svilen Zhelev is offline New Member Join Date Oct 2011 Posts 1

    Re: How to clear a DataGridView?

    Just declare Dataset in the click event of the button. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ds1 As New DataSet .........
    Reply With Quote Reply With Quote
  15. Oct 25th, 2011, 06:24 AM #15 .paul.
    • View Profile
    • View Forum Posts
    • Visit Homepage
    .paul. is offline eXtreme Programmer .paul.'s Avatar Join Date May 2007 Location Chelmsford UK Posts 25,755

    Re: How to clear a DataGridView?

    this thread is 4.5 years old. it might be resolved by now
    • Coding Examples: My Web Site | CodeBank Submissions
    • Features: Sorting Techniques | DataGridView Printing | Average Strategies - Plugin Version
    • Online Games: hangMan | masterMind | sudoku | crack the lock | numbers game | Fit Words | Fit Numbers | Crossword Hangman
    • Compiled Games: Puzzle Games
    Reply With Quote Reply With Quote
  16. Oct 25th, 2011, 06:35 AM #16 Joshlad
    • View Profile
    • View Forum Posts
    Joshlad is offline Addicted Member Joshlad's Avatar Join Date May 2011 Location England Posts 160

    Re: How to clear a DataGridView?

    hahaha
    Quote Originally Posted by Shaggy Hiker View Post EDIT: Wow, I think I need to refresh my browser faster. You had it solved before I even started writing. May not be able to provide code, but i sure can provide the logic AutoTyper Moves Mouse Cursor to Screen Position All my Projects contain flashy scrolling text
    Reply With Quote Reply With Quote
  17. Feb 16th, 2012, 08:21 PM #17 abidquraishi
    • View Profile
    • View Forum Posts
    abidquraishi is offline New Member Join Date Feb 2012 Posts 1

    Re: How to clear a DataGridView?

    Quote Originally Posted by dogboydelta View Post How do I clear a DataGridView? Basically I want the user to be able to click the button again that fills it and it will erase everything and refresh the data. When you want to clear DataGridView rows and trying to show another records from the record selection query; Simply write the following code: First Clear the DataSet and then select that cleared dataset for your query ds.Clear() DataGridView.DataSource = ds
    Reply With Quote Reply With Quote
  18. Mar 4th, 2012, 12:52 PM #18 Manolis
    • View Profile
    • View Forum Posts
    Manolis is offline New Member Join Date Mar 2012 Posts 1

    Re: How to clear a DataGridView?

    Usually setting Datasource to nothing is enough. You may use all three statements as well. Datagridview1.Datasource = nothing Datagridview1.Rows.clear Datagridview1.Columns.clear Manolis
    Reply With Quote Reply With Quote
  19. Jan 17th, 2017, 08:22 AM #19 Trubadur
    • View Profile
    • View Forum Posts
    Trubadur is offline New Member Join Date Jan 2017 Posts 3

    Re: How to clear a DataGridView?

    My vb does not accept Datagridview1.Rows.clear... so this is one solution: Datagridview1.Datasource = nothing datagridview1.refresh
    Reply With Quote Reply With Quote
  20. Mar 21st, 2020, 01:37 PM #20 LordAlex
    • View Profile
    • View Forum Posts
    LordAlex is offline New Member Join Date Mar 2020 Posts 1

    Re: How to clear a DataGridView?

    I know it might be a bit long-winded, but maybe you could try something along the lines of: Grid.Rows(x).Cells(y).Value = "" and repeat for all the rows and cells Just an idea
    Reply With Quote Reply With Quote
  21. Mar 21st, 2020, 02:09 PM #21 wes4dbt
    • View Profile
    • View Forum Posts
    wes4dbt is offline PowerPoster Join Date Sep 2005 Location Modesto, Ca. Posts 5,279

    Re: How to clear a DataGridView?

    This question is over 3yrs old???
    Reply With Quote Reply With Quote
  22. Mar 22nd, 2020, 10:24 AM #22 jmcilhinney
    • View Profile
    • View Forum Posts
    • Visit Homepage
    jmcilhinney is offline Super Moderator jmcilhinney's Avatar Join Date May 2005 Location Sydney, Australia Posts 110,796

    Re: How to clear a DataGridView?

    Quote Originally Posted by wes4dbt View Post This question is over 3yrs old??? The thread is 13 years old. It has been resurrected five times by people making their first post. Apparently, this thread is a magnet for would-be heroes who probably haven't actually read any of the previous posts.
    Why is my data not saved to my database? | MSDN Data Walkthroughs VBForums Database Development FAQ My CodeBank Submissions: VB | C# My Blog: Data Among Multiple Forms (3 parts) Beginner Tutorials: VB | C# | SQL
    Reply With Quote Reply With Quote
Quick Navigation Visual Basic .NET Top
  • Site Areas
  • Settings
  • Private Messages
  • Subscriptions
  • Who's Online
  • Search Forums
  • Forums Home
  • Forums
  • Visual Basic
    1. Visual Basic .NET
      1. CodeBank - VB.net
    2. Visual Basic 6 and Earlier
      1. CodeBank - Visual Basic 6 and earlier
    3. TwinBASIC
      1. CodeBank - TwinBASIC
    4. Universal Windows Platform and Modern Windows Experience
    5. Xamarin
    6. Mobile Development
    7. ASP, VB Script
    8. Office Development
    9. Database Development
    10. Reporting
    11. API
    12. Games and Graphics Programming
      1. Game Demos
    13. COM and ActiveX
    14. Network Programming
    15. Visual Basic FAQs
    16. Slow Chat with the Microsoft Visual Basic team
  • .NET and More
    1. ASP.NET And ASP.NET Core
    2. Visual Basic .NET
    3. MVC .Net
    4. C#
    5. Microsoft Azure and Cloud Dev
    6. WPF, WCF, WF
    7. .NET Architecture and Design
    8. Silverlight
  • General
    1. General Developer Forum
    2. IoT, IoE, and Maker Forum
    3. Testers and Testing
    4. Application Testing
    5. Application Deployment
    6. Linux Development
    7. General PC
    8. VBForums Coding Contests
      1. Contest Entries
    9. Code It Better
    10. Maths Forum
  • Other Languages
    1. Other BASIC
    2. C and C++
    3. Java
    4. PHP
    5. XML, HTML, Javascript, Web and CSS
    6. jQuery
    7. Assembly
    8. Other Programming Languages
  • VBForums CodeBank
    1. CodeBank - Visual Basic .NET
    2. CodeBank - Visual Basic 6 and earlier
    3. CodeBank - ASP / ASP.NET / Blazor / MVC / Web API
    4. CodeBank - C#
    5. CodeBank - C++
    6. CodeBank - Java / J#
    7. CodeBank - PHP
    8. Codebank - Game Programming
    9. Codebank - Mobile Development
    10. CodeBank - JavaScript
    11. Codebank - Cascading Style Sheets (CSS)
    12. CodeBank - Other
  • VBForums UtilityBank
    1. UtilityBank - Utilities
    2. UtilityBank - IDE Add-Ins
    3. UtilityBank - Components
    4. UtilityBank - Tutorials
    5. UtilityBank - Other
  • Projects
    1. Project Requests
    2. Project Communication Area
  • Jobs
    1. Just VB Jobs
    2. Open Positions (Jobs)
    3. Looking for Work
  • Community
    1. Forum Feedback
    2. General Discussion / Chit Chat
      1. World Events
    3. Forum Test Area
« Previous Thread | Next Thread »
  • Home
  • VBForums
  • Visual Basic
  • Visual Basic .NET
  • How to clear a DataGridView?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is On
  • HTML code is Off

Forum Rules

Click Here to Expand Forum to Full Width
Terms of Service | About Us | Privacy Notice | Contact Us | Advertise | Sitemap| California - Do Not Sell My Info

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.

All times are GMT -5. The time now is 11:27 AM.

Từ khóa » Visual Basic Clear Gridview