Referencing a datagrid from a string name

  • Thread starter Mac via DotNetMonster.com
  • Start date
M

Mac via DotNetMonster.com

Hi all,

I have a string variable within a user control that contains the name of a
datagrid control that I want to act on from within my user control.

What I want to achieve is to have the same code within my user control to
call the same methods on whatever datagrid is relevant. But the relevant
datagrid is only known through a string property.

Can anyhelp help me out here? I hope I made some sense.


regards,

Mac
 
C

Cor Ligthert [MVP]

Mac,

Are you sure from the end part that you want to achieve.

It is in my opinon almost impossible to create special DataGrids, because
the DataGrid you use handles always conform his datasource or/and the styles
that you use with the columns of that.

There are some more predefined custom datagrids (and now the datagridview).
However that can not mean that there are more than 5 or 6 and using them
intermixed has in my idea not any sence.

And therefore my question, can you explain to us a little bit more in detail
what that datagrids you are talking about, just to give us more idea's to
make it possible to helps you.

Cor
 
K

Ken Tucker [MVP]

Hi,

I would search through the controls and look for one with the same
name as you are looking for. You can then use directcast to set the
properties you are looking for. Here is a simplified example.

For Each ctrl As Control In Me.Controls

If ctrl.Name = "DataGrid1" Then

DirectCast(ctrl, DataGrid).CaptionVisible = True

DirectCast(ctrl, DataGrid).CaptionText = "Hi there"

End If

Next



Ken
 
M

Mac via DotNetMonster.com

Thanks for that suggestion Ken - I didn't think of looping through the
collection of controls but that is just what I needed.

I had to futher modify your code snippet because I needed to actually look
through all the controls on the relevant parent form and also make sure to
look through all the controls within the controls, because the datagrid I was
after was in a tabfolder. So a handy bit of recursion helped solve this
problem.

The only downside seems to be that using the DirectCast seems to lag a bit.
What I have got in my usercontrol are several buttons, that when pressed will
navigate the currently selected row in the relevant datagrid (Apologies to
Cor - I should have said this in the first post). Of course with your help, I
have all the code to do this within my user control and specifically a click
event for each of these buttons and in each of these events I am using the
DirectCast function.

However, it seems to me that there is a performance issue because when I
click one of these buttons a few times quickly, the execution doesn't keep up
and it moves the current row only once or twice when I may have clicked it 3
times.

Do you know of any performance issues with using direct cast? FYI, the code
that loops through all the controls is executed in the "load" event for the
control. I assume this only occurs once when the form that contains the
instance of the user control is actually loaded.

Also, is there a way to use CType to convert the control I know is a datagrid
to the datagrid type. All I get are exceptions. I assume thats why you
suggested using DirectCast in the first place.


Mac
 
C

Cor Ligthert [MVP]

Mac,

I am still not sure the way you that you use it (It is not needed to explain
now). However if the code from Ken fulfils your need, than you can as well
do.

For Each ctrl As Control In Me.Controls
If ctrl.Name = "DataGrid1" Then
dim myDataGrid as Datagrid = DirectCast(ctrl,Datagrid)
MyDatagrid.CaptionVisible = True
MyDatagrid.CaptionText = "Hi there"
End If

For that you need only to cast once. DirectCast does no conversion. It is
only casting the datagrid to the right type. CType has included a conversion
confirm the rules of IConvertible.

http://msdn.microsoft.com/library/d...ef/html/frlrfsystemiconvertibleclasstopic.asp

I hope this helps,

Cor
 

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