How do I hide first column in every DataGridView using a CheckBox?

  • Thread starter Thread starter Jonas Andersson
  • Start date Start date
J

Jonas Andersson

Guest
How do I hide the first column for all the DataGridViews in the tabs?

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
tabControl1.TabPages.Clear();
createGridViews();
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
dgwDynamic.Columns["Column1"].Visible = false;
}

DataGridView dgwDynamic;
private void createGridViews()
{
int pageNumber = 0;
for (int i = 0; i < 3; i++)
{
/* Dynamic DataGridView */
dgwDynamic = new DataGridView();
dgwDynamic.Name = "DataGridView" + i.ToString();
dgwDynamic.ScrollBars = ScrollBars.Both;
dgwDynamic.Dock = DockStyle.Fill;

/* Dynamic TabPage */
TabPage tpDynamic = new TabPage();
pageNumber = i + 1;
tpDynamic.Name = "tabPage" + pageNumber.ToString();

string tabName = "TabPage" + pageNumber.ToString();

tpDynamic.Text = tabName;

tpDynamic.TabIndex = i;
// One newGridView per newTabPage
tpDynamic.Controls.Add(dgwDynamic); // Add Dynamic DataGridView to Dynamic TabPage
tabControl1.Controls.Add(tpDynamic); // Add Dynamic TabPage to TabControl
dgwDynamic.DataSource = GetTable(); // Add DataTable to Dynamic DataGridView

}//End for loop
}

private static DataTable table;
static DataTable GetTable()
{
// Here we create a DataTable with four columns.
table = new DataTable();
table.Columns.Add("Column1", typeof(string));
table.Columns.Add("Column2", typeof(string));
table.Columns.Add("Column3", typeof(string));

// Here we add five DataRows.
table.Rows.Add("Col1_Row1", "Col2_Row1", "Col3_Row1");
table.Rows.Add("Col1_Row2", "Col2_Row2", "Col3_Row2");
table.Rows.Add("Col1_Row3", "Col2_Row3", "Col3_Row3");
table.AcceptChanges();
return table;
}
}
I want to hide the first column for every DGW with one click? By the way, how do I also display the columns again when uncheck the CheckBox? Or maybe it would be more logical to hide the columns from the beginning and then check the CheckBox to have them displayed (so true=show columns, false=hide columns).

Continue reading...
 
Back
Top