RadGridView: Row Header (How to get the Text of the Row Header?)

1 Answer 147 Views
GridView
WEI TZE
Top achievements
Rank 2
Iron
Iron
WEI TZE asked on 09 Nov 2022, 03:58 AM | edited on 09 Nov 2022, 04:11 AM

[C# WinForm]

Regarding the Row Header (as shown in the figure below: (R1, R2, R3,...)

Question:

  1. How to get the Text of the Row Header?  - E.g. string rowHeaderText = radGridView1.Rows[0].......

Reference:

https://www.telerik.com/forums/radgridview-row-header-related-questions

Based on the reference topic, I have attempted the to get the Row Header using Visual Rows.

var cellContent = this.radGridView1.TableElement.VisualRows[0].VisualCells[0].Text;

But seems like the VisualRows doesn't reflect all the rows.

For example, I have populated 77 rows as shown in first figure below.

However, the count of VisualRows is 4 only (as shown in second figure).

 

 

Formatting Row Header:

private void dgvMap_ViewCellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
        {
            if (e.CellElement is GridRowHeaderCellElement)
            {
                    e.CellElement.Text = string.Format("R{0}", dgvMap.CurrentView.ViewInfo.Rows.IndexOf(e.Row) + 1);
                    var element = new RadButtonElement();
                    element.Margin = new Padding(3, 3, 3, 3);
                    element.Padding = new Padding(2, 0, 2, -2);
                    element.ImageAlignment = ContentAlignment.MiddleCenter;
                    element.Alignment = ContentAlignment.MiddleCenter;
                    element.ForeColor = Color.Black;
                    element.Text = string.Format("R{0}", dgvMap.CurrentView.ViewInfo.Rows.IndexOf(e.Row) + 1);
                    element.AutoSizeMode = RadAutoSizeMode.Auto;
                    element.MinSize = new Size(80, 50);
                    e.CellElement.Children.Add(element);
                    e.CellElement.ForeColor = Color.White;
            }

 

Populating Rows & Columns:

            numberOfRow = int.Parse(maxRow);
            numberOfColumn = int.Parse(maxCol);

            for (int j = 1; j <= numberOfColumn; j++)
                dgvMap.Columns.Add(string.Format("C{0}", j));


            for (int k = 1; k <= numberOfRow; k++)
            {
                dgvMap.Rows.Add(new string[] { "" });
            }

            for (int k = 1; k <= numberOfRow; k++)
            {
                for (int j = 1; j <= numberOfColumn; j++)
                {
                    string key = string.Format("{0},{1}", this.dgvMap.TableElement.VisualRows[k-1].VisualCells[0].Text, dgvMap.Columns[j - 1].Name);
                  
                }
            }



 

1 Answer, 1 is accepted

Sort by
1
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 09 Nov 2022, 11:24 AM

Hi WEI TZE LIM

Thank you for the provided details.

The GridRowHeaderCellElement is a visual cell that will be reused when you scroll. This behavior comes from the virtualization mechanism of the RadGridView. That is why the VisualRows contains only the rows in the ViewPort of the RadGridView. May I ask you to share if is there a particular reason to use the GridRowHeaderCellElement to place additional content? I can see that except for the text there is a RadButtonElement that interacts with the current row when pressed. In this case, I would suggest creating an additional column that will store the same button with text as the GridRowHeaderCellElement. This way you can get the key text of the row nevertheless is visible or not.

If this approach is not an option, what else you could try is to use the Tag property of the GridViewRowInfo. You can continue using the approach to place a button with text inside the GridRowHeaderCellElement. You can use the tag property to place only the text of the button. This way you can get its value no matter if the row is visible or not. For example:

foreach (GridViewRowInfo row in this.radGridView1.Rows)
{
    row.Tag = "your info";
}

private void radButton1_Click(object sender, EventArgs e)
{
    var key = this.radGridView1.Rows[100].Tag;
}

I hope one of these two approaches works for you.

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
GridView
Asked by
WEI TZE
Top achievements
Rank 2
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or