【问题标题】:Infragistics WebDataGrid Checkbox and hidden column problemsInfragistics WebDataGrid 复选框和隐藏列问题
【发布时间】:2012-08-15 23:11:43
【问题描述】:

我正在尝试隐藏“键”或 [0] 列。我还尝试在下面的代码中设置最终用户可以单击/取消单击的复选框。默认情况下,我将复选框设置为未选中状态。

此代码dgvPunchs.Columns[0].Hidden = true; 是我发现如何隐藏列的方法,但它出错并出现以下错误。

“对象引用未设置为对象的实例。”

目前也显示复选框,但最终用户无法单击它们。我很困惑。请帮忙! :)

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

【问题讨论】:

    标签: c# asp.net infragistics webdatagrid


    【解决方案1】:

    您需要在WebDataGrid中手动创建列,如果不这样做,只需检查columns.count,它将= 0。
    因此,您可以在 WebDataGrid 的 init 事件中执行此操作(在设置 WebDataGrid1.datasource 并设置 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);
                }
    

    试试上面这个,让我们知道。

    PS:不确定列显示复选框不允许您检查它的问题...

    【讨论】:

    • 我今天早些时候找到了解决方案。这是为了使列绑定并在 html 中为 Infrag 控件添加标记。然后我可以在原帖中调用上面的代码来隐藏它,它终于起作用了。
    【解决方案2】:

    我必须将所有列的标记添加到 html 端。关闭 AutoGenerateColumns,我打开 EnableAjaxViewState 和 EnableDataViewState 不确定是否需要这些。但是通过将其添加到 HTML 中,我就可以调用代码

    ig:WebDataGrid ID="dgvPunchs" runat="server" 宽度="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;
    

    【讨论】:

    • EnableAjaxViewState 和 EnableDataViewState 的设置与能否隐藏列无关。您需要在标记中添加列的原因是只有您定义的列在 Columns 集合中。
    猜你喜欢
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 2012-10-28
    • 2014-12-12
    • 2011-05-15
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    相关资源
    最近更新 更多