|
|
|
|
|
Home
»
VB.NET
»
VB.NET Deleting Columns in the DataGrid Control
|
|
|
|
Author Rank :
|
|
|
Page Views :
|
5072
|
|
Downloads :
|
0
|
|
Rating :
|
Rate it
|
|
Level :
|
Beginner
|
|
|
Deleting Columns in the Windows Forms DataGrid Control
You can programmatically delete or hide columns in the Windows Forms DataGrid control by using the properties and methods of the GridColumnStylesCollection and DataGridColumnStyle objects (which are members of the DataGridTableStyle class).
The deleted or hidden columns still exist in the data source the grid is bound to, and can still be accessed programmatically. They are just no longer visible in the datagrid.
Note If your application does not access certain columns of data, and you do not want them displayed in the datagrid, then it is probably not necessary to include them in the data source in the first place.
To delete a column from the DataGrid programmatically
-
In the declarations area of your form, declare a new instance of the DataGridTableStyle class.
-
-
Add the new DataGridTableStyle object to the datagrid's table styles collection.
-
Call the RemoveAt method of the DataGrid's GridColumnStyles collection, specifying the column index of the column to delete.
' Declare a new DataGridTableStyle in the ' declarations area of your form. Dim ts As DataGridTableStyle = New DataGridTableStyle()
Sub DeleteColumn() ' Set the DataGridTableStyle.MappingName property ' to the table in the data source to map to. ts.MappingName = DataGrid1.DataMember
' Add it to the datagrid's TableStyles collection DataGrid1.TableStyles.Add(ts) ' Delete the first column (index 0) DataGrid1.TableStyles(0).GridColumnStyles.RemoveAt(0) End Sub
To hide a column in the DataGrid programmatically
-
In the declarations area of your form, declare a new instance of the DataGridTableStyle class.
-
Set the MappingName property of the DataGridTableStyle to the table in your data source that you want to apply the style to. The following code example uses the DataGrid.DataMember property, which it assumes is already set.
-
Add the new DataGridTableStyle object to the datagrid's table styles collection.
-
Hide the column by setting its Width property to 0, specifying the column index of the column to hide.
Dim ts As DataGridTableStyle = New DataGridTableStyle()
Sub HideColumn() ' Set the DataGridTableStyle.MappingName property ' to the table in the data source to map to. ts.MappingName = DataGrid1.DataMember
' Add it to the datagrid's TableStyles collection DataGrid1.TableStyles.Add(ts)
' Hide the first column (index 0) DataGrid1.TableStyles(0).GridColumnStyles(0).Width = 0 End Sub
|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|