Monday, September 11, 2017

Add or Remove a Column to a WebDataGrid

I have been using Infragistics for long, I have gone through this scenario many times. Hope this helps few people.  The following code shows you how to add bound data field and a template data field.

private void AddReferenceFieldColumnstoWebGrid()
            DataTable dtConfigureReffields = crBl.GetConfigureReferenceFields(intCompanyId, intAccountId);

            // NS: Commenting
            // Adding colums dynamically.
            // if we matched these column kyes with grid result
            // binding will take care automatically
            string strReferenceFieldName;
            int intOffset = 11;  // position where to start after ref field. 
             //this is the place where we add these columns. for me i will get dynamic columns
             // from the datatable.
            for (int j = 0; j < dtConfigureReffields.Rows.Count; j++)
                strReferenceFieldName = "";
                BoundDataField boundField1 = new BoundDataField(true);
                strReferenceFieldName = "reference" + (j + 2);
                boundField1.Key = strReferenceFieldName;
                boundField1.Header.Text = dtConfigureReffields.Rows[j]["name"].ToString();
                boundField1.DataFieldName = strReferenceFieldName;

                // ADD COLUMNS
                this.referenceFieldApprovalQueueUltraWebGrid.Columns.Insert(intOffset, boundField1);

To remove column for the grid, add this below code after databind of webdatagrid.