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
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.UniqueNameEnd SubAdditionally, 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
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
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)NextHope this helps.
Regards,
Konstantin Dikov
Telerik
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.
We have answered your question in your other post.
Hope that the approach there covers you requirements.
Best Regards,
Konstantin Dikov
Telerik
