datagrid verification issue

  • Thread starter Thread starter Jack Kaufmann
  • Start date Start date
J

Jack Kaufmann

I am trying to verify a datagrid in which there must be an entry in both
columns of any new row, and those values must be greater than the
corresponding values in the previous row. It is pretty simple to verify the
cell the user starts in by invoking a verifying event for the underlying
textbox in that cell, but what I would like to do is to change the focus to
the other column in the new row if it has not been filled in. If in
handling the verifying event for the first column I include e.cancel = true
I will come back to that column in the new row, but if before or after doing
that I change the currentcell to the other column, I don't come back. If I
don't include e.cancel, of course, the focus goes whereever the user went in
triggering the verifying event. Any ideas re how in verifying one cell I
can get the user back to another cell in the datagrid? Many thanks.
 
Hi Jack,

Currently I am looking for somebody who could help you on it. We will reply
here with more information as soon as possible.
If you have any more concerns on it, please feel free to post here.


Thanks for your understanding!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
Hello,

Thanks for your post. As I understand, the problem you are facing is that
you cannot set current cell to other when calling e.cancel = true in the
Validating event of your DataGrid control. Please correct me if there is
any misunderstanding.

1. I tested the following code in my WinForm application, however, I am not
able to reproduce the problem. That is, it can go to another cell in the
datagrid.

private void dataGrid1_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
e.Cancel= true;
dataGrid1.CurrentCell = new DataGridCell(3,3);
}

2. I think more information is needed before moving forward:
What's the type of your application, WinFor or WebForm?
Are you able to reproduce the problem in a simple project?
Could you post some code snippet or tell me the detailed steps to reproduce
the problem?

I look forward to hearing from you.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks for your reply. I'm not sure what all my problems are, but I've
found the following 3 phenomena in a WinForm project: in a two column data
grid, whenever the verifying routine (which as I mentioned is tied to the
column's underlying textbox, not to the datagrid itself) returns
e.Cancel=true, then (1) if verifying was triggered by clicking on a new cell
in the same column, the verifying routine is called once, but if it is
triggered by clicking on any cell in the other column, the verifying routine
is called twice. Here's some simple code to show what I mean:

[class- wide variables]
int test = 0;
.. . . .
private void datagrid_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
test += 1;
label1.Text = test.ToString();
e.Cancel = true;
}

When this code is running, assuming verifying is first called from column 0,
whenever a cell in column 1 is clicked, the label shows "test" incrementing
by 2, but if any cell in column 0 is clicked, the label only increments
once.

(2) 2nd phenomenon: in the same example, when a cell in the same column is
clicked, the current cell will move to the new row, even though e.Cancel was
set to true. Is this because what is being verified is the textbox
associated with the column, and it moves up and down with whatever cell is
clicked? (It does not matter if code is added as in your sample code to the
above routine setting currentcell to what is at that point the current cell
anyway, the one that was highlighted when verifying was triggered by
clicking on another cell in the same column. This could probably be
prevented by doing something in a currentcellchanged event, but I'm just
trying to figure out what's going on.)

(3) 3rd phenomenon: also in the same example, when a cell in the opposite
column is clicked, in addition to the verifying routine being called twice,
no cell is highlighted, and trying to enter data results in no change in
either column, but the little pencil in the row header of the row in which
the user clicked appears, suggesting that "startedediting" is now set.

I'm probably missing something very simple, but I'd be very grateful for
whatever help and advice you can give me on the above issues.

Regards,
Jack Kaufmann
 
Hello Jack,

Thanks for your update. I reviewed your description carefully, and I think
more information is needed before moving forward:

Could you post a simple project which is able to reproduce the problem? I
believe that will be most helpful for me to pinpoint the problem and
resolution.

I look forward to your response.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Sure. Following (I couldn't send it as an attachment, for some reason) is a
simple Form1.cs file which demonstrates the stuff in my last post. (Let me
know if you need more files than that.) Thanks for your help.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DatagridVerification
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.Anchor =
((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.To
p | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(40, 24);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(360, 224);
this.dataGrid1.TabIndex = 0;
//
// label1
//
this.label1.Anchor =
((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bot
tom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.label1.Location = new System.Drawing.Point(192, 256);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 23);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(440, 294);
this.Controls.Add(this.label1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Closing += new
System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = "MyTable";
dataGrid1.TableStyles.Add(dgts);

DataTable dt = GetTheTable();
dataGrid1.DataSource = dt;

for (int i = 0; i < dt.Columns.Count; ++i)
{
DataGridTextBoxColumn dgtbc = dgts.GridColumnStyles as
DataGridTextBoxColumn;
if (dgtbc != null)
{
dgtbc.TextBox.Validating += new System.ComponentModel.
CancelEventHandler(dataGrid_Validating);
}
}
}
int test = 0;

private void dataGrid_Validating(object sender, CancelEventArgs e)
{
test += 1;
label1.Text = test.ToString();
e.Cancel = true;
}

private DataTable GetTheTable()
{
DataTable dt = new DataTable("MyTable");

int nCols = 4;
int nRows = 10;

for(int i = 0; i < nCols; i++)
dt.Columns.Add(new DataColumn(string.Format("Col{0}", i)));

for(int i = 0; i < nRows; ++i)
{
DataRow dr = dt.NewRow();
for(int j = 0; j < nCols; j++)
dr[j] = string.Format("row{0} col{1}", i, j);
dt.Rows.Add(dr);
}
return dt;
}

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
e.Cancel = false;
}
}
}
 
Hi, Jack

May attempted to send you the following in email, but what she had is
apparently invalid. You can reach her as >mayji< for her email and
microsoft.com< as her domain. Since the SPAM bots will get it if I put
the pieces together, I leave it to you to put them together and respond.

Thanks,

John Eikanger
Microsoft Developer Support
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>

Hi Jack,

This is May Ji from Microsoft Developer Support. Our newsgroup team has
created a grace support incident to track the progress of your issue
regarding the DataGrid. I currently own the case and will be very glad to
work with you to provide you the solution. Could you please send me a
sample to demonstrate what you are trying to do, and what’s not working?

I look forward to hearing back from you soon.

Thanks,
May Ji
Tech Lead
Microsoft Developer Support
 
More from May:

From: May Ji
Sent: Tuesday, April 20, 2004 5:05 PM
To: 'Jack Kaufmann'
Subject: RE: Case SRX040415603350

Jack,

I did some testing with your sample. Using your sample, I did notice some
problems with the DataGrid’s Validating event such as the Validating event
firing multiple times, etc. By reading your email and also looking at the
code in your sample, I think your end goal is trying to implement
cell-by-cell validation. Is this thinking correct?

There are problems implementing cell-by-cell validation using the
DataGrid's Validating Event architecture. The problem is that the grid is
not the object handling the data. Instead, a TextBox or some other control
is the control managing the changing of the cell contents. One way to
implement the validation at the grid level is to handle the
CurrentCellChanged event, and if the previous cell's value is not proper,
then return to that cell. You can download a sample from Syncfusion's FAQ
page (http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q773q
<http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp>) that implements this
process. The sample only handles the validation from cell to cell movement.
If you want to handle the validation when the user clicks on the form’s
Close button, then you would have to add a special event handler for this
and do one last validation at this point.

If I have completely misunderstood what you are trying to do, please let me
know. If it helps, you can call me at (425)7042136 to discuss this issue
(or let me know how to reach you via Phone).

Thanks!
May
 
Back
Top