Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

ASPxGridView - How to display different controls in different cells of the specific column

$
0
0

Problem:
I have two columns in my ASPxGridView. The first column contains values. The second column should contain different controls/editors depending on the values in the first column.

Solution:
In this scenario you need to generate a template for the GridViewDataColumn.DataItemTemplate Property dynamically by implementing the ITemplate Interface. For instance:

[C#]
protectedvoidPage_Init(objectsender,EventArgse){((GridViewDataColumn)ASPxGridView1.Columns["UnitPrice"]).DataItemTemplate=newCustomTemplate();}
[C#]
publicclassCustomTemplate:ITemplate{publicvoidInstantiateIn(Controlcontainer){GridViewDataItemTemplateContainergcontainer=(GridViewDataItemTemplateContainer)container;intunits=Convert.ToInt32(DataBinder.Eval(gcontainer.DataItem,"UnitsOnOrder"));if(units== 0){ASPxLabellabel=newASPxLabel();label.ID="label1";gcontainer.Controls.Add(label);label.Text=DataBinder.Eval(gcontainer.DataItem,"UnitPrice").ToString();label.Width= 100;}else{ASPxSpinEditbutton=newASPxSpinEdit();button.ID="spinEdit1";gcontainer.Controls.Add(button);button.Value=DataBinder.Eval(gcontainer.DataItem,"UnitPrice").ToString();button.Width= 100;button.ClientSideEvents.LostFocus=string.Format("function(s, e) {{ grid.PerformCallback('{0}|' + s.GetValue()); }}",gcontainer.KeyValue);}}}

Here we display a simple label if the "UnitsOnOrder" field value is zero. Otherwise, we display a spin edit control with the editing capability.

See Also:
General information about Template containers


Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>