How to Convert

  • Thread starter Thread starter Miro
  • Start date Start date
M

Miro

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("txtCellName"),
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
 
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(nonQuotedVariable),
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?
 
James Hahn said:
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(nonQuotedVariable),
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?


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
 
Miro said:
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("txtCellName"),
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.CustomerNameDataGridViewTextBoxColumn, _
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.
 
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 said:
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("txtCellName"),
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.CustomerNameDataGridViewTextBoxColumn, _
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.CustomerNameDataGridViewTextBoxColumn, _
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
 
Miro wrote:

(1)
How did you get this example to work:
<example>
Me.dgvmyData.Sort(Me.CustomerNameDataGridViewTextBoxColumn, _
  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)
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.
 
Miro wrote:

(1)
How did you get this example to work:
<example>
Me.dgvmyData.Sort(Me.CustomerNameDataGridViewTextBoxColumn, _
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)
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("txtRound"))
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("txtRound"))


Thank you for your patients.

Miro
 
Miro wrote:
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("txtRound"))
        dgvRounds.Columns.Remove("txtRound")
        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.
 
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


Miro wrote:
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("txtRound"))
dgvRounds.Columns.Remove("txtRound")
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.
 
Thank you for the explination. -I think I understand.

Much appreciated, and thanks for being patient.

Cheers'

Miro
 
Back
Top