none
DataGridView. Set Column Type to notNull

    Question

  • Hi to Everyone,

    Is it possible to set a datagridview column value behavior to not null?

    When a returned from DataAdapter (after SQL statement) datatable assigned to datagridview datasource property, then datagridview changes its column values according to DB table description: If the column is not nullable - datagridview controls the value itself.

    Is it possible to do the same by setting a property of the datagridview object, without using EndEdit and such?

     

    Thanks

    Friday, July 09, 2010 10:14 AM

Answers

  • Try this DataGridView1.Columns("ColumnName").DefaultCellStyle.NullValue = "Default Value"
    Friday, July 09, 2010 10:26 AM
  • Hi Robbirk,

       Welcome to MSDN forums! I'm glad to see your active participation and discussion in MSDN forums. Based on your issue 

      Set a datagridview column value behavior to not null?

        dataGridView1.Columns["coulmnName"].DefaultCellStyle.NullValue = "null";

     but what I'd like to get is a warning from datagridview with info just like "the value can't be set to null".   

            public Form2(){

                InitializeComponent();

                dataGridView1.CellValidating +=new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);

            }

            protected void dataGridView1_CellValidating(object source, DataGridViewCellValidatingEventArgs e)

            {

                if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null |

                      dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == string.Empty)

                    MessageBox.Show("the value can't be set to null");

                    e.Cancel = true ;

            }

     

     Hope this will help you!  

       If you still have any doubt and concern about this issue, please let me know. If I misunderstood you, please kindly elaborate your question.


    Please Mark as Answered If this is helpful Or Un-Mark as Answered if it is not helpful.

    Best Regards,
    Yan Jun
    Microsoft Online Community Support

    Tuesday, July 13, 2010 8:04 AM

All replies

  • Try this DataGridView1.Columns("ColumnName").DefaultCellStyle.NullValue = "Default Value"
    Friday, July 09, 2010 10:26 AM
  • Well, it's ok. thanks. but what I'd like to get is a warning from datagridview with info just like "the value can't be set to null".
    Friday, July 09, 2010 10:33 AM
  • You are getting error message because of schema design in data source  ( your database table). Above settings will just display a value to the cell, it won't make any changes to 'Row State'.

    Other option is 'Table Mapping ' DataTable as per requirement and assign to datagridview's data source.

    Use this link for details http://msdn.microsoft.com/en-us/library/ms810286.aspx

    Friday, July 09, 2010 10:50 AM
  • Hi Robbirk,

       Welcome to MSDN forums! I'm glad to see your active participation and discussion in MSDN forums. Based on your issue 

      Set a datagridview column value behavior to not null?

        dataGridView1.Columns["coulmnName"].DefaultCellStyle.NullValue = "null";

     but what I'd like to get is a warning from datagridview with info just like "the value can't be set to null".   

            public Form2(){

                InitializeComponent();

                dataGridView1.CellValidating +=new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);

            }

            protected void dataGridView1_CellValidating(object source, DataGridViewCellValidatingEventArgs e)

            {

                if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null |

                      dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == string.Empty)

                    MessageBox.Show("the value can't be set to null");

                    e.Cancel = true ;

            }

     

     Hope this will help you!  

       If you still have any doubt and concern about this issue, please let me know. If I misunderstood you, please kindly elaborate your question.


    Please Mark as Answered If this is helpful Or Un-Mark as Answered if it is not helpful.

    Best Regards,
    Yan Jun
    Microsoft Online Community Support

    Tuesday, July 13, 2010 8:04 AM