Sections
     
     
WWWCoder.com Resource Directory

Hiding DataGrid Columns
6/27/2005 12:27:20 PM

In this article we will discuss how to write C# code to hide datagrid columns based on a user click.

User Interface:

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

 datagrid display

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.


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 2/16/2009 3:40:25 AM by Anonymous
Comments: waste
Left on 10/28/2008 7:43:01 PM by Anonymous
Comments: Hi AzamSharp you wrote the line
myDataGrid.Columns[columnIndex].Visible = false;

but I think in c# there is no defination for columns in'System.Windows.Forms.DataGrid' does not contain a definition for 'Columns'

No ratings available.
Left on 5/20/2008 9:32:26 AM by Anonymous
Comments: thank you!  I was actually working on something entirely different, and this helped me greatly!
Left on 8/17/2007 5:32:27 AM by Anonymous
Comments: good one thanks,this is what i was searching for
Left on 6/25/2007 8:06:31 PM by Anonymous
Comments: GREAT!!
Left on 12/8/2006 1:35:28 AM by Anonymous
Comments: hai
Left on 9/14/2006 10:57:50 PM by Anonymous
Comments:
Left on 3/21/2006 9:31:18 AM by Anonymous
Comments: hey thanks buddy
Left on 1/25/2006 4:24:31 PM by Anonymous
Comments: Told me exactly what I needed to know.  Thank you!
     
     

 

     
     

 


 


Digg This
 


DotNetNuke Platinum Benefactor

     
     

Other family network sites: santry.com - katieandkarleigh.com

Powered by 

 

     
Copyright 20010 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970
Privacy Statement | Terms Of Use