DataGridView-EventHandler in .Net 2.0

Also, ich habe mir das .Net Framework 2.0 besorgt. Dabei gibt es ja eine laut MS einfacher zu nutzende Klasse namens DataGridView. Ich habe damit auch schon ein paar DataGrids erstellt, die sich tatsächlich an einigen Stellen leichter konfigurieren lassen, als die alte Klasse DataGrid.
Allerdings bekomme ich es einfach nicht hin, dem DataGridView ein OnKeyPress-EventHandler unterzujubeln. Das ganze soll folgendermaßen aussehen: Ich möchte Tastatureingaben abfangen und nur 2Byte-Hexwerte zulassen. Also, alles außer 0…9 und A…F soll ignoriert werden. Außerdem sollen pro DataGridViewCell nur 2 Character zugelassen werden. Wie bekomme ich den OnKeyPress Event eingebunden?
Ich habe natürlich schon versucht einfach

 dataGridView1.KeyPress += new KeyPressEventHandler(OnKeyPress) 

zu nutzen, auch eine eigene Klasse der Form

public class MyDataGridView: DataGridView
 {
 private Form1 ParentFrm;

 public MyDataGridView(Form1 aFrm1):base()
 {
 this.ParentFrm = aFrm1;
 this.KeyPress += new KeyPressEventHandler(OnKeyPress);
 }

 protected void OnKeyPress(object sender, KeyPressEventArgs e)
 {
 //
 //ignore if not hex or control key
 //
 Console.WriteLine("test");
 char key = e.KeyChar;
 key = Convert.ToChar(key.ToString().ToUpper());
 if(!char.IsDigit(key) && !char.IsControl(e.KeyChar) && !(key == 'A') && !(key == 'B') && !(key == 'C') && !(key == 'D') && !(key == 'E') && !(key == 'F'))
 e.Handled = true;
 //
 //jump to next cell if more than 2 digits 
 //
 if(!e.Handled && ((string)ParentFrm.dataGridView1.CurrentCell.Value).Length \>= 1 && !char.IsControl(e.KeyChar))// && !dataGridView1.Selected)
 {
 if (((string)ParentFrm.dataGridView1.CurrentCell.Value).Length 

habe ich versucht, aber der Handler wird einfach nicht aufgerufen...

Mein DataGridView habe ich folgendermaßen angelegt:



    
    dataGridView2.EditMode = DataGridViewEditMode.EditOnEnter;
     dataGridView2.Name = "dataGridView2";
     dataGridView2.Location = new Point(70, 315);
     dataGridView2.Size = new Size(378, 158);
     dataGridView2.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.ColumnsDisplayedRows;
     dataGridView2.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
     dataGridView2.CellBorderStyle = DataGridViewCellBorderStyle.Single;
     dataGridView2.GridColor = SystemColors.ActiveBorder;
     dataGridView2.MultiSelect = false;
     dataGridView2.RowHeadersVisible = false;
    
    dataGridView2.ColumnCount = 12;
     dataGridView2.AllowUserToDeleteRows = false;
     dataGridView2.AllowUserToAddRows = false;
     dataGridView2.AllowUserToOrderColumns = false;
     dataGridView2.ColumnHeadersHeightResizable = false;
     dataGridView2.ColumnHeadersHeight = 35;
     DataGridViewCellStyle style = dataGridView2.ColumnHeadersDefaultCellStyle;
     style.BackColor = Color.LightGray;
     style.ForeColor = Color.Black;
     style.Alignment = DataGridViewContentAlignment.MiddleCenter;
     style.Font = new Font(dataGridView1.Font, FontStyle.Bold);
    
    
     dataGridView2.Columns[0].Name = "Frame";
     dataGridView2.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
     dataGridView2.Columns[0].Width = 40;
     dataGridView2.Columns[0].Resizable = DataGridViewTriState.False;
     dataGridView2.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
     dataGridView2.Columns[0].DefaultCellStyle = style;
     dataGridView2.Columns[0].ReadOnly = true;
     dataGridView2.Columns[1].Name = "Rx=1 Tx=0";
     dataGridView2.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
     dataGridView2.Columns[1].Width = 40;
     dataGridView2.Columns[1].Resizable = DataGridViewTriState.False;
     dataGridView2.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
     dataGridView2.Columns[2].Name = "Chk length";
     dataGridView2.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
     dataGridView2.Columns[2].Width = 40;
     dataGridView2.Columns[2].Resizable = DataGridViewTriState.False;
     dataGridView2.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable;
     dataGridView2.Columns[3].Name = "ID";
     dataGridView2.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
     dataGridView2.Columns[3].Width = 28;
     dataGridView2.Columns[3].Resizable = DataGridViewTriState.False;
     dataGridView2.Columns[3].SortMode = DataGridViewColumnSortMode.NotSortable;
     dataGridView2.Columns[4].Name = "D0";
     dataGridView2.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
     dataGridView2.Columns[4].Width = 28;
     dataGridView2.Columns[4].Resizable = DataGridViewTriState.False;
     dataGridView2.Columns[4].SortMode = DataGridViewColumnSortMode.NotSortable;
     dataGridView2.Columns[5].Name = "D1";
     dataGridView2.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
     dataGridView2.Columns[5].Width = 28;
     dataGridView2.Columns[5].Resizable = DataGridViewTriState.False;
     dataGridView2.Columns[5].SortMode = DataGridViewColumnSortMode.NotSortable;
     dataGridView2.Columns[6].Name = "D2";
     dataGridView2.Columns[6].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
     dataGridView2.Columns[6].Width = 28;
     dataGridView2.Columns[6].Resizable = DataGridViewTriState.False;
     dataGridView2.Columns[6].SortMode = DataGridViewColumnSortMode.NotSortable;
     dataGridView2.Columns[7].Name = "D3";
     dataGridView2.Columns[7].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
     dataGridView2.Columns[7].Width = 28;
     dataGridView2.Columns[7].Resizable = DataGridViewTriState.False;
     dataGridView2.Columns[7].SortMode = DataGridViewColumnSortMode.NotSortable;
     dataGridView2.Columns[8].Name = "D4";
     dataGridView2.Columns[8].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
     dataGridView2.Columns[8].Width = 28;
     dataGridView2.Columns[8].Resizable = DataGridViewTriState.False;
     dataGridView2.Columns[8].SortMode = DataGridViewColumnSortMode.NotSortable;
     dataGridView2.Columns[9].Name = "D5";
     dataGridView2.Columns[9].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
     dataGridView2.Columns[9].Width = 28;
     dataGridView2.Columns[9].Resizable = DataGridViewTriState.False;
     dataGridView2.Columns[9].SortMode = DataGridViewColumnSortMode.NotSortable;
     dataGridView2.Columns[10].Name = "D6";
     dataGridView2.Columns[10].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
     dataGridView2.Columns[10].Width = 28;
     dataGridView2.Columns[10].Resizable = DataGridViewTriState.False;
     dataGridView2.Columns[10].SortMode = DataGridViewColumnSortMode.NotSortable;
     dataGridView2.Columns[11].Name = "D7";
     dataGridView2.Columns[11].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
     dataGridView2.Columns[11].Width = 28;
     dataGridView2.Columns[11].Resizable = DataGridViewTriState.False;
     dataGridView2.Columns[11].SortMode = DataGridViewColumnSortMode.NotSortable;
    
     String[] row0 = {"0", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00"};
     String[] row1 = {"1", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00"};
     String[] row2 = {"2", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00"};
     String[] row3 = {"3", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00"};
     String[] row4 = {"4", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00"};
     String[] row5 = {"5", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00"};
    
     DataGridViewRowCollection rows = this.dataGridView2.Rows;
     rows.Add(row0);
     rows.Add(row1);
     rows.Add(row2);
     rows.Add(row3);
     rows.Add(row4);
     rows.Add(row5);
    




Könnt ihr mir etwas auf die Sprünge helfen?

Im Voraus vielen Dank!!!

hallo holger,

Also, ich habe mir das .Net Framework 2.0 besorgt. Dabei gibt
es ja eine laut MS einfacher zu nutzende Klasse namens
DataGridView.

mmh, das klingt als würdest du MS glauben…

Ich habe damit auch schon ein paar DataGrids
erstellt, die sich tatsächlich an einigen Stellen leichter
konfigurieren lassen, als die alte Klasse DataGrid.

die kenne ich nicht, nur damit du eine idee bekommst…wie inkompetent ich bin…nee, aber ich kenne keine ‚alten‘ datagrids…

Allerdings bekomme ich es einfach nicht hin, dem DataGridView
ein OnKeyPress-EventHandler unterzujubeln. Das ganze soll
folgendermaßen aussehen: Ich möchte Tastatureingaben abfangen
und nur 2Byte-Hexwerte zulassen. Also, alles außer 0…9 und
A…F soll ignoriert werden.

eine notlösung wäre keypreview der zugehörigen form auf true zu setzen und dann im keypress-event weiter zu machen.

noch eine notlösung wäre den datagrid.currentcellchanged (oder so) event zu abbonieren und die werte da abzuchecken.

was du aber eigtl. willst und, so weit ich weiss, nur mit ein wenig aufwand geht ist dich an den textbox.validated event der textbox zu hängen, die das editieren von datagridcells übernimmt.

aaalso:
public class MyColumnStyle : DataGridTextBoxColumn

…und im constructor:

this.TextBox.Validated += new System.EventHandler(this.CheckForHexAndBlaBla);

und damit du das in deinem dg verwenden kannst:

DataGridTableStyle tableStyle = new DataGridTableStyle();
PropertyDescriptorCollection pcol = this.BindingContext[dt]. GetItemProperties();
//„dt“ ist die entsprechende datatable…
columnStyle = new MyColumnStyle(pcol[tableStyle.MappingName], GetColumn(iCurrCol), „*“);
tableStyle.GridColumnStyles.Add(columnStyle);

hope this helps…

stefan

hallo holger,

Also, ich habe mir das .Net Framework 2.0 besorgt. Dabei gibt
es ja eine laut MS einfacher zu nutzende Klasse namens
DataGridView.

mmh, das klingt als würdest du MS glauben…

oh ne, so tief sinke ich nicht :smile:)

Ich habe damit auch schon ein paar DataGrids
erstellt, die sich tatsächlich an einigen Stellen leichter
konfigurieren lassen, als die alte Klasse DataGrid.

die kenne ich nicht, nur damit du eine idee bekommst…wie
inkompetent ich bin…nee, aber ich kenne keine ‚alten‘
datagrids…

mich deucht, du kennst leider (für mich leider, ist bestimmt keine tragische Wissenslücke :smile:) ) nur die ‚alten‘ DataGrids… Weil, soweit bin ich auch gekommen, aber das funzt mit der neuen DataGridView-Klasse nimmer. (s.u.)

Allerdings bekomme ich es einfach nicht hin, dem DataGridView
ein OnKeyPress-EventHandler unterzujubeln. Das ganze soll
folgendermaßen aussehen: Ich möchte Tastatureingaben abfangen
und nur 2Byte-Hexwerte zulassen. Also, alles außer 0…9 und
A…F soll ignoriert werden.

eine notlösung wäre keypreview der zugehörigen form auf true
zu setzen und dann im keypress-event weiter zu machen.

Muss ich mal versuchen…

noch eine notlösung wäre den datagrid.currentcellchanged (oder
so) event zu abbonieren und die werte da abzuchecken.

wäre ebenfalls zu testen…

was du aber eigtl. willst und, so weit ich weiss, nur mit ein
wenig aufwand geht ist dich an den textbox.validated event der
textbox zu hängen, die das editieren von datagridcells
übernimmt.

und soweit war ich auch schon. Die neue Klasse DataGridView kennt keine DataGridViewTextBoxColumn.TextBox !! Das war bisher im .Net 1.x so, aber nimmer im 2.0 in der DataGridView. Und die „alte“ DataGridTextBoxColumn funktioniert nicht mit der DataGridView-Klasse…

aaalso:
public class MyColumnStyle : DataGridTextBoxColumn

…und im constructor:

this.TextBox.Validated += new
System.EventHandler(this.CheckForHexAndBlaBla);

und damit du das in deinem dg verwenden kannst:

DataGridTableStyle tableStyle = new DataGridTableStyle();
PropertyDescriptorCollection pcol = this.BindingContext[dt].
GetItemProperties();
//„dt“ ist die entsprechende datatable…
columnStyle = new
MyColumnStyle(pcol[tableStyle.MappingName],
GetColumn(iCurrCol), „*“);
tableStyle.GridColumnStyles.Add(columnStyle);

hope this helps…

Naja, vielleicht. Trotzdem Vielen Dank!!!

stefan

Danke, hab es…
Also, ich hab was gefunden. Die so genannte Notlösung mit dem KeyPreview reicht mir.

Vielen Dank!!!