Search
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Codes Author
Tue, 02-Sep-2014 Authors
Vishal@Neeraj
120
All Time Authors
Satyapriyanayak
12160
Jayakumars
8120
Niladri.biswas
6000

Latest members | More ...


(Statistics delayed by 5 minutes)
 Codes

Delete DataGridView Columns in RunTime using C#

T.saravanan
Posted by T.saravanan under Windows Forms on 9/4/2010 4:55:14 PM | Points: 40 | Views : 15413 | Status : [Member] [MVP]

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 :)


Thanks,
T.Saravanan
Found interesting? Add this to:



Comments or Responses


Login to post response