About the DataGridView event Validating

P

Peter

Hello!

I have a DataGridView which has been dragged from the Toolbox into the Form
and is called dataGridView1.
My question is about the event Validating. There is an event method below
called dataGridView1_Validating
which has been created automatically when I selected the Validating
event.and can be seen below.

I have noticed that the event method dataGridView1_Validating is called when
the Form is closed.
I know that the best way to update the database is to use a save button
where the command dAdapter.Update(dataTable); is located
But just for my knowledge I want to know if it's possible to fire the
Validating event so the event method dataGridView1_Validating is called.

I have read in the documentation about ther Validating event but I can't
figure out how I fire the Validating event ?
The relevant code can be seen below.

public partial class Form1 : Form
{
private OleDbDataAdapter dAdapter;
private DataTable dataTable;

public Form1()
{
InitializeComponent();

string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:\\database\\nwind.mdb";

string query = "SELECT * FROM customers";
dAdapter = new OleDbDataAdapter(query, connString);
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter)

dataTable = new DataTable();

try
{

dAdapter.Fill(dataTable);
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
}


BindingSource bindSource = new BindingSource();
bindSource.DataSource = dataTable;
dataGridView1.DataSource = bindSource;
}

private void dataGridView1_Validating(object sender, CancelEventArgs
e)
{
Console.WriteLine("Testing");
//dAdapter.Update(dataTable);
}
}

private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit()
;
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(104, 87);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(446, 316);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.Validating += new
System.ComponentModel.CancelEventHandler(this.dataGridView1_Validating);


//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(562, 465);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
private System.Windows.Forms.
....

//Tony
 
D

Devin

Hello!

I have a DataGridView which has been dragged from the Toolbox into the Form
and is called dataGridView1.
My question is about the event Validating. There is an event method below
called dataGridView1_Validating
which has been created automatically when I selected the Validating
event.and can be seen below.

I have noticed that the event method dataGridView1_Validating is called when
the Form is closed.
I know that the best way to update the database is to use a save button
where the command dAdapter.Update(dataTable); is located
But just for my knowledge I want to know if it's possible to fire the
Validating event so the event method dataGridView1_Validating is called.

I have read in the documentation about ther Validating event but I can't
figure out how I fire the Validating event ?
The relevant code can be seen below.

public partial class Form1 : Form
    {
        private OleDbDataAdapter dAdapter;
        private DataTable dataTable;

        public Form1()
        {
            InitializeComponent();

            string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
               "Data Source=C:\\database\\nwind.mdb";

            string query = "SELECT * FROM customers";
            dAdapter = new OleDbDataAdapter(query, connString);
            OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter)

            dataTable = new DataTable();

            try
            {

                dAdapter.Fill(dataTable);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
            }

            BindingSource bindSource = new BindingSource();
            bindSource.DataSource = dataTable;
            dataGridView1.DataSource = bindSource;
        }

        private void dataGridView1_Validating(object sender, CancelEventArgs
e)
        {
            Console.WriteLine("Testing");
            //dAdapter.Update(dataTable);
        }
    }

         private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(­)
;
            this.SuspendLayout();
            //
            // dataGridView1
            //
            this.dataGridView1.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing..Point(104, 87);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(446, 316);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.Validating += new
System.ComponentModel.CancelEventHandler(this.dataGridView1_Validating);

            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(562, 465);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);
        }
        private System.Windows.Forms.
...

//Tony

If updating a database and using a DataGridView you might want to look
into using as the .DataSource for the DataGridView a table from a
strongly typed DataSet. Then when a user enter a new row all you have
to do is catch the DataError event and handle it as you see fit.

Devin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top