none
How to make a cell in a Datagridview mandatory RRS feed

  • Question

  • I have seen a lot of example on Cell Validating but no good answer.

    What i am attempting to do is if a row is edited (This column cell needs value). 

    I don't need it to loop through the whole grid just the row the user has edited.

    An Error provider would be awesome I have got it to work on TextBoxes but not datagride.

    This is what i tried 

    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
            {
                // column 11 
                if (e.ColumnIndex == 11)
                {

                    if (e.FormattedValue == null)
                    {
                        dataGridView1.CurrentRow.ErrorText = "Required value in eleventh column";
                        e.Cancel = true;
                    }
                    else
                    {
                        dataGridView1.CurrentRow.ErrorText = "";
                    }
                }
            }
        }
    }


    Booney440



    • Edited by Booney440 Saturday, September 30, 2017 6:05 PM update
    Saturday, September 30, 2017 5:56 PM

Answers

  • This has solved my issue. Hope it helps others.

     private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
            {
                 if (dataGridView1.Rows[e.RowIndex].Cells[10].FormattedValue.ToString() == string.Empty)
            {
                e.Cancel = true;
                dataGridView1.Rows[e.RowIndex].Cells[10].ErrorText = "Column 10 Mandatory";
            }
            else
            {
                dataGridView1.Rows[e.RowIndex].Cells[10].ErrorText = string.Empty;
            }
            }
        }
    }


    Booney440


    • Marked as answer by Booney440 Sunday, October 1, 2017 12:13 AM
    • Edited by Booney440 Sunday, October 1, 2017 2:02 PM update
    Sunday, October 1, 2017 12:13 AM