Home » Windows OSRSS

DataGridView. Set Column Type to notNull

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

 

4 Answers Found

 

Answer 1

Try this DataGridView1.Columns("ColumnName").DefaultCellStyle.NullValue = "Default Value"
 

Answer 2

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".
 

Answer 3

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

 

Answer 4

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);

        }

        protectedvoid 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.

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter