473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problems with DataGrid

Hi,
I need a big help to resolve this problem.

I need to put a usercontrol in a datagrid: this control check if the code
inserted is present in a archive and if not return a error message.
(In the example I have changed this check with a easy "if string is empty" )

Everything would be easy if the datagrid had a predictable behaviour.
I explain my problem.

If you try to compile and use the example, you can see that if I press the
return key, the validating event isn't called .......
else
if I press the key tab, the validation event is called, but the grid takes
the focus on the next object without waiting for the result of the
validation.
The result is a three times error message and the grid blocked ( with mouse
you can't change the cell ): only a shift-tab permit to return on the cell
with the blank code.

Where do I make a mistake?

Thanks
Mauro

To respond leave NOSPAM to my e-mail address.
ma******@NOSPAMLibero.it

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

namespace HelpMe
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private DataTable myTable = null;
private DataColumn myCol1, myCol2 = null;
private MyTextBox myTextBox = null;
private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;
private DataGridControlColumn dataGridTextBoxColumn2;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeTable();
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.myTextBox = new MyTextBox();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
this.dataGridTextBoxColumn1 = new
System.Windows.Forms.DataGridTextBoxColumn();
this.dataGridTextBoxColumn2 = new DataGridControlColumn(myTextBox);
((System.ComponentModel.ISupportInitialize)(this.d ataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataSource = myTable;
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 16);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(272, 128);
this.dataGrid1.TabIndex = 0;
this.dataGrid1.TableStyles.AddRange(new
System.Windows.Forms.DataGridTableStyle[] {
this.dataGridTableStyle1});
//
// dataGridTableStyle1
//
this.dataGridTableStyle1.DataGrid = this.dataGrid1;
this.dataGridTableStyle1.GridColumnStyles.AddRange (new
System.Windows.Forms.DataGridColumnStyle[] {
this.dataGridTextBoxColumn1,
this.dataGridTextBoxColumn2});
this.dataGridTableStyle1.HeaderForeColor =
System.Drawing.SystemColors.ControlText;
this.dataGridTableStyle1.MappingName = "myTable";
//
// dataGridTextBoxColumn1
//
this.dataGridTextBoxColumn1.Format = "";
this.dataGridTextBoxColumn1.FormatInfo = null;
this.dataGridTextBoxColumn1.HeaderText = "First";
this.dataGridTextBoxColumn1.MappingName = "Col1";
this.dataGridTextBoxColumn1.Width = 75;
//
// dataGridTextBoxColumn2
//
this.dataGridTextBoxColumn2.HeaderText = "Second";
this.dataGridTextBoxColumn2.MappingName = "Col2";
this.dataGridTextBoxColumn2.Width = 75;
//
// myTextBox
//
this.myTextBox.Validating+=new CancelEventHandler(myTextBox_Validating);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.d ataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

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

private void InitializeTable()
{
myTable = new DataTable("MyTable");
myCol1 = new DataColumn("Col1", typeof(System.String));
myCol2 = new DataColumn("Col2", typeof(System.String));

myTable.Columns.Add(myCol1);
myTable.Columns.Add(myCol2);

DataRow myRow = myTable.NewRow();
myRow["Col1"] = "a";
myRow["Col2"] = "b";

myTable.Rows.Add(myRow);
}

private void myTextBox_Validating(object sender, CancelEventArgs e)
{
if (myTextBox.Text == string.Empty)
{
MessageBox.Show("Empty not valid");
e.Cancel = true;
}
}
}

#region DataGridControlColumn
/// <summary>
/// Summary description for DataGridControlBoxColumn.
/// </summary>
// This example shows how to create your own column style that
// hosts a control, in this case, a DateTimePicker.
public class DataGridControlColumn : DataGridColumnStyle
{
private MyTextBox myControl = null;
private bool isEditing;

public DataGridControlColumn(MyTextBox mycontrol) : base()
{
myControl = mycontrol;
mycontrol.Visible = false;
mycontrol.BorderStyle = BorderStyle.None;
myControl.TextChanged+=new EventHandler(myControl_TextChanged);
// myControl.Validating+=new
System.ComponentModel.CancelEventHandler(myControl _Validating);
}

protected override void Abort(int rowNum)
{
isEditing = false;
Invalidate();
}

protected override bool Commit(CurrencyManager dataSource, int rowNum)
{
myControl.Bounds = Rectangle.Empty;

if (!isEditing)
return true;

isEditing = false;

try
{
object value = myControl.Text;
SetColumnValueAtRow(dataSource, rowNum, value);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
Abort(rowNum);
return false;
}

try
{
Invalidate();
return true;
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
return false;
}
}

protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
object value = GetColumnValueAtRow(source, rowNum);
if (cellIsVisible)
{
myControl.Bounds = new Rectangle
(bounds.X + 2, bounds.Y + 2,
bounds.Width - 4, bounds.Height - 4);
myControl.Text = (value == System.Convert.DBNull) ? string.Empty :
(string)value;
myControl.Visible = true;
myControl.TextChanged+=new EventHandler(myControl_TextChanged);

myControl.Enabled = !this.DataGridTableStyle.ReadOnly;
myControl.Focus();
}
else
{
myControl.Text = (value == System.Convert.DBNull) ? string.Empty :
(string)value;
myControl.Visible = false;
}

if (myControl.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);
}

protected override Size GetPreferredSize(
Graphics g,
object value)
{
return new Size(100, myControl.Height - 4);
}

protected override int GetMinimumHeight()
{
return myControl.Height - 4;
}

protected override int GetPreferredHeight(Graphics g,
object value)
{
return myControl.Height - 4;
}

protected override void Paint(Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum)
{
Paint(g, bounds, source, rowNum, false);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
bool alignToRight)
{
Paint(
g,bounds,
source,
rowNum,
Brushes.Red,
Brushes.Blue,
alignToRight);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight)
{
object value =
GetColumnValueAtRow(source, rowNum);
string text = (value == System.Convert.DBNull) ? string.Empty :
(string)value;
Rectangle rect = bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(0, 2);
rect.Height -= 2;
g.DrawString(text,
this.DataGridTableStyle.DataGrid.Font,
foreBrush, rect);
}

protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn(value);
if (myControl.Parent != null)
{
myControl.Parent.Controls.Remove
(myControl);
}
if (value != null)
{
value.Controls.Add(myControl);
}
}

private void myControl_TextChanged(object sender, EventArgs e)
{
if (myControl.Parent != null)
if (((DataGrid) myControl.Parent).ReadOnly) return;

this.isEditing = true;
base.ColumnStartedEditing(myControl);
}

// private void myControl_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
// {
// if (myControl.Text == string.Empty)
// {
// MessageBox.Show("Not valid");
// e.Cancel = true;
// }
// }
}
#endregion

#region MyTextBox
/// <summary>
/// MyTextBox inherited from TextBox to
/// remove the Tab KEYUP message to avoid
/// focus problem
/// </summary>
public class MyTextBox : System.Windows.Forms.TextBox
{
const int WM_KEYDOWN=0x100;
const int WM_KEYUP=0x101;

public override bool PreProcessMessage(
ref System.Windows.Forms.Message msg)
{
Keys keyCode=(Keys)(int)msg.WParam & Keys.KeyCode;
if(msg.Msg==WM_KEYUP && keyCode==Keys.Tab)
{
msg.WParam=(System.IntPtr)0;
return true;
}

if (msg.Msg == WM_KEYDOWN)
{
// if (keyCode == Keys.Return || keyCode == Keys.Down)
// msg.WParam = (IntPtr)Keys.Tab;

if (keyCode == Keys.Up)
msg.WParam = (IntPtr)((int) Keys.Tab | (int)Keys.Shift);
}
return base.PreProcessMessage(ref msg);
}
}

#endregion
}

Nov 16 '05 #1
0 3519

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: JP011 | last post by:
Hello I have hit a major road block when it comes to building my dynamic datagrid. To make a long story short I need a dynamic datagrid because my connection string could change and I need the...
2
by: Tamlin | last post by:
Hi all, I'm getting a bug with the datagrid object. I've created one from scratch, bound it to a dataview with 2 int32 columns and formatted the output as currency. I've found that when you...
3
by: Igor Mendizabal | last post by:
Hello, We're doing our own datagrid based on the System.windows.forms.datagrid control, and are having some problems with horizontal scrolling. In general, we construct our datagrid adding a...
3
by: Bill C. | last post by:
Hello, I know this has been discussed a lot already because I've been searching around for information the last few weeks. I'm trying to implement a DataGridComboBoxColumn class. I've found...
0
by: THM5101 | last post by:
Hello I have two problems with my dataGrid. I created dataGrid with 4 columns. The first and the last one are LinkButton type, the second and third are Bound Column type. My problems are:...
0
by: optimizeit | last post by:
What I am attempting to do is import an Excel Workbook and display the worksheets in a datagrid dynamically. I am very close to getting this to work. I have to this point successfully imported a...
3
by: simchajoy2000 | last post by:
Hi, I have been working with datagrids a lot in the past two weeks and I am running across a lot of problems. Maybe there is no way around these problems but I hope there are and someone out...
1
by: Vili | last post by:
Hi all I am having problems with asp.net 1.1 and nested datagrid. I have a datagrid (dgDetail) nested inside a datagrid (dgMaster). How can I make all the rows editable in dgDetail? So that...
0
by: Scott | last post by:
Hello all and thanks in advance for any help you may be able to offer me. I am quite new to asp.net and am trying to work with a datagrid but am having some problems with it. Here's the...
2
by: ree321 | last post by:
I tried moving an ASP.net project form 1.1 to 2.0. But I am having problems with getting the OnItemDataBound function to run for a datagrid, in 2.0. It only gets called up if AutoGenerateColumns...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.