0
votes

I having a hard time trying to access hidden columns that been hidden in DataTable which later bind and accessed in DataGridView

In Details : dtResult is a DataTable Example Tried :

dtResult.Columns[i].ColumnMapping = MappingType.Hidden;

OR

dtResult.Columns.RemoveAt(i);

Then Store into DataSource of DataGridView :

dataGridView.DataSource = dtResult;

Accessing Hidden Columns occur error : "Columns 'Active' Cannot be found" :

dataGridView.SelectedRows[0].Cells["active"].Value.ToString()

So Is there are way for DataGridView to get Hidden Columns that been hidden in DataTable before binding it into DataGridView ?

Please help me~ . Thank you

FYI :

I just realized
That DataSource Binding doesn't include Hidden Columns of DataTable.
Means That If DataTable.Column.Count = 10. (2 Hidden)
DataGridView.DataSource = DataTable.
DataGridView.Column.Count == 8. 

So lead me to ask. How to Bind Those Hidden Column into the DataGridView From DataTable

2

2 Answers

1
votes

Why do you need to hide the column in the datatable ? Visability in UI is a UI concern, I would bind the the datatable to the datagridview and hide the desired columns in the datagridview.

0
votes

I know this is old but I had the same issue today so I am posting my solution. I was using DataSet.ReadXml(Stream) to dump an xml file into a DataSet and the columns that are generated for relations were not showing up in a DataGridView. The following code loops through all of the columns in each DataTable in the DataSet and changes the ColumnMapping property to MappingType.Element. I picked MappingType.Element since the visible columns were set to that and the description fits for my situation: "The column is mapped to an XML element."

Short answer: Change the ColumnMapping from MappingType.Hidden to something that is not hidden before binding to the DataGridView.

    ...
    DataSet ds;
    ...
    foreach(DataTable dtbl in ds.Tables)
    {
        foreach(DataColumn dcol in dtbl.Columns)
        {
            if (dcol.ColumnMapping == MappingType.Hidden)
            {
                dcol.ColumnMapping = MappingType.Element;
            }
        }
    }

    DataGridView.DataSource = ds;

Then if you need to hide them in UI you can do it on the DataGridView.