none
clear datagridview

    Question

  • if there are records shown in a data gridview i want to remove them not from database only from datagrieview. on button click event whats wrong with code

                foreach (DataGridViewCell cell in dataGridView1.cells)

    {

       cell.Value = null;

    }

    Error    1    'System.Windows.Forms.DataGridView' does not contain a definition for 'cells' and no extension method 'cells' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?)  
    • Moved by Naomi N Sunday, August 25, 2013 11:33 PM Better answer can be here
    Sunday, August 25, 2013 12:56 PM

Answers

  • I agree with Sarat that your question is not on topic for this forum since it is related to the control methods rather than T-SQL.  But it looks like this may be a WinForm question rather than a Web application.  You can find the DataGridView reference online at http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx.  There you will find that the Rows collection (DataGridViewRowCollection) has a Clear method that you can use to remove all the rows.  I don't believe that will remove the rows from the database but I'm no expert in that area.


    Dan Guzman, SQL Server MVP, http://www.dbdelta.com

    Sunday, August 25, 2013 4:14 PM
  • Hi M Tayyab Mughal,

    Welcome to the MSDN forum.

    According to your description, you want to remove the records not from database only from datagrieview.

    I agree with Dan.

    You can try use the following code to achieve it.

    this.dataGridView1.Rows.Clear();

    If you have any question, just feel free to let me know.

    Regards,

    Damon


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, August 27, 2013 9:53 AM

All replies

  • Looks like the issue is related to Asp.Net , please post the question in appropriate forum for better and quick answers.

    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Sunday, August 25, 2013 3:41 PM
  • I agree with Sarat that your question is not on topic for this forum since it is related to the control methods rather than T-SQL.  But it looks like this may be a WinForm question rather than a Web application.  You can find the DataGridView reference online at http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx.  There you will find that the Rows collection (DataGridViewRowCollection) has a Clear method that you can use to remove all the rows.  I don't believe that will remove the rows from the database but I'm no expert in that area.


    Dan Guzman, SQL Server MVP, http://www.dbdelta.com

    Sunday, August 25, 2013 4:14 PM
  • Hi M Tayyab Mughal,

    Welcome to the MSDN forum.

    According to your description, you want to remove the records not from database only from datagrieview.

    I agree with Dan.

    You can try use the following code to achieve it.

    this.dataGridView1.Rows.Clear();

    If you have any question, just feel free to let me know.

    Regards,

    Damon


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, August 27, 2013 9:53 AM