I’ve been working on a project where we use only one GridView to display results for different Linq results.
This gridview has:
AutoGenerateColumns = true;
There is a Linq result that we want to use as a datasource.
In this result we have columns that we’d like to use as DataKeys.
When we bind results to our GridView, we don’t want to show these columns to users.
Here’s an example:
gvMyGrid.DataSource = from finalRows in summaryReport
select new
{
finalRows.a,
finalRows.b,
finalRows.c,
finalRows.d,
finalRows.e
};
gvMyGrid.DataKeyNames = new string[] { "a", "b" };
gvMyGrid.DataBind();
So after this binding is done, we still see “a” and “b” as columns.
we can use “a” and “b” when we call:
gvReport.Datakeys
but, we don’t want to see them on gridview.
So my question is, how can we hide these columns but still use them as datakeys?
Thank you.
I don’t think there is a direct way of doing this using DataKeys if
AutoGenerateColumns = true;. You can try this way. before binding the datasource make sure your DataKey columns are always on the same position in the collection. say datakey “a” is column index 1 and say datakey “b” is column index 2. So whether your collection returns 10 columns or 20 columns still “a” and “b” should be in index 1 and 2. Then you can do this to hide the columns.GridView will be like