User Interface:
The User Interface is
pretty simple which contains link buttons inside a Datagrid controls and an
independent Button control.

When we click on the Hide
link inside the column header that particular column will disappear. And when we
click on the "View all Columns" button all the columns of the Datagrid will
appear.
ItemCommand Event:
Since the link buttons are
inside the Datagrid we must somehow catch the events generated by those buttons.
The best place to catch all of these events is inside the ItemCommand event. You
should set the "CommandName" property of link button control to some text that
will distinguish the different events generated by different controls inside
Datagrid.
|
LinkButton lb = new LinkButton();
lc.Text = "<B>" + columnName + "</B>";
lb.Text = "Hide";
lb.CommandName = "HideColumn"; |
Now let's see that how we
can capture these events and take appropriate action when these events are
generated.
|
private
void myDataGrid_ItemCommand(object
source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName
== "HideColumn")
{
LinkButton l = new LinkButton();
l
= (LinkButton) e.CommandSource;
int
itemIndex = l.Parent.Parent.Controls.IndexOf(l.Parent);
// Send the column id to make that column invisible
MakeColumnInvisible(itemIndex);
}} |
Inside the "ItemCommand"
event of the Datagrid we are checking that if the event is generated by the link
button. If it is than we find the column index where the link button is clicked
and we send the column index to our custom made method "MakeColumnInvisible(itemIndex)".
Let's see the
MakeColumnInvisible method.
|
// Method to make the column invisible
private
void MakeColumnInvisible(int
columnIndex)
{
myDataGrid.Columns[columnIndex].Visible = false;
} |
Inside the
MakeColumnInvisible method we simply use the columnIndex and make the particular
column invisible.
That's it.
You must be wondering how
I make the columns visible again. This is even more simpler, I just call the
BuildDataGrid method and that's it.
|
// Event raised to view all the columns
private
void Button1_Click(object
sender, System.EventArgs e)
{
BuildDataGrid();
} |
I hope you like the
article, happy coding.
download project files
About the Author:
My name is Mohammad
Azam. You might know me as "AzamSharp". I started programming at the
age of 13 with GW BASIC as my first programming language. Later I
switched to C programming language and now I exclusively develop solutions in
C#.
I have written several articles for many websites. Some of them are also
published on Microsoft Official Website
www.asp.net. You will find most of my articles on
www.azamsharp.net.
I spend most my time reading .net articles and posting solutions to questions
on asp.net forums. I am also Top 25 poster on Asp.net forums. Currently I am an
undergraduate student in University of Houston. I will be graduating in
Fall 2005. I am also working as an Asp.net developer for my university.
My hobbies include listening music, playing video games and walk on a sunny day.
Recently I got engaged to the most beautiful girl in the world which is the best
thing ever happened to me. I would also like to thank my parents for all their
support and love they have given me on every step of life.
You can contact me at azamsharp@gmail.com.