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

RadGrid generated from code behind can not be edited

1 Answer 56 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Эрик
Top achievements
Rank 1
Эрик asked on 10 Apr 2019, 03:30 PM

Hi,

I think I have quite a specific task. I have a page, where I need to generate multiple grids, for different types of entities.

And the thing is that on grid generation step I do not know types of entities that I will have, in other words I have a set of 'Type' entities each describing some class, so I can not declare grids in 'aspx' page, but I need to generate them in code behind.

I am trying to generate grid in the code behind using this code:

var grid = new RadGrid();
grid.AutoGenerateColumns = true;
grid.MasterTableView.EditMode = GridEditMode.InPlace;
grid.DataSource = input.GetDataForEngineQueryPosition(job, metricKey); //just collection of entities
grid.Visible = true;
grid.DataBind();

Quite simple, and the code is working, I do see the grid with appropriate columns, but I cant edit the values in the cells.

I also tried to generate columns manually depending on properties provided by 'type' entity, like this:

 

var grid = new RadGrid();
grid.AutoGenerateColumns = false;
var propertiesBindingFlags = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance;
var customizableFields = input.CustomizableProperties;
bool showAll = false;
                
foreach(var property in inputType.GetProperties(propertiesBindingFlags))
{
  var field = new Telerik.Web.UI.GridBoundColumn();
  field.DataField = property.Name;
  field.HeaderText = property.Name;
  field.ReadOnly = false;
  field.Visible = true;
  grid.Columns.Add(field);
}
grid.MasterTableView.EditMode = GridEditMode.InPlace;
grid.DataSource = input.GetDataForEngineQueryPosition(job, metricKey);
grid.Visible = true;
grid.DataBind();

Values are displayed correctly in this case, but unlike for the autogeneration I don't see proper controls for every column (like checkbox) and I understand why.

But I still can't edit the values in the cells.

 

Also, after updating the values I need to submit them back to the server, what is the best way to do this. 

Thanks.

Kind regards,

Erik Martirosyan

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 15 Apr 2019, 12:46 PM
Hi Erik,

By not being able to edit the cell values, you mean that there is no button to turn the cell into edit mode or there are some issues that pop up when trying so? In case the issue is the missing option to make the cell editable, then the solution would be to either set the AutoGenerateEditColumn/AutoGenerateDeleteColumn properties to true or create those columns dynamically. If the issue is due to an error, we would more information on that to be able to help further.

Example code snippets to enable Edit/Delete columns for RadGrid in the code behind:

// Auto Generate Edit and Delete columns
grid.AutoGenerateEditColumn = true;
grid.AutoGenerateDeleteColumn = true;
 
// Create Edit and Delete columns manually
GridButtonColumn editCol = new GridButtonColumn();
editCol.UniqueName = "EditColumn"
editCol.HeaderText = editCol.UniqueName;
editCol.CommandName = "Edit";
editCol.ButtonType = GridButtonColumnType.ImageButton;
grid.MasterTableView.Columns.Add(editCol);
 
GridButtonColumn deleteCol = new GridButtonColumn();
deleteCol.UniqueName = "DeleteColumn";
deleteCol.HeaderText = deleteCol.UniqueName;
deleteCol.CommandName = "Delete";
deleteCol.ButtonType = GridButtonColumnType.ImageButton;
grid.MasterTableView.Columns.Add(deleteCol);

From data binding perspective, the code sample you have shared uses the Simple data binding technique to bind data. This is more than enough to be used for displaying data, however, for more complex scenarios where you're building several grids programmatically and perform CRUD (Create, Read, Update, Detelet) operations, I recommend using either the Advanced Data-binding (Using NeedDataSource Event) or Declarative DataSource to bind data to RadGrid. 

Also, when creating a RadGrid programmatically, there are very specific events that you will need to use, for instance Page Init event. The earliest event which would be the best for creating a RadGrid control dynamically. You can check out the Creating the Grid Entirely in the Code Behind documentation article to learn more about the different ways a Grid can be created in the code behind.

I have also attached a sample WebForms page with a dynamically created Grid and fully functioning CRUD operations. The page can be imported into a WebSite project and it does not require further coding to run.

I hope the information from above will prove helpful.

Kind regards,
Attila Antal
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Эрик
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or