This is a migrated thread and some comments may be shown as answers.

[Solved] Get Columns Name

5 Answers 647 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stefan
Top achievements
Rank 2
Stefan asked on 11 Jul 2013, 05:26 PM

I am dynamically greating columns and assigning a sequence as their name.  How do I get the columns name of the column I have selected?

        For Each column As DataColumn In odt.Columns

            Dim boundColumn As GridBoundColumn

            boundColumn = New GridBoundColumn()
            dgCompGrid.MasterTableView.Columns.Add(boundColumn)

            With boundColumn
                .DataField = column.ColumnName
                .UniqueName = "dgX" & i
                .Display = True
                .HeaderText = column.ColumnName
                .HeaderStyle.ForeColor = Color.Black
                .HeaderStyle.BackColor = Color.White
                .HeaderStyle.Font.Bold = True
                .ItemStyle.Font.Size = 8
                .ItemStyle.ForeColor = Color.Gray
                .ItemStyle.HorizontalAlign = HorizontalAlign.Left
                .HeaderStyle.Font.Size = 8

            End With
            i = i + 1
        Next 'i




So each column will be named dgX1, dgX2 and so on... I just will never know how many so I need be able to get the columns name depending on the cell I have selected in the grid.  Also I need the row number. 

vb code

Thanks,

5 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 16 Jul 2013, 10:11 AM
Hi Stefan,

Thank you for contacting us.

In RadGrid, selected cells column name and row index can be retrieved both client-side and server-side.

For client-side you could use the RadGrid client-side event OnCellSelected or OnCellDeselected and the following code:
function cellSelected(sender, args) {
    var columnName = args.get_column().get_uniqueName();
    var rowIndex = args.get_gridDataItem().get_itemIndexHierarchical();
}

For server-side you should use OnSelectedIndexChanged event and the following:
Protected Sub RadGrid1_SelectedCellChanged(sender As Object, e As EventArgs)
     ......
    Dim columnName = RadGrid1.SelectedCells(i).Item.ItemIndexHierarchical
    Dim rowIndex = RadGrid1.SelectedCells(i).Column.UniqueName
End Sub

Additionally, you could have a look at our online demo "Cell selection functionality" where client-side approach for retrieving information for the selected cells is demonstrated. 
 
If you have any further questions, please feel free to get back to us.
 


Best Regards,
Konstantin Dikov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Stefan
Top achievements
Rank 2
answered on 23 Jul 2013, 02:13 PM
Hi,

I am using batchedit so this event never gets fired.  To clairify I am trying to get the row and cell name while in the BatchEditCommand event.  In VB it would be as follows

For ROW I can use
dgComponents.CurrentRow.Index

For Column Name I can use
dgComponents.Columns(e.ColumnIndex).Name


Is there an equivalant?

 

Remember col names are generated dynamically so I wont be able to use the col name.  I need to use the index of the col selected. 

 

 

Thanks

0
Konstantin Dikov
Telerik team
answered on 24 Jul 2013, 05:21 PM
Hi Stefan,

If you want to get the selected cells on the BatchEditCommand server-side event you just have to move the code that I have provided the last time from SelectedCellChanged handler to the BatchEditCommand event handler (since no arguments from the event are needed). 

Here is a code snippet of how to go through all selected cells and obtain their column UniqueName and row index:
For i As Integer = 0 To RadGrid1.SelectedCells.Count - 1
    Dim cellColumnUniqueName As String = RadGrid1.SelectedCells(i).Column.UniqueName
    Dim cellRowIndex As Integer = Integer.Parse(RadGrid1.SelectedCells(i).Item.ItemIndexHierarchical)
Next

Hope this helps.

Regards,
Konstantin Dikov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Stefan
Top achievements
Rank 2
answered on 25 Jul 2013, 03:05 PM
HI Konstantin,

Sorry let me clarify a bit.  I need the column index of the cell that was actually updated when I click on Save Change in Batch Update. I am using....

 

 

Protected

 

 

Sub dgGrid_BatchEditCommand(sender As Object, e As GridBatchEditingEventArgs) Handles dgGrid.BatchEditCommand

 

    For

 

 

Each item As GridBatchEditingCommand In e.Commands

 

 

 

        Dim editedRow = item

 

 

 

        Dim iRowIndex As Int32

 

 

 

        Dim iColIndex As Int32

 

        iRowIndex = item.Item.ItemIndex

                iColIndex = ??????????
        Next

 

 


End Sub

So since only edit commands are in the class I can not use the ordinal i.  I am able to get the row by using editedRow item.itemindex but I can not figure out how the get the column index of the cell edited. I can get old values, new values but I need the col index as the table is dynmically built given x and y positions.

Do you have any suggestions.  Thank you kindly.

 

0
Konstantin Dikov
Telerik team
answered on 26 Jul 2013, 04:48 PM
Hello Stefan,

We have answered your question in your other post.

Hope that the approach there covers you requirements. 

Best Regards,
Konstantin Dikov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Stefan
Top achievements
Rank 2
Answers by
Konstantin Dikov
Telerik team
Stefan
Top achievements
Rank 2
Share this question
or