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

how do we add row numbers on a radgrid?

12 Answers 575 Views
Grid
This is a migrated thread and some comments may be shown as answers.
GarryP
Top achievements
Rank 1
GarryP asked on 04 Feb 2009, 03:05 PM
Hi,

I wanted to number the rows so that if there are 10 rows i wanted radgrid to display it from 1-10. I used the code below but keep having "System.Data.DataRowView" in the rows.

Dim rowLength As Integer

        For rowLength = 1 To RadGrid1.Items.Count
            RadGrid1.Items.Item(1).Cells.Item(1).Text = rowLength - 1
        Next

Is there any other way doing it so that it can display it on the rows of radgrid.


Thanks

12 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 05 Feb 2009, 04:33 AM
Hello Garry,

You can go through the following help document which explains on how to display row numbers in a label in the TemplateColumn of the grid.
Adding row numbers

Thanks
Princy.

0
GarryP
Top achievements
Rank 1
answered on 05 Feb 2009, 09:44 AM
Hi Princy,

Thanks for the reply. I was writing the project in vb.net so i copied the following code.

Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
 If (TypeOf e.Item Is GridDataItem AndAlso e.Item.OwnerTableView.DataSourceID = "AccessDataSource1") Then
  Dim lbl As Label = CType(e.Item.FindControl("numberLabel"), Label)
  lbl.Text = e.Item.ItemIndex + 1
 End If
End Sub

It was giving a few errors so I amended it to following.

Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.WebControls.GridItemEventArgs) Handles RadGrid1.ItemDataBound
      
dim rowLength as integer

 If (TypeOf e.Item Is Telerik.WebControls.GridDataItem AndAlso e.Item.OwnerTableView.DataSourceID = "SqlDataSource1") Then
            Dim lbl As Label = CType(e.Item.FindControl("lblDictationsDisplayed"), Label)
            lbl.Text = e.Item.ItemIndex + 1
            Dim rowLength As Integer

            For rowLength = 1 To RadGrid1.Items.Count
                RadGrid1.Items.Item(rowLength - 1).Cells.Item(1).Text = rowLength - 1
            Next
        End If
    End Sub

What is label used for? on the code above. it was still displaying "System.Data.DataRowView" and not displaying the numbers.

Thanks
0
Shinu
Top achievements
Rank 2
answered on 05 Feb 2009, 12:12 PM
Hi GarryP,

In the above help article the row number is displayed using a label in a GridTemplateColumn. If you want to insert the row number to the cells first column you may try the following code snippet.

VB:
Protected Sub RadGrid1_PreRender(ByVal sender As ObjectByVal e As EventArgs) 
     Dim rowLength As Integer = 0 
     
     For rowLength = 1 To RadGrid1.MasterTableView.Items.Count 
         Dim RowLength As Integer = rowLength - 1 
         RadGrid1.Items(rowLength - 1).Cells(2).Text = RowLength.ToString() 
         
     Next 
 End Sub 


Thanks
Shinu
0
GarryP
Top achievements
Rank 1
answered on 10 Feb 2009, 04:47 PM
Hi, I tried the code but its not adding it to the row and saying displaying "System.Data.DataRowView" only for that row but on other rows it contained the data as usual. I dont know if the problem is with sqldatasource as it fills the radgrid with it. Is there any other method like sorting where you tick an option and it sorts itself?

Thanks
0
Sebastian
Telerik team
answered on 11 Feb 2009, 07:29 AM
Hello GarryP,

This issue can appear when the DataField value of the column in question does not matches the underlying source column name (the same can be observed with regular MS GridView control). Please revise your code accordingly to address the problem in question.

Best regards,
Sebastian
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Princy
Top achievements
Rank 2
answered on 11 Feb 2009, 08:01 AM
Hello Garry,

You can also try out the following code to set the row numbers:
aspx:
 <telerik:GridBoundColumn UniqueName="RowNumber"></telerik:GridBoundColumn> 

cs:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem dataItem = (GridDataItem)e.Item; 
            dataItem["RowNumber"].Text = Convert.ToString(e.Item.ItemIndex + 1); 
             
        } 
    } 

Thanks
Princy.

0
GarryP
Top achievements
Rank 1
answered on 11 Feb 2009, 10:16 AM
Hi,

The unique name was Number for that coloum as show below

<radG:GridBoundColumn HeaderText="Number" UniqueName="Number" EditFormColumnIndex="1">
                        </radG:GridBoundColumn>

I converted CS code to vb.net as below;

Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.WebControls.GridItemEventArgs)
        If TypeOf e.Item Is Telerik.WebControls.GridDataItem Then
            Dim dataItem As Telerik.WebControls.GridDataItem = DirectCast(e.Item, Telerik.WebControls.GridDataItem)
            dataItem("Number").Text = Convert.ToString(e.Item.ItemIndex + 1)

        End If
    End Sub

Still displaying the same error.



0
Sebastian
Telerik team
answered on 11 Feb 2009, 11:11 AM
Hello GarryP,

From your code snippet I do not see that you set the DataField property of your GridBoundColumn which is a prerequisite to display values from the respective source.
 
Regards,
Sebastian
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
GarryP
Top achievements
Rank 1
answered on 11 Feb 2009, 11:53 AM
Hi,

The code, which you have provided as follows was giving the same error on the radgrid so i converted the one Princy gave to vb.net and still no use.

Protected Sub RadGrid1_PreRender(ByVal sender As ObjectByVal e As EventArgs) 
     Dim rowLength As Integer = 0 
     
     For rowLength = 1 To RadGrid1.MasterTableView.Items.Count 
         Dim RowLength As Integer = rowLength - 1 
         RadGrid1.Items(rowLength - 1).Cells(1).Text = RowLength.ToString() 
         
     Next 
 End Sub 

I created another test project to see but it wasnt working in there too. PreRender and RadGrid1_ItemCommand snipets dont seem to be called at all. What column can i use instead of GridBoundColumn to display the row numbers.

Thanks
0
Shinu
Top achievements
Rank 2
answered on 13 Feb 2009, 06:22 AM
Hi,

You can access the Grid cell either using the column's UniqueName property(which is a better approach) or using the column index. If you are using the column index try accessing the columns starting from position 2 since the first two columns will be the expand/collapse column and rowIndicator column.

VB:
 
 
     Protected Sub RadGrid1_PreRender(ByVal sender As ObjectByVal e As EventArgs) 
         Dim rowLength As Integer = 0 
         
         For rowLength = 1 To RadGrid1.MasterTableView.Items.Count 
             Dim RowLength As Integer = rowLength1 - 1 
             RadGrid1.Items(rowLength - 1).Cells(2).Text = RowLength.ToString() 
         Next 
         
     End Sub 
 
 

Thanks
Shinu
0
GarryP
Top achievements
Rank 1
answered on 13 Feb 2009, 03:58 PM
Thanks.

I used the code below and got it working but there is one slight problem.

Dim rowLength As Integer = 1

        For rowLength = 1 To RadGrid1.MasterTableView.Items.Count
                       RadGrid1.Items.Item(rowLength - 1).Item("Number").Text = rowLength - 1
        Next

It displays from 0 rather than starting from 1 so
0
1
2
3

Is there a way so that it can start from 1.

Thanks
0
GarryP
Top achievements
Rank 1
answered on 13 Feb 2009, 04:01 PM
Please ignore the message above as i used the code below to sort it. Only difference was deleting -1 from row length

 RadGrid1.Items.Item(rowLength - 1).Item("Number").Text = rowLength

Thanks for your help



Tags
Grid
Asked by
GarryP
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
GarryP
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Sebastian
Telerik team
Share this question
or