List of Exercises

Add Columns in DataGridView c# winform

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

    private void add_Click(object sender, EventArgs e)
    {
      if (columns_num.Text != "")
      {
        int num_columns = Convert.ToInt16(columns_num.Text);
        if (num_columns > 0)
        {
          //===Delete these two line if you want to insert columns
          if (dataGridView1.Columns.Count > 0)
            dataGridView1.Columns.Clear();

          for (int i = 0; i < num_columns; i++)
          {
            dataGridView1.Columns.Add("Col_" + (i + 1).ToString(), "Column" + (i + 1).ToString());
          }
        }
        else
        { MessageBox.Show("The number of columns should be more than zero!"); }
      }
      else
      { MessageBox.Show("Enter number of columns!"); }
    }
  }
}

	


Download the project of Visual Studio 2013 in DropBox Download


C# Add or Insert Columns in a DataGridView by user(in Runtime)


Video Thumbnail