add handlers to textboxes in a datagrid

J

Josh Golden

sorry for so many posts recently

i am loading a datagrid from a datatable. the shape of the datatable is not
known at design time. i want to dynamically add event handlers to the
datagrid textboxes. it would be really easy if i were using a
DataGridTableStyle at design time but that means i know the columns.

how do i loop through the textboxes in the datagrid? i will have to do this
after i've used the setDataBinding method of the datagrid. that's the only
way how i will know how many columns (DataGridTextBoxColumn) there are.

for each x as DataGridTextBoxColumn in dgOrders.< ? >
AddHandler x.TextBox.MouseDown, New MouseEventHandler(AddressOf
someMouseDownEvent)
AddHandler x.TextBox.DoubleClick, New EventHandler(AddressOf
someDoubleClickEvent)
next
 
C

Cor Ligthert

Josh,
i am loading a datagrid from a datatable. the shape of the datatable is not
known at design time.

When the shape of the datasource is not known at runtime you have no
datagrid at runtime as well, so what do you mean with that sentense?.

However when you want to do something with an event in a databox from a
datagrid, mayby you can than use this sample that I once made?

Cor

\\\
Private WithEvents dtbCol1 As DataGridTextBox
Private ToolTip1 As New ToolTip
')
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Datagrid1.ReadOnly = True
Dim ts As New DataGridTableStyle
ts.MappingName = ds.Tables(0).TableName
Dim column As New DataGridTextBoxColumn
ts.GridColumnStyles.Add(column)
DataGrid1.TableStyles.Add(ts)
column = DirectCast(ts.GridColumnStyles(0), DataGridTextBoxColumn)
dtbCol1 = DirectCast(column.TextBox, DataGridTextBox)
column.MappingName = ds.Tables(0).Columns(0).ColumnName
column.HeaderText = "Cor"
column.Width = 30
dv = New DataView(ds.Tables(0))
dv.AllowNew = False
DataGrid1.DataSource = dv
End Sub
Private Sub dtbCol1_ToolTip(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles dtbCol1.MouseEnter
ToolTip1.SetToolTip(DirectCast(sender, DataGridTextBox), _
"Row: " & DataGrid1.CurrentRowIndex + 1)
End Sub
///
 
C

Cor Ligthert

Hi Brian,

A dataset is a collection of datatables, where ever you get them, it has to
be there at runtame to be usable with a datagrid. (And the runtime moment is
the moment it is connected as a datasource to the datagrid).
..
A datagrid has always a datasource whatever it is or it has no rows and no
cells.

So why do you not agree with me?

Cor

I am not sure how you can load the grid and not know the column count.
Are you loading the grtid with a dataset? If so see this example.
 
G

Guest

I know that.. and I did agree with you

B

Cor Ligthert said:
Hi Brian,

A dataset is a collection of datatables, where ever you get them, it has to
be there at runtame to be usable with a datagrid. (And the runtime moment is
the moment it is connected as a datasource to the datagrid).
..
A datagrid has always a datasource whatever it is or it has no rows and no
cells.

So why do you not agree with me?

Cor


Are you loading the grtid with a dataset? If so see this example.
 

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