Infragistics WebDataGrid Checkbox and hidden column problems

9.4k Views Asked by At

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;
        }
2

There are 2 best solutions below

1
On BEST ANSWER

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...

1
On

I had to add the markup for all of the columns into the html side. Turn off AutoGenerateColumns, I turned on EnableAjaxViewState and EnableDataViewState not sure if those are needed. But with adding it to the HTML I was then able to call the code

ig:WebDataGrid ID="dgvPunchs" runat="server" Width="800px" DataKeyFields="Key" AutoGenerateColumns="False" EnableAjaxViewState="True" EnableDataViewState="True" >

<Columns>
    <ig:BoundDataField DataFieldName="Key" Key="Key">
        <Header Text="Key" />
    </ig:BoundDataField>
    <ig:BoundDataField DataFieldName="Date" Key="Date">
        <Header Text="Date" />
    </ig:BoundDataField>
    <ig:BoundCheckBoxField DataFieldName="Worked" 
        Key="Worked">
        <Header Text="Worked" />
    </ig:BoundCheckBoxField>
    <ig:BoundCheckBoxField DataFieldName="Vaction" 
        Key="Vaction">
        <Header Text="Vaction" />
    </ig:BoundCheckBoxField>
    <ig:BoundCheckBoxField DataFieldName="Sick" 
        Key="Sick">
        <Header Text="Sick" />
    </ig:BoundCheckBoxField>
    <ig:BoundCheckBoxField DataFieldName="Holiday" 
        Key="Holiday">
        <Header Text="Holiday" />
    </ig:BoundCheckBoxField>
    <ig:BoundDataField DataFieldName="Error" Key="Error">
        <Header Text="Error" />
    </ig:BoundDataField>
</Columns>

<Behaviors>
    <ig:EditingCore>
        <Behaviors>
            <ig:CellEditing>
            </ig:CellEditing>
        </Behaviors>
    </ig:EditingCore>
</Behaviors>

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