POV
POV

Reputation: 12025

How to validate cells in DataGridView?

I have DataGridView with several cells. How to validate all cells and highlight invalidated cells?

Validation should work after filling cells.

I tried to use this way:

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
}

But I need to set some validation rules for each fields(columns). For example, check on number or string.

Upvotes: 0

Views: 128

Answers (1)

Kasunjith Bimal
Kasunjith Bimal

Reputation: 188

You can try this code. When We are going to edit cell('EmployeeName') it avoid from integer values. When focus lost in selected cell Display the error Message.

this is My model

namespace WindowsFormsApplication1
{
    public class Employee
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public string EmployeeAddress { get; set; }
    }
}

this is My code

 namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            List<Employee> EmployeeList = new List<Employee>();
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {


                Employee emp = new Employee();

                emp.EmployeeAddress = "polonnaruwa";

                emp.EmployeeId = 1;
                emp.EmployeeName = "Kasun";

                EmployeeList.Add(emp);
                Employee emp1 = new Employee();
                emp1.EmployeeAddress = "Kandy";

                emp1.EmployeeId = 2;
                emp1.EmployeeName = "Bimal";

                EmployeeList.Add(emp1);

                Employee emp2 = new Employee();
                emp2.EmployeeAddress = "New Town";

                emp2.EmployeeId = 3;
                emp2.EmployeeName = "ASheain";

                EmployeeList.Add(emp2);

                dataGridView1.DataSource = EmployeeList;

            }

            private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (this.dataGridView1.Columns[e.ColumnIndex].Name == "EmployeeName")
                {
                    int RowIndex = e.RowIndex;
                    int columnIndex = e.ColumnIndex;
                    if (e.Value != null)
                    {

                        string stringValue = (string)e.Value;
                        int val;
                        if (int.TryParse(stringValue, out val))
                        {

                            label1.Text = "it is integer";
                            dataGridView1.Rows[RowIndex].Cells[columnIndex].Value = "Please Enter String Value";

                        }
                        else
                        {
                            label1.Text = "it is not integer";

                        }

                    }
                }

            }

            private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
            {

            }
        }
    } 

Upvotes: 6

Related Questions