我试图隐藏“Key”或[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;
}
答案 0 :(得分: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:不确定列显示复选框的问题是否允许您检查...
答案 1 :(得分:0)
我必须将所有列的标记添加到html端。关闭AutoGenerateColumns,我打开了EnableAjaxViewState和EnableDataViewState,不确定是否需要它们。但是,通过将其添加到HTML,我就可以调用代码
ig:WebDataGrid ID =“dgvPunchs”runat =“server”Width =“800px” DataKeyFields =“Key”AutoGenerateColumns =“False”EnableAjaxViewState =“True”EnableDataViewState =“True”&gt;
<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;