default cell values in bound DataGridView

Posts   
 
    
BFynewever
User
Posts: 10
Joined: 19-Dec-2007
# Posted on: 14-Feb-2008 16:12:46   

LLBLGen Pro Version: 2.5 Final, December 5, 2007 Runtime Library Version: 2.5.7.1214 Code Generation: C#, .NET 2.0, SelfServicing Database: SQL Server 2005

In a WinForms application I have a DataGridView that is bound to an EntityCollection via a BindingSource. The DataGridView is auto-generating its columns based on the EntityCollection.

The problem I'm having is that when a new row is added to the grid, the cell value for columns that are bound to integer fields in the EntityCollection is automatically being defaulted to 0, and the cell value for columns that are bound to decimal fields is automatically being defaulted to 0.0. I don't want this to happen because 0 is not a valid value for the fields per my business logic. I'd like the cell to be null or blank to begin with. How can I change this behavior?

Thanks. Bret

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 15-Feb-2008 09:24:03   

Are these nullable types (int?, decimal?) ? They should be to allow for null values.

BFynewever
User
Posts: 10
Joined: 19-Dec-2007
# Posted on: 15-Feb-2008 17:03:11   

No, they are not nullable types. The values are required in the database, so the types of the generated entity fields are Int32 and Decimal.

So does the entity class provide the defaults to the DataGridView that it is bound to? Or does the DataGridView automatically provide defaults based on the type of the entity field that each column is bound to? I guess I don't understand how this works, and I haven't been able to find any documentation that tells me.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 16-Feb-2008 03:34:57   

This thread could be helpful: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7953

You can indicate the Datagrid to default null-values, however the underlying datasource doesn't know how to setup that, unless your fields would be nullables.

Have you tried with the property someColumn.DefaultCellStyle.DataSourceNullValue?

David Elizondo | LLBLGen Support Team
BFynewever
User
Posts: 10
Joined: 19-Dec-2007
# Posted on: 05-Mar-2008 22:28:22   

Yes, I've tried setting the following properties of the column:

DefaultCellStyle.NullValue DefaultCellStyle.DataSourceNullValue CellTemplate.DefaultNewRowValue via DefaultValuesNeeded event

but nothing seems to keep the grid from defaulting columns bound to int with "0" and columns bound to decimal with "0.0".

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 06-Mar-2008 10:17:00   
        private void dataGridView4_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
            e.Row.Cells[2].Value = "1.5";
        }

I just used the above code and it worked.

The DataGridView displayed rows from the OrderDetails table of the Northwind database. The third cell (index=2) displays the UnitPrice(decimal). And after using the above code it defaulted to 1.5 when adding a new row to the grid.

Hint: If you are automatically creating the columns, then don't use column names in the above event handler, instead use column indexs. e.Row.Cells[2].Value