Can you add button controls in an mvc3 grid view? I have a grid - two coloumns containing data and 5 columns that need to be buttons to cotnrol the next action for the data in the first two columns. Is this possible?
GridView - How to define Button inside grid
Answers approved by DevExpress Support
Hello Nancy,
Yes, it is possible to use the MVCxGridViewColumn.SetDataItemTemplateContent method to define the (Button) content of the required column.
Take a look at the E4236 - How to implement the multi-row editing feature in the GridView Code Central example to see this method in action.
UPDATED:
It is possible to use the same technique to define the required number of columns and accomplish this task. I have attached a sample project that may be helpful.
C#settings.Columns.Add(column => {
column.FieldName = ...
column.SetDataItemTemplateContent(c => {
Html.DevExpress().Button(settingsBtn => {
settingsBtn.Name = "btn" + c.KeyValue;
settingsBtn.Text = "Price Value: " + c.Text;
settingsBtn.ClientSideEvents.Click = string.Format("function(s, e) {{ alert('{0}'); }}", c.KeyValue);
settingsBtn.RouteValues = new { Controller = "Home", Action = "ButtonClickAction", rowKeyValue = c.KeyValue };
}).Render();
});
});
C#public ActionResult ButtonClickAction(int? rowKeyValue) {
return ...
}
Thank you Mike; but this does not help me very much. The example shows a grid with two columns - a textbox and a combobox. What next? My grid needs to look like this:
data field data field button button button button button - for as many rows are retrieved from the database
Each button need to contain the same ID number from the data retrieved for the first two columns and to trigger the creation of a different partial view when each of four of the buttons are clicked by the user and the last button needs to update the database when clicked. Does some sort of a template need to be created for the button columns?
Hello Nancy,
Yes, it is possible to use the same technique to define the required number of columns and accomplish this task. I have attached a sample project that may be helpful.
C#settings.Columns.Add(column => {
column.FieldName = ...
column.SetDataItemTemplateContent(c => {
Html.DevExpress().Button(settingsBtn => {
settingsBtn.Name = "btn" + c.KeyValue;
settingsBtn.Text = "Price Value: " + c.Text;
settingsBtn.ClientSideEvents.Click = string.Format("function(s, e) {{ alert('{0}'); }}", c.KeyValue);
settingsBtn.RouteValues = new { Controller = "Home", Action = "ButtonClickAction", rowKeyValue = c.KeyValue };
}).Render();
});
});
C#public ActionResult ButtonClickAction(int? rowKeyValue) {
return ...
}