TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


Forums >


DataGridView: Set value for columns having NULL value while updating database


10/31/2008 11:43:00 AM

I have a typed dataset assigned to a DataGridView. There are some
columns which can be null according to the table structure(this can't be
changed under any circumstances).

Now, when I am adding a new row using DataGridView, some columns remains
NULL. I want to set the value of these columns to Zero (numeric 0) when
updating the database.

For this I tried to handle the RowChanging event and set the value if
the value is DBNull. But it doesn't allow me to set the value. It throws
an exception.

How can I achieve this?

Thanks & Regards,
3 Answers


10/31/2008 2:36:00 PM


I assume that you need to replace 0 to all null values in the
DataGridView. Try this code. I have not tested but let me know.

int col = 0;
foreach (DataRow row in dataGridView1.Rows)
if (row[col] == null)
row[col] = 0;
if (col < 3)


11/3/2008 12:12:00 PM


This is NOT a solution! The reason
1) this code is not efficient if the number of rows is the order of
thousands or more.
2) This needs to be executed many times.
3) Doesn't work for new added rows

By the way, this did come to my mind before posting the question. Thanks

RP wrote:
> I assume that you need to replace 0 to all null values in the
> DataGridView. Try this code. I have not tested but let me know.
> int col = 0;
> foreach (DataRow row in dataGridView1.Rows)
> {
> if (row[col] == null)
> row[col] = 0;
> if (col < 3)
> col++;
> }


11/3/2008 1:31:00 PM


Hi Ashutosh,

Thank you for using Microsoft Managed Newsgroup Service, my name is Zhi-Xin
Ye, I'm assigned to help you on this issue.

From your description, you want to change the value to 0 for cells on the
nullable columns if they're null.

You can set the DefaultValue property to 0 for the DataColumn, then when
adding a new row in the DataGridView, the value on the nullable field would
be 0 by default.
You can also set the DefaultCellStyle.DataSourceNullValue property for the
DataGridViewColumn to 0, then when user input null in the nullable field, 0
will be saved instead of null.

For example:

private void Form1_Load(object sender, EventArgs e)
// TODO: This line of code loads data into the
'northwindDataSet.test' table. You can move, or remove it, as needed.

this.dataGridView1.Columns[4].DefaultCellStyle.DataSourceNullValue = 0;
this.northwindDataSet.test.Columns[4].DefaultValue = 0;

For more information, you can refer to these documents:

DataGridViewCellStyle.DataSourceNullValue Property

DataColumn.DefaultValue Property

If you have any problems or concerns, please feel free to let me know.

Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can

improve the support we provide to you. Please feel free to let my manager
know what you think of the level

of service provided. You can send feedback directly to my manager at:

Get notification to my posts through email? Please refer to


Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the

community or a Microsoft Support Engineer within 2 business day is
acceptable. Please note that each follow

up response may take approximately 2 business days as the support
professional working with you may need

further investigation to reach the most efficient resolution. The offering
is not appropriate for situations

that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working

with a dedicated Microsoft Support Engineer by contacting Microsoft
Customer Support Services (CSS) at

This posting is provided "AS IS" with no warranties, and confers no rights.