我正在尝试隐藏“键”或 [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;
}