hi,
how can i valiadte each cell in a datagrid view seperately,
i have one column int which is the 7th column and the 3rd and the 5th are calender control and the first two are combo boxes, and a check box column

Question 1:
how can i select on item in the combo box cell, now i have to click twice to select a values. can i specify to that cell only.???

Qustion 2:
and also to the Calender control i need to validate that the user enters a valid date (todate or early date) not a previous date,

Question 3:
when i click on the heck box i need to display todays date in the next calender control cell

Question 4:
in the textbox cell i need to calidate that the user should not enter special characters .

Question 5:
and also i need to make sure that only integer is allows in the other textbox column which alows the user to add the telephone number

please can someone help me with this

appreciate alot

thaxxxxxx

Dani AI

Generated

As suggested, the right place for per-cell checks is CellValidating/CellValidated. Below are compact, practical handlers that: validate combo/date/text/int cells, open a combo on a single click for chosen columns, and set today's date when a checkbox is checked. Replace the numeric column indexes with the actual indexes used in the grid.

// requires: using System.Text.RegularExpressions;

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    var grid = (DataGridView)sender;
    grid.Rows[e.RowIndex].ErrorText = string.Empty;
    string val = e.FormattedValue == null ? string.Empty : e.FormattedValue.ToString();

    // combo columns (0 and 1): must have a selection
    if (e.ColumnIndex == 0 || e.ColumnIndex == 1)
    {
        if (string.IsNullOrWhiteSpace(val))
        {
            grid.Rows[e.RowIndex].ErrorText = "Select a value.";
            e.Cancel = true;
            return;
        }
    }

    // date columns (2 and 4): must parse and be today or later
    if (e.ColumnIndex == 2 || e.ColumnIndex == 4)
    {
        DateTime dt;
        if (!DateTime.TryParse(val, out dt) || dt.Date < DateTime.Today)
        {
            grid.Rows[e.RowIndex].ErrorText = "Enter a valid date (today or later).";
            e.Cancel = true;
            return;
        }
    }

    // text column (no special chars) at index 5
    if (e.ColumnIndex == 5 && !Regex.IsMatch(val, @"^[A-Za-z0-9 ]*$"))
    {
        grid.Rows[e.RowIndex].ErrorText = "Special characters are not allowed.";
        e.Cancel = true;
        return;
    }

    // integer-only column at index 6 (telephone)
    if (e.ColumnIndex == 6 && !Regex.IsMatch(val, @"^\d*$"))
    {
        grid.Rows[e.RowIndex].ErrorText = "Only digits allowed.";
        e.Cancel = true;
        return;
    }
}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;
}
// single-click combo dropdown for chosen columns
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex < 0) return;
    if (e.ColumnIndex == 0 || e.ColumnIndex == 1) // combo columns
    {
        dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        dataGridView1.BeginEdit(true);
        var combo = dataGridView1.EditingControl as DataGridViewComboBoxEditingControl;
        if (combo != null) combo.DroppedDown = true;
    }
}

// checkbox immediate commit so CellValueChanged fires
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty && dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}

// when checkbox toggles, write today's date into the target date cell
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex < 0) return;
    int checkCol = 3;    // change to actual checkbox column index
    int nextDateCol = 4; // change to the date column to fill
    if (e.ColumnIndex == checkCol)
    {
        object raw = dataGridView1.Rows[e.RowIndex].Cells[checkCol].Value;
        bool isChecked = raw != null && raw != DBNull.Value && Convert.ToBoolean(raw);
        dataGridView1.Rows[e.RowIndex].Cells[nextDateCol].Value = isChecked ? DateTime.Today : null;
    }
}

Notes and troubleshooting: prefer DateTime.TryParseExact when a fixed format is required; set date column ValueType = typeof(DateTime) if possible (assign DateTime objects rather than strings); clear Row/ErrorText in CellEndEdit or CellValidated; use EditMode = EditProgrammatically if more control is needed. Mentioning here: the above patterns address the combo single-click, calendar validation, checkbox-to-date, special-character filtering and integer-only telephone column while keeping validation local to the affected columns.

Recommended Answers

All 3 Replies

Use CellValidating and CellValidated events.

hey what should the code be
the syntax

thanxx

Not too much, only if and else, if you can.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.