The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
[Originally posted by jdadamo]

Can a datagrid be configured to display only selected columns from a datatable
datasource? I'd rather not create a second dataset and datatable to hold only
the columns I want to display. From reading the book it sounds like there's
no direct control over the columns displayed in a dataview either so that
doesn't seem to be an option. The book talks about using a "Property Builder"
tool to configure webform datagrids but I couldn't find anything on using the
Property Builder with winform datagrids. Is there a Property Builder for this
or is there some other way to configure which columns are displayed in a

thanks - jeff.
import-bot (20211) [Avatar] Offline
Re: DataGrid - Displaying Columns
[Originally posted by arlen]

It is possible, but is something of a pain. You can create custom column
styles and add and set them up manually - then you only bind to the columns
you have specified.

A quick and dirty way to do this is to have the column styles automatically
created, then set the width of the columns you want to hide to 0 (you can also
remove them from the collection, but you can get strange behaviors - probably
fixable but I haven't drilled down enough to figure out the details). Here is
a code snippet:

// Define the data table
private DataTable m_dataTable = new DataTable("Test");

private void Form1_Load(object sender, System.EventArgs e)
// Create a DataTable with 3 columns and two rows.
DataRow row1 = m_dataTable.NewRow();
row1["Col1"] = "Value 1";
row1["Col2"] = "Hide me";
row1["Col3"] = "Value 3";
DataRow row2 = m_dataTable.NewRow();
row2["Col1"] = "Value 1B";
row2["Col2"] = "Hide me too";
row2["Col3"] = "Value 3B";

// Set the datasource for the data grid
dataGrid1.DataSource = m_dataTable;

// Create a table style, set its source to match the DataTable, then
// add it to the grid's table styles - order is important here, and
// setting the mapping name is important. By doing this, the column styles
// will automatically be created as defaults pointing to the columns that
// exist (alternatively, you can create each column style manually and add
// for the columns you want - but you must add the column styles _before_
// add the table style to the grid. (That is more flexible, but more work).
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = "Test";

// Now just set the column style for the column to hide to 0 width - for
// all intents and purposes hiding it.
dgts.GridColumnStyles["Col1"].Width = 0;

Hope this helps,