Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9120313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:35:59+00:00 2026-06-17T05:35:59+00:00

I have a DataGridView that is bound to a DataTable. I later add a

  • 0

I have a DataGridView that is bound to a DataTable.

I later add a new button column directly to the DGV. The next time I refresh the table I want to clear all the previous data from the DGV.

For the table I just do var table = new DataTable();
but doing this with the DataGridView when the DGV is defined as a local inside the method causes it to never display on the form.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //dataGridView1 = new DataGridView(); //<-- uncommenting this line breaks the form's dgv from displaying anything

            DataTable table = new DataTable();
            table.Columns.Add("C_" + table.Columns.Count);
            table.Rows.Add("R1");
            table.Rows.Add("R2");
            dataGridView1.DataSource = table;

            DataGridViewButtonColumn oCol = new DataGridViewButtonColumn();
            oCol.Name = "Buttons";
            oCol.Text = "(...)";
            oCol.UseColumnTextForButtonValue = true;
            dataGridView1.Columns.Add(oCol);
        }
    }
}

Is this a bug or how should I refresh/reset/clear a dgv properly?

EDIT:

Code snippet above has been edited from the original. Uncomment the line in the code to see the different behaviour of button1 when in RunMode.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:36:00+00:00Added an answer on June 17, 2026 at 5:36 am
    dataGridView1.DataSource = null;
    

    or you could opt to clear the columns/rows.

    dataGridView1.Rows.Clear();
    dataGridView1.Columns.Clear();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGridView that is bound to a DataTable, it has a column
I have a DataGridView which is bound to a DataTable that contains a column
I have a DataGridView bound to a DataTable that has 1+16 columns defined as
I have a DataGridView that is bound to a DataTable, is there a way
I have a datagridview bound from a datatable. I have a calculated column linetotal
I have a Datagridview that is bound to a DataTable. I have added DataGridViewComboBoxColumn
I have a DataGridView that is bound to a list of objects called BaseChange.
I have a DataGridView that's bound to a DataSet. It has columns DateCreated, Weight,
I Have a Datagridview bound to a datatable wich hold data from a database
I have a datatable bound to a winforms dataGridView via a BindingSourceControl. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.