Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Null
 

Null

I am populating a datagrid from a existing sql express database

A field named "Comment1" has NULL values in by defualt

I try to update Comment1 using

dstassessmentEdit.Tables("AttainmentEdit").Rows(nrow - 1).Item("Comment1") = "Testdatanew"

This will not work because the value in theComment1 fieldis null

If I manually replace the null value in the field Comment1with text "Testdataold" and then re-run the above code its overwrites as you would expect.

I do not have authority to have the nulls replace as the database is provided by a third party

Many thanks

fogmajor  Monday, May 26, 2008 4:14 PM

Hi fogmajor,

Based on my understanding, what you want to do is to show other text in the grid instead of null, right? If you are using the DataGrid in .Net Framework 1.1, you can set the NullText of DataGridColumnStyle object to format the displaying of the DataGrid. Here is a sample for your information.

Code Snippet

DataTable dt = new DataTable("mytable");

dt.Columns.Add("aa");

dt.Columns.Add("bb");

for (int i = 0; i < 5; i++)

{

dt.Rows.Add("aa" + i, "bb" + i);

}

dt.Rows.Add(DBNull.Value, DBNull.Value);

dt.Rows.Add(DBNull.Value, DBNull.Value);

dt.Rows.Add(DBNull.Value, DBNull.Value);

dt.AcceptChanges();

DataGridTableStyle ts = new DataGridTableStyle();

ts.MappingName = "mytable";

DataGridTextBoxColumn tbcol1 = new DataGridTextBoxColumn();

tbcol1.MappingName = "aa";

tbcol1.NullText = "mytext";

tbcol1.HeaderText = "aa";

ts.GridColumnStyles.Add(tbcol1);

DataGridTextBoxColumn tbcol2 = new DataGridTextBoxColumn();

tbcol2.MappingName = "bb";

tbcol2.HeaderText = "bb";

ts.GridColumnStyles.Add(tbcol2);

this.dataGrid1.TableStyles.Add(ts);

this.dataGrid1.DataSource = dt;

If you mean the DataGridView in .Net Framework 2.0, you can the NullValue of DataGridViewCellStyle object to format the displaying of the DataGridView.

Code Snippet

DataTable dt = new DataTable("mytable");

dt.Columns.Add("aa");

dt.Columns.Add("bb");

for (int i = 0; i < 5; i++)

{

dt.Rows.Add("aa" + i, "bb" + i);

}

dt.Rows.Add(DBNull.Value, DBNull.Value);

dt.Rows.Add(DBNull.Value, DBNull.Value);

dt.Rows.Add(DBNull.Value, DBNull.Value);

dt.AcceptChanges();

this.dataGridView1.DataSource = dt;

this.dataGridView1.Columns[0].DefaultCellStyle.NullValue = "mytext";

Hope this helps.
Best regards.
Rong-Chun Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Rong-Chun Zhang  Friday, May 30, 2008 7:23 AM

Hi fogmajor,

Based on my understanding, what you want to do is to show other text in the grid instead of null, right? If you are using the DataGrid in .Net Framework 1.1, you can set the NullText of DataGridColumnStyle object to format the displaying of the DataGrid. Here is a sample for your information.

Code Snippet

DataTable dt = new DataTable("mytable");

dt.Columns.Add("aa");

dt.Columns.Add("bb");

for (int i = 0; i < 5; i++)

{

dt.Rows.Add("aa" + i, "bb" + i);

}

dt.Rows.Add(DBNull.Value, DBNull.Value);

dt.Rows.Add(DBNull.Value, DBNull.Value);

dt.Rows.Add(DBNull.Value, DBNull.Value);

dt.AcceptChanges();

DataGridTableStyle ts = new DataGridTableStyle();

ts.MappingName = "mytable";

DataGridTextBoxColumn tbcol1 = new DataGridTextBoxColumn();

tbcol1.MappingName = "aa";

tbcol1.NullText = "mytext";

tbcol1.HeaderText = "aa";

ts.GridColumnStyles.Add(tbcol1);

DataGridTextBoxColumn tbcol2 = new DataGridTextBoxColumn();

tbcol2.MappingName = "bb";

tbcol2.HeaderText = "bb";

ts.GridColumnStyles.Add(tbcol2);

this.dataGrid1.TableStyles.Add(ts);

this.dataGrid1.DataSource = dt;

If you mean the DataGridView in .Net Framework 2.0, you can the NullValue of DataGridViewCellStyle object to format the displaying of the DataGridView.

Code Snippet

DataTable dt = new DataTable("mytable");

dt.Columns.Add("aa");

dt.Columns.Add("bb");

for (int i = 0; i < 5; i++)

{

dt.Rows.Add("aa" + i, "bb" + i);

}

dt.Rows.Add(DBNull.Value, DBNull.Value);

dt.Rows.Add(DBNull.Value, DBNull.Value);

dt.Rows.Add(DBNull.Value, DBNull.Value);

dt.AcceptChanges();

this.dataGridView1.DataSource = dt;

this.dataGridView1.Columns[0].DefaultCellStyle.NullValue = "mytext";

Hope this helps.
Best regards.
Rong-Chun Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Rong-Chun Zhang  Friday, May 30, 2008 7:23 AM
Many thanks for your prompt reply
Worked like a dream

Thanks you

Regards
Andrew

fogmajor  Saturday, May 31, 2008 7:08 AM

You can use google to search for other answers

Custom Search

More Threads

• Adding commands and modifying strongly typed datasets?
• Combo Box problem
• HeaderClick to select all rows in DGV
• DataGridviews Button Click ./&*
• Log Shipping
• datagridview erroricon shows on current cell but no tooltip?
• local data
• What is this control?
• RadioButton.DataBindings
• problem while handling dataerror