Omit DataGrid Columns

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way to limit the columns that are viewable in a datagrid?

The only thing I can think of is to create a new table and loop through the
original table to fill the new table. The problem with this is that if I
don't want the primary key to show, I'll lose ability to accurately update
the database.

For a simple example, how could I display the Northwind.Products table in an
editable datagrid, showing only the ProductName and UnitsInStock columns?

Thanks,

Eric
 
Hi Eric,

If you are talking about Web DataGrid, following can give
you some hint.

Actually it doesn't matter how many fields in sql query.
You can manually control which columns appear in the
DataGrid control by setting the AutoGenerateColumns
property to false and then listing the columns that you
want to include between the opening and closing <Columns>
tags. The columns specified are added to the Columns
collection in the order listed. This allows you to
programmatically control the columns in the DataGrid
control.

HTH


Elton Wang
(e-mail address removed)
 
Thanks for the reply, Tim. It works very well.

One question, though. If you decide to omit the primary key from the column
styles, how can you insert records using the DataGrid?

For example, I used Northwind and included only CompanyName, ContactName,
and Phone from the Customers table as my columns. Then when I try to insert
a record, I get this error message:

[Title bar] Error when committing the row to the original data store.
Column 'CustomerID' does not allow nulls. Do you want to correct the value?

Note that CustomerID is of SQL Server datatype nchar, so I can't rely on the
column to auto increment itself.

Thank you,

Eric
 
Nevermind, I just realized how stupid my question was.

If the CustomerID is character-based, then it probably has significance, so
it should be inserted along with the rest of the columns. Therefore, I
shouldn't have omitted it in the first place. :-)



Thanks for the reply, Tim. It works very well.

One question, though. If you decide to omit the primary key from the column
styles, how can you insert records using the DataGrid?

For example, I used Northwind and included only CompanyName, ContactName,
and Phone from the Customers table as my columns. Then when I try to insert
a record, I get this error message:

[Title bar] Error when committing the row to the original data store.
Column 'CustomerID' does not allow nulls. Do you want to correct the value?

Note that CustomerID is of SQL Server datatype nchar, so I can't rely on the
column to auto increment itself.

Thank you,

Eric



See the DataGridTableStyle class. An example is included in the docs.
http://msdn.microsoft.com/library/d...mwindowsformsdatagridtablestyleclasstopic.asp
--
Tim Wilson
.Net Compact Framework MVP

through
the
if
 
If you're inserting the record yourself then simply generate the ID for the
appropriate column in the new row before you add it to, let's say, the
DataTable. The DataGridTableStyle class merely allows you to hide certain
columns from being displayed in the DataGrid. So at this point your new ID
has been created and associated as the PK for the record, but the field is
not shown through the DataGrid. If, however, you want this ID value to be
generated for you when the user clicks on the last row of the DataGrid to
cause an insert, then all I can say is to try to see if the DataColumn's
Expression property can help. Short of that, assuming you're using a
DataTable, look at the events on the DataTable class to see if hooking into
any of those may allow you to catch the record before it's inserted into the
DataTable, allowing you to generate an ID. If all else fails, post this
question as a new thread and maybe someone else might have a more concrete
answer for you.

--
Tim Wilson
..Net Compact Framework MVP

Thanks for the reply, Tim. It works very well.

One question, though. If you decide to omit the primary key from the column
styles, how can you insert records using the DataGrid?

For example, I used Northwind and included only CompanyName, ContactName,
and Phone from the Customers table as my columns. Then when I try to insert
a record, I get this error message:

[Title bar] Error when committing the row to the original data store.
Column 'CustomerID' does not allow nulls. Do you want to correct the value?

Note that CustomerID is of SQL Server datatype nchar, so I can't rely on the
column to auto increment itself.

Thank you,

Eric



See the DataGridTableStyle class. An example is included in the docs.
http://msdn.microsoft.com/library/d...mwindowsformsdatagridtablestyleclasstopic.asp
--
Tim Wilson
.Net Compact Framework MVP

through
the
if
 
Yes, I was making reference to when the user clicks the last row in the
DataGrid to add a new row.

I did a little testing and found that the new record will add okay if it is
an auto-incrementing integer (which must also be declared in the DataSet's
designer for that particular column). Therefore, you do not need to display
your "usual" primary keys. But for the nonnumeric PKs, it looks easiest to
just display those to avoid a failed insert.

I will certainly look into the DataColumn's Expression property, but the
above concept works for now.

Once again, thank you so much for your help, Tim!

Eric


Tim Wilson said:
If you're inserting the record yourself then simply generate the ID for the
appropriate column in the new row before you add it to, let's say, the
DataTable. The DataGridTableStyle class merely allows you to hide certain
columns from being displayed in the DataGrid. So at this point your new ID
has been created and associated as the PK for the record, but the field is
not shown through the DataGrid. If, however, you want this ID value to be
generated for you when the user clicks on the last row of the DataGrid to
cause an insert, then all I can say is to try to see if the DataColumn's
Expression property can help. Short of that, assuming you're using a
DataTable, look at the events on the DataTable class to see if hooking into
any of those may allow you to catch the record before it's inserted into the
DataTable, allowing you to generate an ID. If all else fails, post this
question as a new thread and maybe someone else might have a more concrete
answer for you.

--
Tim Wilson
.Net Compact Framework MVP

Thanks for the reply, Tim. It works very well.

One question, though. If you decide to omit the primary key from the column
styles, how can you insert records using the DataGrid?

For example, I used Northwind and included only CompanyName, ContactName,
and Phone from the Customers table as my columns. Then when I try to insert
a record, I get this error message:

[Title bar] Error when committing the row to the original data store.
Column 'CustomerID' does not allow nulls. Do you want to correct the value?

Note that CustomerID is of SQL Server datatype nchar, so I can't rely on the
column to auto increment itself.

Thank you,

Eric



"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message
See the DataGridTableStyle class. An example is included in the docs.
http://msdn.microsoft.com/library/d...mwindowsformsdatagridtablestyleclasstopic.asp
if table
in
 
Glad to help.

--
Tim Wilson
..Net Compact Framework MVP

Yes, I was making reference to when the user clicks the last row in the
DataGrid to add a new row.

I did a little testing and found that the new record will add okay if it is
an auto-incrementing integer (which must also be declared in the DataSet's
designer for that particular column). Therefore, you do not need to display
your "usual" primary keys. But for the nonnumeric PKs, it looks easiest to
just display those to avoid a failed insert.

I will certainly look into the DataColumn's Expression property, but the
above concept works for now.

Once again, thank you so much for your help, Tim!

Eric


Tim Wilson said:
If you're inserting the record yourself then simply generate the ID for the
appropriate column in the new row before you add it to, let's say, the
DataTable. The DataGridTableStyle class merely allows you to hide certain
columns from being displayed in the DataGrid. So at this point your new ID
has been created and associated as the PK for the record, but the field is
not shown through the DataGrid. If, however, you want this ID value to be
generated for you when the user clicks on the last row of the DataGrid to
cause an insert, then all I can say is to try to see if the DataColumn's
Expression property can help. Short of that, assuming you're using a
DataTable, look at the events on the DataTable class to see if hooking into
any of those may allow you to catch the record before it's inserted into the
DataTable, allowing you to generate an ID. If all else fails, post this
question as a new thread and maybe someone else might have a more concrete
answer for you.

--
Tim Wilson
.Net Compact Framework MVP

Thanks for the reply, Tim. It works very well.

One question, though. If you decide to omit the primary key from the column
styles, how can you insert records using the DataGrid?

For example, I used Northwind and included only CompanyName, ContactName,
and Phone from the Customers table as my columns. Then when I try to insert
a record, I get this error message:

[Title bar] Error when committing the row to the original data store.
Column 'CustomerID' does not allow nulls. Do you want to correct the value?

Note that CustomerID is of SQL Server datatype nchar, so I can't rely
on
the
column to auto increment itself.

Thank you,

Eric



"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message

See the DataGridTableStyle class. An example is included in the docs.
http://msdn.microsoft.com/library/d...mwindowsformsdatagridtablestyleclasstopic.asp
 
Back
Top