From c0839c8a2d4a006908f3ec3ab9a2f7b9f3bed7b2 Mon Sep 17 00:00:00 2001 From: SwathiDhatchanamoorthi Date: Tue, 29 Nov 2022 18:24:58 +0530 Subject: [PATCH] WF-64803-Updated the README.md file --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d503bd0..d38c0e7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,40 @@ -# how-to-add-the-button-and-text-in-gridtextcolumn-in-winforms-datagrid -How to add the Button and Text in GridTextColumn in WinForms DataGrid (SfDataGrid)? +# How to add the Button and Text in GridTextColumn in WinForms DataGrid (SfDataGrid) + +By default, you can add either text or button in a GridColumn but, you can add both button and text in a column by customizing [GridTextBoxCellRenderer](https://help.syncfusion.com/cr/Syncfusion.WinForms.DataGrid.Renderers.GridTextBoxCellRenderer.html?_gl=1*1cyquje*_ga*NzY2NDkwMTMwLjE2NTA1MzA5NTc.*_ga_WC4JKKPHH0*MTY2OTcyMzQ0My4zMjIuMS4xNjY5NzI2MzE3LjAuMC4w&_ga=2.93241946.696256746.1669612014-766490130.1650530957). In the custom renderer, the OnRender method can be overridden to draw buttons in the cells. + +``` +public class GridTextButtonCellRenderer : GridTextBoxCellRenderer +{ +public GridTextButtonCellRenderer(SfDataGrid dataGrid) +{ + IsEditable = true; + DataGrid = dataGrid; +} +protected override void OnRender(Graphics paint, Rectangle cellRect, string cellValue, CellStyleInfo style, DataColumnBase column, RowColumnIndex rowColumnIndex) + { + base.OnRender(paint, cellRect, cellValue, style, column, rowColumnIndex); + + //To set the rectangle for button in the cell. + var rect = new Rectangle(cellRect.Location.X + cellRect.Width - 22, cellRect.Location.Y, 20, cellRect.Height); + + (column.GridColumn as GridTextButtonColumn).CellButton = new CellButton(); + (column.GridColumn as GridTextButtonColumn).CellButton.Image = Image.FromFile(@"..\..\Images\icons.png"); + (column.GridColumn as GridTextButtonColumn).CellButton.TextImageRelation = TextImageRelation.ImageBeforeText; + + PropertyInfo highlightedItemProperty = (column.GridColumn as GridTextButtonColumn).CellButton.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance).Single(pi => pi.Name == "Bounds"); + highlightedItemProperty.SetValue((column.GridColumn as GridTextButtonColumn).CellButton, rect); + + //To draw the button in cell + DrawButton(paint, cellRect, rect, "...", new ButtonCellStyleInfo(), column, rowColumnIndex); + +} +} +``` + +``` +//To add custom renderer into SfDataGrid. +this.sfDataGrid.CellRenderers.Add("TextButton", new GridTextButtonCellRenderer(this.sfDataGrid)); + +//To add TextButtonColumn in grid +this.sfDataGrid.Columns.Add(new GridTextButtonColumn() { MappingName = "CustomerID", Width = 140 }); +``` \ No newline at end of file