Articles in this section
Category / Section

How to format and add isPrimaryKey property to auto generated columns?

1 min read


We can format the auto generated column using dataBound event. While we remove the columns property, grid will automatically generate the column using the field name of the dataSource.

 The following code example shows how to format and add primaryKey to grid column.

    function dataBound(args) {
                    for (var column = 0; column < this.model.columns.length; column++) {
                        if (this.model.columns[column]["type"] == "number") //Align based on column field
                            this.model.columns[column].textAlign = "right";                        
                        if (this.model.columns[column]["field"] == "EmployeeID") //Adding primary key based on field
                            this.model.columns[column].isPrimaryKey = true;
                        else if (this.model.columns[column]["field"] == "Freight") //Formating based on field
                            this.model.columns[column].format = "{0:C}";
                    this.refreshContent(true); //Reflect the changes in grid


<div id="Grid"></div>
<script type="text/javascript">
    $(function () {// Document is ready.        
            dataSource: data, // the datasource "data" is referred from jsondata.min.js            
                      dataBound: "dataBound",


[In View]
namespace EJGrid.Controllers
    public class HomeController : Controller
        public ActionResult Index()
            var DataSource = OrderRepository.GetAllRecords();
   = DataSource;
            return View();


<ej:Grid ID="OrdersGrid" runat="server" >  
            <ClientSideEvents DataBound="dataBound" />              
public partial class _Default : Page
    protected void Page_Load(object sender, EventArgs e)
            this.OrdersGrid.DataSource = new NorthwindDataContext().Orders;

Screen shot:

   Formatted of auto generated column

Did you find this information helpful?
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment