Infragistics WebDataGrid Checkbox and hidden column problems

James Wilson picture James Wilson · Aug 16, 2012 · Viewed 9.2k times · Source

I am trying to hide the column "Key" or [0]. I am also trying to set up check boxes in the below code that the end user can click/unclick. By default I set the checkbox to an unchecked state.

This code dgvPunchs.Columns[0].Hidden = true; is how I found out how to hide a column, but it errors out with the following error.

"Object reference not set to an instance of an object."

Also currently the check boxes display, but the end user is not able to click them. I am baffled. Please help! :)

protected void GenerateSalaryPunchesTable()
        {
            this.dgvPunchs.Rows.Clear();

            string[] DateRange = this.cboPayPeriods.SelectedItem.Text.ToString().Replace(" ", "").Split('-');

            DataTable pDates = new DataTable();

            pDates.Columns.Add("Key");
            pDates.Columns.Add("Date", System.Type.GetType("System.DateTime")); // Date Cell
            pDates.Columns.Add("Worked", System.Type.GetType("System.Boolean")); //Worked CB
            pDates.Columns.Add("Vaction", System.Type.GetType("System.Boolean")); //Vacation CB
            pDates.Columns.Add("Sick", System.Type.GetType("System.Boolean")); //Sick CB
            pDates.Columns.Add("Holiday", System.Type.GetType("System.Boolean")); //Holiday CB
            pDates.Columns.Add("Error", System.Type.GetType("System.String")); //Error

            foreach (DataColumn col in pDates.Columns)
            {
                col.ReadOnly = false;
            }

            pDates.Columns["Key"].ColumnMapping = MappingType.Hidden;

            while (Convert.ToDateTime(DateRange[0]) <= Convert.ToDateTime(DateRange[1]))
            {
                if (Convert.ToDateTime(DateRange[0]).DayOfWeek != DayOfWeek.Saturday & Convert.ToDateTime(DateRange[0]).DayOfWeek != DayOfWeek.Sunday)
                {

                    DataRow nRow = pDates.NewRow();
                    nRow["Key"] = Convert.ToDateTime(DateRange[0].ToString()).ToShortDateString();
                    nRow["Date"]= Convert.ToDateTime(DateRange[0].ToString()).ToShortDateString();
                    nRow["Worked"] = 0;
                    nRow["Vaction"] = 0;
                    nRow["Sick"] = 0;
                    nRow["Holiday"] = 0;
                    nRow["Error"] = "";

                    pDates.Rows.Add(nRow);

                }

                DateRange[0] = Convert.ToDateTime(DateRange[0]).AddDays(1).ToShortDateString();
            }


            dgvPunchs.DataSource = pDates;
            dgvPunchs.DataBind();
            dgvPunchs.Columns[0].Hidden = true;
        }

Answer

Fernando Pardo picture Fernando Pardo · Aug 17, 2012

You need to manually create the columns in WebDataGrid, if you don't, just check columns.count, it will be = 0.
So, you can do this in the init event of the WebDataGrid (before you set the WebDataGrid1.datasource and having WebDataGrid1.autogenertecolumns = false):

Infragistics.Web.UI.GridControls.BoundDataField f;    
Infragistics.Web.UI.GridControls.EditingColumnSetting columnSettingReadOnly;

foreach (System.Data.DataColumn c in pDates.Columns)
            {
                    f = new Infragistics.Web.UI.GridControls.BoundDataField(true);
                    f.DataFieldName = c.ColumnName;
                    f.Key = c.ColumnName;
                    f.Header.Text = c.ColumnName;
                    WebDataGrid1.Columns.Add(f);
// In order to set it as readonly:
columnSettingReadOnly = New Infragistics.Web.UI.GridControls.EditingColumnSetting();
                columnSettingReadOnly.ColumnKey = f.Key;
                columnSettingReadOnly.ReadOnly = True;
                WebDataGrid1.Behaviors.EditingCore.Behaviors.CellEditing.ColumnSettings.Add(columnSettingReadOnly);
            }

Try this above and let us know.

PS: not sure about the issue with column showing a checkbox doesn't allowing you to check it...