Home > Codes >

Delete DataGridView Columns in RunTime using C#

T.saravanan
Posted by T.saravanan under Windows Forms category on | Points: 40 | Views : 19315
Hi All,

Here i am given a code snippet for how to delete a DataGridView columns in RunTime.

//Assign a string object in Global

string sColumnName=string.Empty;

// For Column Selection
Private void datagridview_ColumnHeaderMouseClick(object sender,DataGridViewCellMouseEventArgs e)
{
if(DataGrdView1.Columns[e.ColumnIndex].Selected == false)
{
DataGridView1[e.ColumnIndex,0].Style.BackColor=Color.Red; \\ For Selected Column assign a Color
sColumnName=DataGridView1.Columns[e.ColumnIndex].Name.ToString(); \\ Set the Selected Column Name into string
}
}

// For Column Deletion
Private void DataGridView_KeyDown(object sender,keyEventArgs e)
{
if(e.Keycode == Keys.Delete)
{
DataGridView1.Columns.Remove(sColumnName); \\ Remove that Column in DataGridView
sColumnName=string.Empty;
}
}


If you want multiple Column deletion in single click means replace sColumnName into ArrayList Object.

I hope this code useful to delete a Columns in DataGridView.

Cheers :)

Comments or Responses

Login to post response