Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > hide column in datagrid
 

hide column in datagrid

how do i hide a column in datagrid.I have used the datagridtablestyle collection however the code does not seem to workcould you help me out with the same please.
thanks,
Supriya
Supriya  Tuesday, August 09, 2005 4:30 AM
The best you can do is set the width of a column to 0.

-mark
Program Manager
Microsoft
This post is provided "as-is"
Mark Rideout  Tuesday, August 09, 2005 4:55 PM

Hi,

Use the, DataViewColumn.Visible property:

dataGridView1.Columns["Col2"].Visible = false;

 

cheers,

Paul June A. Domag

Paul Domag  Tuesday, August 09, 2005 5:36 AM
Hi,
I am not using a datagridView control.I am using Datagrid control.So theres no columns collection in Datagrid control.
I tried to use the DatagridTableStyle collection which is not working at all.
could you give me sample code as to how to hide a column in Datagrid control.
Thanks,
Supriya
Supriya  Tuesday, August 09, 2005 5:42 AM
The best you can do is set the width of a column to 0.

-mark
Program Manager
Microsoft
This post is provided "as-is"
Mark Rideout  Tuesday, August 09, 2005 4:55 PM

how to make sure only the columns for the ItemTemplate specified should be displayed in a normal mode and in edit mode more columns should be displayed.

<asp:TemplateField ..>

<ItemTemplate>

...

</ItemTemplate>

<EditItemTemplate>

...

</EditItemTemplate>

</asp:TemplateField>

<asp:TemplateField ..>

<EditItemTemplate>

...

</EditItemTemplate>

</asp:TemplateField>

Both columns will be displayed in both normal and in edit node, even if we don’t specify an ItemTemplate for the other TemplateField. In some cases you probably want to display few columns in normal mode, and some more columns in edit mode. By using the RowDataBound event of the GridView, we can programmatically hide columns. So if we have a GridView with 4 columns (including the edit buttons etc) and want to hide the last two columns, we can do something like this in the RowDataBound event:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (GridView1.EditIndex > 0)

return;

if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) &&

(e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))

{

e.Row.Cells[2].Visible = false;

e.Row.Cells[3].Visible = false;

}

}

  • Proposed As Answer byRenju.R Friday, April 24, 2009 8:55 AM
  •  
Renju.R  Monday, March 19, 2007 5:51 AM

You can use google to search for other answers

Custom Search

More Threads

• error in datagridview????
• in windows application i am using process.start(iexplorer,"http://www.google.com")
• Control formatting with Enabled=False
• Creating a XSD and XML file for storage.
• Dataset, ManagementObjectSearcher and WMI query - indexed property problems
• DataGridView - Adding data & Default values
• How to set value? control.DataBindings["Text"].BindingManagerBase.Current = ?
• DataTable.Copy does not update bound textbox
• DataGridView does not auto generate column of some table.
• Binding navigator - new default values