Henry Intersection - DataGrid Event Response (2)

zhaozj2021-02-16  53

Henry Intersection - DataGrid Event Response (2)

Han Rui (5/6/2003)

Hi, everyone is good. For the use of the DataGrid control in Windows Form, I have written a lot of articles, but I have continually received the opinions of netizens, I hope to write more. At the request of a friend, I will write some articles for several issues. I hope to bring you a little inspiration, you can use DataGrid, which is more important to give you an antipyrett, apply the method of processing the problem to other controls. Go in.

This article mainly solves the needs of the selected row with any click on the DataGrid. With this article, you will also understand the relationship between focus and controls.

This question seems very simple, but if you usually pay attention, each time you click on a single element, we will click on a plaid inside, but will not choose a line. I want to choose a row and only click on the line of each line.

Some friends will say, it is not simple, we can write the processing code in the DataGrid mousedown event handler, use HitTest to determine which line is the lattice, and then Select is the line.

but,……. When you do it, you will find it, no right! Every time I click, I still look old, the focus will go to the inside of TextBox. This……

Is it that DataGrid sneaks not to do what we ask for it! Oh, you have it, in fact it is still implemented! However, the speed of execution is too fast, we didn't see the results of the Qing people. Who is moving its cheese?

If you have seen my previous article, it should not understand: User mouse is going to DataGrid's one-click -> DataGrid first receive the focus, execute the event you ask for it -> Then focus will fall into this grid TEXTBOX to get Textbox focus. (Detailed analysis, please see "DataGrid Event Response (1)")

Understand, after the DataGrid just executed the operation of the selected line, it embeddbox that embedded will lead to a focus event, causing DataGrid no longer choosing a line, but change the focus to the embedded Textbox you click. .

So, the current solution should be: write the code in the event that the focus just falls into the TextBox embedded in the DataGrid. For TextBox, what is the incoming process of the focus, and what's the event we should write the code of the current line?

In order to, the control events that will be caused by:

Sequence number

Event name

Incident meaning

1

ENTER

Focus enters the control

2

Gotfocus

Control received focus

3

Leave

Enter the focus left the control

4

Validating

Controls are verifying

5

Validated

The control completes the verification

6

Lostfocus

Control lost focus

So, the first event that will be triggered is Enter! Ok, let's start!

Now get the work you want to do:

(1) Find a binding data source for DataGrid to make DataGrid displayed data;

(2) To write the event handler of the control of the DataGrid, of course, you need to redefine the DataGrid DataGridTableStyle;

(3) Assign the Enter Event to each column style GridColumnStyles; (4) Write an ENTER event handler of embedding TextBox;

(5) After finishing the work!

The procedure is as follows:

Dim DataSet1 As New DataSet

DIM DT AS DATATABLE '

Private Sub Form1_Load (Byval E AS System.Object, Byval E AS System.Eventargs) Handles MyBase.LOAD

'Build a simple DataSet

DT = Dataset1.tables.add ("MyTable")

Dt.columns.Add ("col1", gettype (string))

Dt.columns.Add ("col2", gettype (integer))

Dim Row, Row1 AS Datarow

Row = DT.NEWROW ()

Row! COL1 = "Name"

Row! COL2 = 123

Dt.Rows.Add (Row)

Row1 = DT.NEWROW ()

Row1! COL1 = "xxx"

Row1! COL2 = 123

Dt.Rows.Add (Row1)

'Construction is completed

DataGrid2.datasource = DT

DIM TS AS New DataGridTableStyle () 'is what it determines what DataGrid is like

DIM AcolumnTextColumn as DataGridTextBoxColumn 'To rewrite Dongdong

DataGrid1.datasource = DT

Ts.mappingname = dt.tablename

DIM Numcols as INTEGER

Numcols = dt.columns.count

DIM I as integer = 0

Do While (i

acolumnTextColumn = New DataGridTextBoxColumn ()

'Capture control of TEXTBOX

AddHandler AcolumnTextColumn.TextBox.Enter, New EventHandler (Addressof TextBoxEnterHandler)

'To change the list name, please refer the Headertext value of the sentence

AcolumnTextColumn.Headertext = DT.COLUMNS (i) .Columnname

AcolumnTextColumn.mappingname = dt.columns (i) .columnname

Ts.GridColumnStyles.add (acolumntextColumn) 'Add a custom Column style

i = (i 1)

Loop

DataGrid1.tablestyles.clear ()

DataGrid1.tableStyles.Add (TS) 'Add a custom table style

End Sub

Private sub textboxenterHandler (Byval E AS ISTEM.EventArgs)

DataGrid1.select (DataGrid1.currentrowindex)

End Sub

Run, then let's click, 咦, why is there a blank stuff with the one that is clicked, as shown in Figure 1: Figure 1 Blank Stuff

What else do you have not considered? Of course, there is, what I said at the beginning, DataGrid is a grid, there is still its structure between every cell, do you want to install one of the TextBox? (I want to put sesame in the dough The process, that dough is DataGrid, sesame is embedded control, can be TextBox, can be CHECKBOX, can be ComboBox, can be other controls. One together, it is ... Burning, ha! It seems hungry ).

In this color selected, because Textbox is focused, DataGrid is masked in part of this grorn. Now I want to choose them, the method is very simple, that is, the normal operation process of DataGrid. When can I choose all, of course, I click on the line. From the corner of the focus, this line is not in a cell!

Therefore, rewrite the ENTER's event handler is as follows, set the TextBox of you clicked to false, it disappeared, the DataGrid's tribute can be selected:

Private sub textboxenterHandler (Byval E AS ISTEM.EventArgs)

DIM inner as control = sender

Inner.visible = FALSE

DataGrid1.select (DataGrid1.currentrowindex)

End Sub

I wish you all a health and happiness in the shadow of SARS, happiness forever and diligent you!

----

Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.

QQ: 18349592

E-mail: Henry7685@hotmail.com

Please visit my column: http://www.9cbs.net/develop/author/netauthor/latitude/

转载请注明原文地址:https://www.9cbs.com/read-25542.html

New Post(0)