By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

How to Convert

P: n/a
How can I convert this part of the line:
Me.dgvmyData.Columns.Item("txtCellName") ' "txtCellName"
which is within this line -
Me.dgvmyData.Sort(Me.dgvmyData.Columns.Item("txtCe llName"),
System.ComponentModel.ListSortDirection.Ascending)

To read something like:
Me.dgvmyData.Columns.Item( nonQuotedVariable )

So I do not have to use a "String" variable to specify the column name.
I made an error in on one of my datagrids and Im sure there is a way to
specify the column name without using a string character that could result
in errors ( like I created ).

Thanks

Miro

Sep 19 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
To use a non-quoted variable as the column identifier, simply assign the
column name to a variable:

nonQuotedVariable = "txtCellName"
Me.dgvmyData.Sort(Me.dgvmyData.Columns.Item(nonQuo tedVariable),
System.ComponentModel.ListSortDirection.Ascending)

You are not using a "String" variable to specify the column name - you are
using a string literal. The change suggested above replaces the string
literal with a string variable. You must use string - either a literal or a
variable, to identify the column.

However, if you know the column index you can use an integer variable to
specify the column in the sort instruction. Is your question that you don't
know how to get the column index of a column where all you know is the
column name?

"Miro" <mi**@beero.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
How can I convert this part of the line:
Me.dgvmyData.Columns.Item("txtCellName") ' "txtCellName"
which is within this line -
Me.dgvmyData.Sort(Me.dgvmyData.Columns.Item("txtCe llName"),
System.ComponentModel.ListSortDirection.Ascending)

To read something like:
Me.dgvmyData.Columns.Item( nonQuotedVariable )

So I do not have to use a "String" variable to specify the column name.
I made an error in on one of my datagrids and Im sure there is a way to
specify the column name without using a string character that could result
in errors ( like I created ).

Thanks

Miro
Sep 19 '08 #2

P: n/a

"James Hahn" <jh***@yahoo.comwrote in message
news:e3**************@TK2MSFTNGP02.phx.gbl...
To use a non-quoted variable as the column identifier, simply assign the
column name to a variable:

nonQuotedVariable = "txtCellName"
Me.dgvmyData.Sort(Me.dgvmyData.Columns.Item(nonQuo tedVariable),
System.ComponentModel.ListSortDirection.Ascending)

You are not using a "String" variable to specify the column name - you are
using a string literal. The change suggested above replaces the string
literal with a string variable. You must use string - either a literal or
a variable, to identify the column.

However, if you know the column index you can use an integer variable to
specify the column in the sort instruction. Is your question that you
don't know how to get the column index of a column where all you know is
the column name?

"Miro" <mi**@beero.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>How can I convert this part of the line:
Me.dgvmyData.Columns.Item("txtCellName") ' "txtCellName"
which is within this line -
Me.dgvmyData.Sort(Me.dgvmyData.Columns.Item("txtC ellName"),
System.ComponentModel.ListSortDirection.Ascending )

To read something like:
Me.dgvmyData.Columns.Item( nonQuotedVariable )

So I do not have to use a "String" variable to specify the column name.
I made an error in on one of my datagrids and Im sure there is a way to
specify the column name without using a string character that could
result in errors ( like I created ).

Thanks

Miro

Hi James,

I am not looking to "assign" my column name to a string variable... I am
looking for something like this:
( I just dont know the right terminology I am looking for - it might be
"IntelliSense" )
Lets pretend we are just dealing with this part of the line:
Me.dgvmyData.Columns.Item("txtCellName")

I am looking to do something like this:
Me.dgvmyData.Columns.Item.txtCellName

(So I get an error at design time if I miss-spell txtCellName to
txtCellTypoName vs getting an error at runtime)

For example...
Here is the same example how it would relate to with a typed dataset:

Lets say I do this:
Dim currentRow As DataRow
'Then fill the datarow
'Then to reference the datarow I would have to do something like this:
currentRow ("AFieldInTheTable") = "Hello"

But I am better off writing this example like this:
Dim currentRow as Dataset.DataTableRow
'Then fill the datarow
'*** Now notice how I cant make an error getting the field name because:
currentRow.AFieldInTheTable = "Hello"
'is better than type this out currentRow ("AFieldInTypoTheTable") and now I
get an exception error at runtime vs an error at design time.

Thanks

Miro

Sep 20 '08 #3

P: n/a
Miro wrote:
How can I convert this part of the line:
Me.dgvmyData.Columns.Item("txtCellName") * ' "txtCellName"
which is within this line -
Me.dgvmyData.Sort(Me.dgvmyData.Columns.Item("txtCe llName"),
System.ComponentModel.ListSortDirection.Ascending)

To read something like:
Me.dgvmyData.Columns.Item( nonQuotedVariable )

So I do not have to use a "String" variable to specify the column name.
<snip>

If you are defining the DataGridView content at design time, then the
designer creates named columns for you, which you can use instead of
refering to the columns by index or name.

These automaticaly created columns will have names such as
DataGridViewTextBoxColumn1, DataGridViewTextBoxColumn2, etc, or
something like it, depending on how you created the association
between the GridView and the data source.

Suppose then, that the column you want to sort is for a field named
CustomerName in the data source. Chances are you'll have a control
named CustomerNameDataGridViewTextBoxColumn (ouch!). You can use this
control explicitly in your call to Sort:

<example>
Me.dgvmyData.Sort(Me.CustomerNameDataGridViewTextB oxColumn, _
System.ComponentModel.ListSortDirection.Ascending)
</example>

(Personaly, I change all column names upon creation to something more
manageable. You can do that by selecting "Edit Columns..." in the
properties window and edit the column's (Name) field in the Design
section)
Hope this helps.

Branco.
Sep 20 '08 #4

P: n/a
Since the column name can be changed at run time, it's not possible to hard
code the name and be certain that there will not be an error at run time.
Use the column index instead of the name. That virtually eliminates the
possibility of a typo.

"Miro" <mi**@beero.comwrote in message
news:Om**************@TK2MSFTNGP05.phx.gbl...
>
"James Hahn" <jh***@yahoo.comwrote in message
news:e3**************@TK2MSFTNGP02.phx.gbl...
>To use a non-quoted variable as the column identifier, simply assign the
column name to a variable:

nonQuotedVariable = "txtCellName"
Me.dgvmyData.Sort(Me.dgvmyData.Columns.Item(nonQu otedVariable),
System.ComponentModel.ListSortDirection.Ascending )

You are not using a "String" variable to specify the column name - you
are using a string literal. The change suggested above replaces the
string literal with a string variable. You must use string - either a
literal or a variable, to identify the column.

However, if you know the column index you can use an integer variable to
specify the column in the sort instruction. Is your question that you
don't know how to get the column index of a column where all you know is
the column name?

"Miro" <mi**@beero.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>>How can I convert this part of the line:
Me.dgvmyData.Columns.Item("txtCellName") ' "txtCellName"
which is within this line -
Me.dgvmyData.Sort(Me.dgvmyData.Columns.Item("txt CellName"),
System.ComponentModel.ListSortDirection.Ascendin g)

To read something like:
Me.dgvmyData.Columns.Item( nonQuotedVariable )

So I do not have to use a "String" variable to specify the column name.
I made an error in on one of my datagrids and Im sure there is a way to
specify the column name without using a string character that could
result in errors ( like I created ).

Thanks

Miro


Hi James,

I am not looking to "assign" my column name to a string variable... I am
looking for something like this:
( I just dont know the right terminology I am looking for - it might be
"IntelliSense" )
Lets pretend we are just dealing with this part of the line:
Me.dgvmyData.Columns.Item("txtCellName")

I am looking to do something like this:
Me.dgvmyData.Columns.Item.txtCellName

(So I get an error at design time if I miss-spell txtCellName to
txtCellTypoName vs getting an error at runtime)

For example...
Here is the same example how it would relate to with a typed dataset:

Lets say I do this:
Dim currentRow As DataRow
'Then fill the datarow
'Then to reference the datarow I would have to do something like this:
currentRow ("AFieldInTheTable") = "Hello"

But I am better off writing this example like this:
Dim currentRow as Dataset.DataTableRow
'Then fill the datarow
'*** Now notice how I cant make an error getting the field name because:
currentRow.AFieldInTheTable = "Hello"
'is better than type this out currentRow ("AFieldInTypoTheTable") and now
I get an exception error at runtime vs an error at design time.

Thanks

Miro


Sep 20 '08 #5

P: n/a

"Branco Medeiros" <br*************@gmail.comwrote in message
news:a4**********************************@a70g2000 hsh.googlegroups.com...
Miro wrote:
How can I convert this part of the line:
Me.dgvmyData.Columns.Item("txtCellName") ' "txtCellName"
which is within this line -
Me.dgvmyData.Sort(Me.dgvmyData.Columns.Item("txtCe llName"),
System.ComponentModel.ListSortDirection.Ascending)

To read something like:
Me.dgvmyData.Columns.Item( nonQuotedVariable )

So I do not have to use a "String" variable to specify the column name.
<snip>

If you are defining the DataGridView content at design time, then the
designer creates named columns for you, which you can use instead of
refering to the columns by index or name.

These automaticaly created columns will have names such as
DataGridViewTextBoxColumn1, DataGridViewTextBoxColumn2, etc, or
something like it, depending on how you created the association
between the GridView and the data source.

Suppose then, that the column you want to sort is for a field named
CustomerName in the data source. Chances are you'll have a control
named CustomerNameDataGridViewTextBoxColumn (ouch!). You can use this
control explicitly in your call to Sort:

<example>
Me.dgvmyData.Sort(Me.CustomerNameDataGridViewTextB oxColumn, _
System.ComponentModel.ListSortDirection.Ascending)
</example>

(Personaly, I change all column names upon creation to something more
manageable. You can do that by selecting "Edit Columns..." in the
properties window and edit the column's (Name) field in the Design
section)
Hope this helps.

Branco.

=================

Hi Branco,

How did you get this example to work:
<example>
Me.dgvmyData.Sort(Me.CustomerNameDataGridViewTextB oxColumn, _
System.ComponentModel.ListSortDirection.Ascending)
</example>

By putting 'just the txtColumnName' in the first variable of the sort - you
will get a runtime error that the column does not exist in the datagridview.

By going back to my original example, and using the
Me.dgvmyData.Columns.Item("txtColName") example, this only allows 2
overloads. 1 is the column index, and 2 is the Name as string.
I am looking for some overload to put the actual column name in there.

I do not want to use the 'string' example cause I came accross this by
making a type.
I do not want to use a column index cause if i forget this 'sort' is in my
code and i reference the index as a number and go re-arrange my grid later
on - i will cause another bug.

I am starting to think there is no way for intellisense to do this, Unless I
create a quick 'do loop' and fly thru the columns in my datagrid view.
Search for my "txtColName" - if it found it - use that column index, if it
didnt, do not sort.
Kind of silly but thats my only current solution.

Miro


Sep 20 '08 #6

P: n/a
Miro wrote:

(1)
<snip>
How did you get this example to work:
<example>
Me.dgvmyData.Sort(Me.CustomerNameDataGridViewTextB oxColumn, _
* System.ComponentModel.ListSortDirection.Ascending)
</example>

By putting 'just the txtColumnName' in the first variable of the sort - you
will get a runtime error that the column does not exist in the datagridview.
</snip>

(2)
<snip>
By going back to my original example, and using the
Me.dgvmyData.Columns.Item("txtColName") example, this only allows 2
overloads. *1 is the column index, and 2 is the Name as string.
I am looking for some overload to put the actual column name in there.
</snip>

Hi, Miro!

1) Of course, you must use the actual control name, I don't know how
the editor named the controls for the columns you are using. To see
the actual control name, right-click the grid, select "Edit
columns...", select the column in the left panel (under "selected
columns") and verify its name in the right panel (under "Bound Column
Properties"). The control name will be in the (Name) field.

2) Maybe I'm not being clear enough. I'm talking about the actual
column control as parameter for the **DataGridView.Sort** method, not
the parameter for the **DataGriView.Column.Item** property.

These column controls were created automatically for you and are
accessible at form level, just type Me, dot, and presto!, the control
list will appear, pick the column control name from there! =))).
(That's why I rename then, it's way easier to pick then up when you
actually know the control's name! =))

You are correct in not wanting to use the "string name" or the
column's index. If you ever need a column's name or index, you can get
then from the control itself (from the properties "Name", "Index" or
"DisplayIndex").

Hope this helps.

Branco.
Sep 20 '08 #7

P: n/a

"Branco Medeiros" <br*************@gmail.comwrote in message
news:16**********************************@y38g2000 hsy.googlegroups.com...
Miro wrote:

(1)
<snip>
How did you get this example to work:
<example>
Me.dgvmyData.Sort(Me.CustomerNameDataGridViewTextB oxColumn, _
System.ComponentModel.ListSortDirection.Ascending)
</example>

By putting 'just the txtColumnName' in the first variable of the sort -
you
will get a runtime error that the column does not exist in the
datagridview.
</snip>

(2)
<snip>
By going back to my original example, and using the
Me.dgvmyData.Columns.Item("txtColName") example, this only allows 2
overloads. 1 is the column index, and 2 is the Name as string.
I am looking for some overload to put the actual column name in there.
</snip>

Hi, Miro!

1) Of course, you must use the actual control name, I don't know how
the editor named the controls for the columns you are using. To see
the actual control name, right-click the grid, select "Edit
columns...", select the column in the left panel (under "selected
columns") and verify its name in the right panel (under "Bound Column
Properties"). The control name will be in the (Name) field.

2) Maybe I'm not being clear enough. I'm talking about the actual
column control as parameter for the **DataGridView.Sort** method, not
the parameter for the **DataGriView.Column.Item** property.

These column controls were created automatically for you and are
accessible at form level, just type Me, dot, and presto!, the control
list will appear, pick the column control name from there! =))).
(That's why I rename then, it's way easier to pick then up when you
actually know the control's name! =))

You are correct in not wanting to use the "string name" or the
column's index. If you ever need a column's name or index, you can get
then from the control itself (from the properties "Name", "Index" or
"DisplayIndex").

Hope this helps.

Branco.
==============================

Hi Branco,

I must still be missing something:

My datagrid name is dgvRounds
My column ( or one of them ) is named 'txtRounds' - i have already renamed
it.
I have my data filled in the datagrid.

What I am understanding is that I can sort my datagrid like this ( by your
code )

Me.dgvRounds.Sort(Me.txtRound,
System.ComponentModel.ListSortDirection.Ascending)

The error I get - during runtime is:
Column provided does not belong to this DataGridVew control

The way I am adding the column is like this:
On the form load:
Dim index As Integer
Dim dgvMaskedEdit As DataGridViewMaskedEditColumn 'custom class
that is a masked edit column

'find old column that is just a text box and remove it and add the
masked edit text box in its place.
index = dgvRounds.Columns.IndexOf(dgvRounds.Columns("txtRo und"))
dgvRounds.Columns.Remove("txtRound")
' create a new custom column
dgvMaskedEdit = New DataGridViewMaskedEditColumn
dgvMaskedEdit.Mask = "###"
dgvMaskedEdit.PromptChar = " "c
dgvMaskedEdit.ValidatingType = GetType(Integer)
dgvMaskedEdit.Name = "txtRound"
dgvMaskedEdit.DataPropertyName = "PlayRound"
dgvMaskedEdit.HeaderText = "Round"
' some more tweaking
dgvMaskedEdit.SortMode = DataGridViewColumnSortMode.Programmatic
' insert the new column at the same location
dgvRounds.Columns.Insert(index, dgvMaskedEdit)
------
so i can access Me.txtRound as a control, but I cannot figure out how to
sort it by that column using the datagridview, unless i use "txtRound"
You mentioned the "method"...im looking for help here:
http://msdn.microsoft.com/en-us/library/ms171608.aspx
and cant seem to find what you are refering to.
I have found this link:
http://msdn.microsoft.com/en-us/library/0868ft3z.aspx

In the example given, it gets the index thru searching which column is
selected.

I know my index by index =
dgvRounds.Columns.IndexOf(dgvRounds.Columns("txtRo und"))
Thank you for your patients.

Miro




Sep 20 '08 #8

P: n/a
Miro wrote:
"Branco Medeiros" <br*************@gmail.comwrote in message
news:16**********************************@y38g2000 hsy.googlegroups.com...
Miro wrote:
Me.dgvRounds.Sort(Me.txtRound,
System.ComponentModel.ListSortDirection.Ascending)
This will be compiled at compile time to refer to the existing column in the
datagrid.
dgvRounds.Columns.Remove("txtRound") ' create a new custom
Now it is not in the grid.
dgvMaskedEdit.Name = "txtRound"
' insert the new column at the same location
dgvRounds.Columns.Insert(index, dgvMaskedEdit)
That's not the same column, even if it has the same name. :(

Sep 20 '08 #9

P: n/a
Miro wrote:
<snip>
My datagrid name is dgvRounds
My column ( or one of them ) is named 'txtRounds' - i have already renamed
it.
I have my data filled in the datagrid.

What I am understanding is that I can sort my datagrid like this ( by your
code )

Me.dgvRounds.Sort(Me.txtRound,
System.ComponentModel.ListSortDirection.Ascending)

The error I get - during runtime is:
Column provided does not belong to this DataGridVew control

The way I am adding the column is like this:
On the form load:
* * * * Dim index As Integer
* * * * Dim dgvMaskedEdit As DataGridViewMaskedEditColumn *'custom class
that is a masked edit column

* * * * 'find old column that is just a text box and remove it and add the
masked edit text box in its place.
* * * * index = dgvRounds.Columns.IndexOf(dgvRounds.Columns("txtRo und"))
* * * * dgvRounds.Columns.Remove("txtRound")
<snip>
* * * * dgvRounds.Columns.Insert(index, dgvMaskedEdit)
<snip>

As Steve pointed out, once you remove txtRound from the grid, it
doesn't belong to the gridview anymore. The column you must use to
sort is dgvMaskedEdit.

<example>
Me.dgvRounds.Sort(Me.dgvMaskedEdit, _
System.ComponentModel.ListSortDirection.Ascending)
</example>

The runtime "Name" property has (almost) nothing to do with the
control's name, which is what you must use =))

Hope that helped.

Branco.
Sep 20 '08 #10

P: n/a
Thank you Steve and Branco,

I have tested out what you have said and you are correct. -As you
suspected, and as I suspected :-)
-By me not "removing" the field, I can directly sort on it using the
Me.txtRounds control name.

I just dont seem to understand why the original code does fail still.
I am adding my "column" back into the datagridview as a column so therefore
it does exist in the controls collection.
So,
dgvRounds.Columns.Insert(index, dgvMaskedEdit)
should be adding the "txtRound" column into the dgvRounds.Columns collection

My problem is that I cannto use dgvMaskedEdit as its only decalred in a mini
sub, and is re-used about 5 or 6 times replacing a couple columns.

If this is not really changing the "Name"
>dgvMaskedEdit.Name = "txtRoundTime"
"The runtime "Name" property has (almost) nothing to do with the
control's name, which is what you must use =))"

Then I am assuming there is no real way to change the runtime's name to be
the REAL controls NAME property?

Thanks

Miro
"Branco Medeiros" <br*************@gmail.comwrote in message
news:95**********************************@f63g2000 hsf.googlegroups.com...
Miro wrote:
<snip>
My datagrid name is dgvRounds
My column ( or one of them ) is named 'txtRounds' - i have already renamed
it.
I have my data filled in the datagrid.

What I am understanding is that I can sort my datagrid like this ( by your
code )

Me.dgvRounds.Sort(Me.txtRound,
System.ComponentModel.ListSortDirection.Ascending)

The error I get - during runtime is:
Column provided does not belong to this DataGridVew control

The way I am adding the column is like this:
On the form load:
Dim index As Integer
Dim dgvMaskedEdit As DataGridViewMaskedEditColumn 'custom class
that is a masked edit column

'find old column that is just a text box and remove it and add the
masked edit text box in its place.
index = dgvRounds.Columns.IndexOf(dgvRounds.Columns("txtRo und"))
dgvRounds.Columns.Remove("txtRound")
<snip>
dgvRounds.Columns.Insert(index, dgvMaskedEdit)
<snip>

As Steve pointed out, once you remove txtRound from the grid, it
doesn't belong to the gridview anymore. The column you must use to
sort is dgvMaskedEdit.

<example>
Me.dgvRounds.Sort(Me.dgvMaskedEdit, _
System.ComponentModel.ListSortDirection.Ascending)
</example>

The runtime "Name" property has (almost) nothing to do with the
control's name, which is what you must use =))

Hope that helped.

Branco.

Sep 24 '08 #11

P: n/a
Miro wrote:
>
If this is not really changing the "Name"
>dgvMaskedEdit.Name = "txtRoundTime"

"The runtime "Name" property has (almost) nothing to do with the
control's name, which is what you must use =))"

Then I am assuming there is no real way to change the runtime's name
to be the REAL controls NAME property?
You are confusing the Name property with the name given to a reference variable
in your code.

When you do Dim txtRounds As TextColumn, you have a reference variable for a
column. Often, but not always or necessarily, the column will also have its Name
property = "txtRounds". But that is the Name of the column, not the name of your
reference variable.

Once you do
dgvMaskedEdit = New DataGridViewMaskedEditColumn

you can assign it any Name you like. But if you want to refer to the column by a
variable, use dgvMaskedEdit, not the column name.

Once you have done
dgvRounds.Columns.Insert(index, dgvMaskedEdit)

you should be able to sort by that column using
dgvRounds.Sort(dgvMaskedEdit)

The only use for the Name would be something like
dgvRounds.Sort(dgvRounds.Columns.Item("txtRounds") )

which pretty much defeats the intent of not using string names.

Sep 24 '08 #12

P: n/a
Thank you for the explination. -I think I understand.

Much appreciated, and thanks for being patient.

Cheers'

Miro

"Steve Gerrard" <my********@comcast.netwrote in message
news:JO******************************@comcast.com. ..
Miro wrote:
>>
If this is not really changing the "Name"
>>dgvMaskedEdit.Name = "txtRoundTime"

"The runtime "Name" property has (almost) nothing to do with the
control's name, which is what you must use =))"

Then I am assuming there is no real way to change the runtime's name
to be the REAL controls NAME property?

You are confusing the Name property with the name given to a reference
variable in your code.

When you do Dim txtRounds As TextColumn, you have a reference variable for
a column. Often, but not always or necessarily, the column will also have
its Name property = "txtRounds". But that is the Name of the column, not
the name of your reference variable.

Once you do
dgvMaskedEdit = New DataGridViewMaskedEditColumn

you can assign it any Name you like. But if you want to refer to the
column by a variable, use dgvMaskedEdit, not the column name.

Once you have done
dgvRounds.Columns.Insert(index, dgvMaskedEdit)

you should be able to sort by that column using
dgvRounds.Sort(dgvMaskedEdit)

The only use for the Name would be something like
dgvRounds.Sort(dgvRounds.Columns.Item("txtRounds") )

which pretty much defeats the intent of not using string names.


Sep 24 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.