38 Answers, 1 is accepted
One way to do this is to change the HeaderText property of the corresponding column:
radGridView1.MasterGridViewTemplate.Columns[0].HeaderText = "some text here"; |
If you have another scenario in mind, please, specify it so that we can further help you.
Regards,
Evtim
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
There is no straight-forward way to add text to the row header as the text is not fixed (like the column headers). What you can do is subscribe to the RowFormatting event and control the cell in the header row.
Here is example, which copies the content of the first column into the header column:
private void radGridView1_RowFormatting(object sender, Telerik.WinControls.UI.RowFormattingEventArgs e) |
{ |
GridRowHeaderCellElement grhce = e.RowElement.Children[0] as GridRowHeaderCellElement; |
grhce.Text = string.Format("{0}", e.RowElement.RowInfo.Cells[0].Value); |
} |
Currently the header row does not auto-resize. What you can do is set the width as you like through the theme (using say Visual Style Builder) or by code:
private void Form1_Load(EventArgs e) |
{ |
(radGridView1.GridElement as GridTableElement).RowHeaderColumnWidth = 120; |
} |
If you have further problems, write us back, we'll be glad to help.
Regards,
Evtim
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
Thank you for this question.
You should process the ViewCellFormatting event and check whether the CellElement is GridTableHeaderCellElement. Consider the sample code below:
void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e) |
{ |
if (e.CellElement is GridTableHeaderCellElement) |
{ |
e.CellElement.Text = "Text"; |
} |
} |
I hope this helps. Should you have further questions, do not hesitate to ask.
All the best,
Jack
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
How to alignment of CurrentRowHeaderImage to MiddleLeft, because now it display in MiddleCenter and show on RowHeaderText. It not so Cool!,help me please.
At this time RadGridView doesn't support image alignment in grid cells. We plan to implement this functionality in our upcoming release, due in a couple of months.
Greetings,
Jack
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
I am new with Telerik components and bring my apologies for my (probably) stupid question.
I want to write a number of row in row header column. Can you explain:
1. How can I find row number from RowFormattingEventArgs in RowFormatting event?
2. How can I renumber all rows in case of some row was inserted/deleted?
3. How can I prevent painting an image of pen (on row header cell) during editing some element in row?
Regarding your questions:
1. You can get the row index by calling the IndexOf method of RadGridView.Rows collection. Look at the sample:
int rowIndex = this.radGridView1.Rows.IndexOf((GridViewDataRowInfo)e.RowElement.RowInfo); |
However, this method will be slow when the data source contains many rows. So, It will be better to add a hidden column that contains a pre-calculated row index.
2. This can be avoided when calculating the row index when requested. Otherwise, you should do this manually.
3. Just set the Image property of GridRowHeaderCellElement to null:
void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e) |
{ |
if (e.CellElement is GridRowHeaderCellElement) |
{ |
e.CellElement.Image = null; |
} |
} |
I hope this helps. Should you have any questions, don't hesitate to ask.
Sincerely yours,
Jack
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
After upgrading to Q2-2010 SP2, we are unable to use this suggested method to display the row numbers on the row header because
the line below doesn't work anymore. It returns null instead of the Row Header Cell Element.
GridRowHeaderCellElement grhce = e.RowElement.Children[0] as GridRowHeaderCellElement;
What can we do to continue displaying the row numbers?
Regards,
MP
In our latest release we changed a bit the element hierarchy of RadGridView. This was necessary, because we introduced new features like cell virtualization and extended the frozen column and row functionality. Instead of using the Children collection you have to use the VisualCells one. However, It will be better to use the ViewCellFormatting event as I described in my previous post.
If you have further questions or need assistance, please do not hesitate to contact me.
All the best,
Jack
the Telerik team
private
void
dgvResults_ViewCellFormatting(
object
sender, CellFormattingEventArgs e)
{
GridRowHeaderCellElement grhce = e.CellElement
as
GridRowHeaderCellElement;
if
(grhce !=
null
&& e.CellElement.RowInfo
is
GridViewDataRowInfo)
{
string
RowNumber = (
this
.dgvResults.Rows.IndexOf(grhce.RowInfo) + 1).ToString();
grhce.Text = RowNumber.ToString();
}
}
Telerik winform controls are upgraded to Q1 2012 version. After grouping the gridview by any column the row number in the rowheader.text is not renumbered for reach group.
Example:
1 A
2 B
3 A
Results as
1 A
3 A
2 B
instead of
1 A
2 A
1 B
Yes, the described solution will work only when no sorting, grouping or filtering operation is applied on RadGridView. This is because the Rows collection contains all rows in the way they are seen in the underlying data source. You can access rows in the way they appear in RadGridView when using the ChildRows property. However, this collection is hierarchical. This means that when grouping applies this collection contains only the groups at the first level, to reach the data rows you should iterate the ChildRows property of every group row.
The following method will work in all cases:
public
int
GetRowNumber(RadGridView grid, GridViewRowInfo row)
{
int
index = 0;
return
GetRowNumberCore(row, grid.ChildRows,
ref
index);
}
private
int
GetRowNumberCore(GridViewRowInfo row, GridViewChildRowCollection childRows,
ref
int
index)
{
foreach
(GridViewRowInfo r
in
childRows)
{
if
(r == row)
{
return
index;
}
if
(r
is
GridViewDataRowInfo)
{
index++;
}
int
result = GetRowNumberCore(row, r.ChildRows,
ref
index);
if
(result != -1)
{
return
result;
}
}
return
-1;
}
If you need further assistance, we will be glad to help.
Kind regards,
Jack
the Telerik team
How to change the gridveiw column header or Row header font size ?
You can do this by handling the ViewCellFormatting event. Consider the following sample:
Font customHeaderFont =
new
Font(
"Comic Sans UI"
, 12f);
void
radButton1_ViewCellFormatting(
object
sender, CellFormattingEventArgs e)
{
GridHeaderCellElement cell = e.CellElement
as
GridHeaderCellElement;
if
(cell !=
null
)
{
cell.Font = customHeaderFont;
}
}
Find more about formatting cells in our online documentation.
All the best,
Jack
the Telerik team
thanks for your reply, i have a scenario where i have to display 3 comboboxes inside a single cell in a gridview. each comboboxes bounded to different data. how to achieve this using telerik gridview?
Thanks,
Uday
Could you please let us know whether the comboboxes should be always visible, or should be visible only when the user enters the edit mode of RadGridView? Generally speaking the desired scenario is not supported out of the box, but the API of RadGridView may allow us to achieve something similar to what you require. In addition, describe how the values of these three comboboxes should be saved in a single cell. These details will allow us to discuss the case further.
A bit off topic, I would kindly ask you to open a new thread for each question that is not related to the main topic of an existing thread. This will allow the others interested in the same questions to find the answers more easily.
Nikolay
the Telerik team
sorry for not opening in another thread, Actually i have to display 3 comboboxes in a single cell of a gridview in edit mode. i have already tried out this scenario and succeeded in displaying 3 comboboxes in a single cell of a gridview bt 2nd and 3rd comboxes not opening at all.
thanks
uday
I suppose that the 2nd and the 3rd comboboxes are not opening, because they do not contain any data.
Please find attached a sample project which follows your scenario, but as you can see there, all comboboxes can be opened and they all contain data. You can follow my approach to get three combos at the same time in just one cell. You can also take a look at this article which covers the topic of creating a custom editor.
Nikolay
the Telerik team
Thanks, this solved my problem
Thanks,
Uday
Regards,
Hugo
If you want to format the row header text, you should use the ViewCellFormatting event.
void
rgv_sample_ViewCellFormatting(
object
sender, CellFormattingEventArgs e)
{
GridRowHeaderCellElement headerCell = e.CellElement
as
GridRowHeaderCellElement;
if
(headerCell !=
null
)
{
// TO DO:
}
}
More information regarding row formatting, can be found here: http://www.telerik.com/help/winforms/gridview-rows-formatting-rows.html.
Let us know if you have any other questions.
Svett
the Telerik team
private void radGridView1_ViewCellFormatting( object sender , CellFormattingEventArgs e )
{
if (e.CellElement is GridRowHeaderCellElement)
{
if ( e.CellElement.RowIndex != -1)
{
e.CellElement.Text = ( e.CellElement.RowIndex + 1 ).ToString ( );
radGridView1.TableElement.RowHeaderColumnWidth = ( e.CellElement.RowIndex.ToString ( ).Length > 2 )
? ( e.CellElement.RowIndex.ToString ( ).Length ) * rowHeaderCellWidth / 2
: rowHeaderCellWidth;
}
}
}
The variable rowHeaderCellWidth is the initial value of the row header width which you should get when the form is load.
Thank you for sharing the code snippet with the community. In order to add some text to the row header, it is appropriate to use the RadGridView.ViewCellFormatting event as my colleague Svett suggested in the previous post:
public
Form1()
{
InitializeComponent();
this
.radGridView1.TableElement.RowHeaderColumnWidth = 50;
}
private
void
radGridView1_ViewCellFormatting(
object
sender, CellFormattingEventArgs e)
{
if
(e.CellElement
is
GridRowHeaderCellElement)
{
if
(e.Row
is
GridViewDataRowInfo)
{
e.CellElement.Text = (e.CellElement.RowIndex + 1).ToString();
}
}
}
I hope this information helps. Should you have further questions, I would be glad to help.
Regards,
Desislava
Telerik
RowHeaderImage can textaligment left?
Thanks,
FE
Thank you for writing.
It is necessary to extend the code snippet from my previous post, setting the CellElement.TextImageRelation property to ImageBeforeText in the ViewCellFormatting event.
I hope this information helps. Should you have further questions, I would be glad to help.
Regards,
Desislava
Telerik
How to add a icon on RowHeader by double click this RowHeader?
Reagrds,
Thank you for writing.
You can get rid of the arrow image or apply a different image which indicates the current row by setting the RadGridView.TableElement.CurrentRowHeaderImage property to null or desired image.
this
.radGridView1.TableElement.CurrentRowHeaderImage = Properties.Resources.next;
Additionally, you can assign a header row image to all cells by using the ViewCellFormatting event and setting the Image property to all GridRowHeaderCellElements.
I hope this information helps. Should you have further questions, I would be glad to help.Regards,
Desislava
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
Thank you very much.
I resolved my problems, thanks again for your help.
Best regards,
I'm in need of your help again.
I can apply a image to rowHeader or column[1] by CellClick or CellDoubleClick the current cell with your help, but it changes back when I Click other rows. How to add images to these cells what I clicked, just like the breakpoint of visual studio.
Additionally, I add the SelfReference to two columns which are invisiable. How to change the position of column[0], just before the column "Test Items Description ".
Thank you in advance.
Best regards,
Thank you for writing back.
The appropriate way to apply an image to a specific cell is to use the CellFormatting event. Thus, when a certain cell is clicked, you can store the GridViewRowInfo and the column name of the cell. Afterwards, in the CellFormatting event you can set the image for the specified cell belonging to this row and column. Due to the UI virtualization in RadGridView, cell elements are created only for currently visible cells and are being reused during operations like scrolling, filtering, grouping and so on.
In order to prevent applying the formatting to other columns' cell elements (because of the cell reuse) all customization should be reset for the rest of the cell elements. Please refer to our Formatting Cells help article.
As to the question related to columns position, you can have a look at our Reordering Columns help article.
I hope this information helps. If you have any additional questions, please let me know.
Regards,
Desislava
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
hi dess
thank you for your answer
but i`ve a problem in this situation
when ever i have multi paging ,row index is reset per page
please help me
Hello, Aseman,
RadGridView exposes two collections that contain data rows:
- Rows - contains all data rows that belong to RadGridView. Data operations such as grouping, sorting, filtering, etc. do not change the content of the collection or the order in which the row objects exist in the collection.
- ChildRows - returns the data rows that are currently represented by RadGridView in the order in which they appear. The collection is modified every time a data operation (grouping, sorting, filtering) occurs.
Similar to filtering, sorting and grouping, the ChildRows collection is affected by the paging as well and it contains only the records on the current page. A common scenario is to access the real row index when the paging is enabled in the order the items appear in the grid.
The RadGridView.MasterTemplate.DataView.Indexer offers the Items collection which contains all the rows in the order in which they appear in the grid. The collection is affected every time a data operation like grouping, sorting, filtering occurs. You can use it to extract the correct index when moving through the pages:
private void radGridView1_ViewCellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
if (e.CellElement is GridRowHeaderCellElement && e.Row is GridViewDataRowInfo)
{
GridDataView dataView = this.radGridView1.MasterTemplate.DataView as GridDataView;
e.CellElement.Text = (dataView.Indexer.Items.IndexOf(e.Row) + 1).ToString();
e.CellElement.TextImageRelation = TextImageRelation.ImageBeforeText;
}
else
{
e.CellElement.ResetValue(LightVisualElement.TextImageRelationProperty, ValueResetFlags.Local);
}
}
I hope this information helps. If you need any further assistance please don't hesitate to contact me.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
I am just testing that: as my grid is displayed, one of the rows in the second group is the first item in DataView.Indexer collection. That right there seems to contradict the above answer. Or from another angle: the first group has 5 records. Doesn't it mean that the first record in the second group would be DataView.Indexer[5] (assuming the group header rows don't count (btw, do they count ?) )..
Hello, Robert,
The RadGridView.MasterTemplate.DataView.Indexer offers the Items collection which contains all the rows in the order in which they appear in the grid. The collection is affected every time a data operation like grouping, sorting, filtering occurs. The Indexer can not be accessed by index as you suggested.
If you want to access the rows after grouping you should use the ChildRows collections. The collection is modified every time a data operation (grouping, sorting, filtering) occurs. More information and examples are available here: https://docs.telerik.com/devtools/winforms/controls/gridview/rows/rows-vs-childrows
I hope this helps. Let me know if you need further assistance.
Regards,
Nadya
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.