我有一个奇怪的问题,我想弄清楚。我正在编制时间表,我应该将日历中的“时间”导入其中。
我想我必须在其中包含标题行或表头的日期。我会为每一栏命名吗?
示例:
这是我的时间表
所以,让我说我将于3月1日起飞度假(这是星期四,将在第2周)。所以,它将在第12栏 - “星期四”。
我希望它能够在12小时/星期四的假期行/列中自动放入8小时。
我不太确定如何设置,然后执行。设置目前是一个更大的问题。我已经设置了表格,但我认为我需要进行调整以适应列名等。我将使用C#进行所有计算和填充以及所有爵士乐。在aspx页面中执行该表。就格式化而言,如果你想看,我可以发布代码,尽管它很大。
第一个问题:如何创建具有ID的表头行?
第二个问题:同样的事情,但对于行?
第三个问题:如果我有特定日期,我想放在表格中的特定位置。我怎么做?
任何建议或想法都会有所帮助
答案 0 :(得分:5)
您应该使用绑定到某些基础数据源的GridView。例如:
在aspx中创建GridView:
<asp:GridView ID="TimeSheetGrid" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Sunday1" HeaderText="Sunday" />
<asp:BoundField DataField="Monday1" HeaderText="Monday" />
<asp:BoundField DataField="Tuesday1" HeaderText="Tuesday" />
<asp:BoundField DataField="Wednesday1" HeaderText="Wednesday" />
<asp:BoundField DataField="Thursday1" HeaderText="Thursday" />
<asp:BoundField DataField="Friday1" HeaderText="Friday" />
<asp:BoundField DataField="Saturday1" HeaderText="Saturday" />
<asp:BoundField DataField="Sunday2" HeaderText="Sunday" />
<asp:BoundField DataField="Monday2" HeaderText="Monday" />
<asp:BoundField DataField="Tuesday2" HeaderText="Tuesday" />
<asp:BoundField DataField="Wednesday2" HeaderText="Wednesday" />
<asp:BoundField DataField="Thursday2" HeaderText="Thursday" />
<asp:BoundField DataField="Friday2" HeaderText="Friday" />
<asp:BoundField DataField="Saturday2" HeaderText="Saturday" />
</Columns>
</asp:GridView>
创建一个类来保存信息:
class TimeSheetItem
{
public string Name { get; set; }
public int Sunday1 { get; set; }
public int Monday1 { get; set; }
public int Tuesday1 { get; set; }
public int Wednesday1 { get; set; }
public int Thursday1 { get; set; }
public int Friday1 { get; set; }
public int Saturday1 { get; set; }
public int Sunday2 { get; set; }
public int Monday2 { get; set; }
public int Tuesday2 { get; set; }
public int Wednesday2 { get; set; }
public int Thursday2 { get; set; }
public int Friday2 { get; set; }
public int Saturday2 { get; set; }
}
设计一种方法以某种方式(从数据库等)检索您的信息。以下是我的例子:
private List<TimeSheetItem> GetData()
{
return new List<TimeSheetItem>()
{
new TimeSheetItem() { Name = "IN1" }
,new TimeSheetItem() { Name = "IN2" }
,new TimeSheetItem() { Name = "IN3" }
,new TimeSheetItem() { Name = "IN4" }
,new TimeSheetItem() { Name = "Personal" }
,new TimeSheetItem() { Name = "Doctor" }
,new TimeSheetItem() { Name = "Other" }
,new TimeSheetItem() { Name = "Sick" }
,new TimeSheetItem() { Name = "Vacation", Thursday2 = 8 } // Put in your 8 hours
,new TimeSheetItem() { Name = "Holiday" }
,new TimeSheetItem() { Name = "Meeting" }
};
}
在代码隐藏中将数据绑定到GridView:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TimeSheetGrid.DataSource = GetData();
TimeSheetGrid.DataBind();
}
}
这应该会给你这样的东西,你可以使用CSS来设置样式。
为了允许用户编辑此网格中的数据,您有几个选项。一种方法是使用支持默认编辑的数据源。您可以在here上阅读更多内容。
您也可以手动添加编辑。为此,您需要对GridView进行一些更改:
您的GridView现在看起来像这样:
<asp:GridView ID="TimeSheetGrid"
runat="server"
AutoGenerateColumns="False"
onrowediting="TimeSheetGrid_RowEditing"
onrowupdating="TimeSheetGrid_RowUpdating"
DataKeyNames="Name" onrowcancelingedit="TimeSheetGrid_RowCancelingEdit">
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="true" />
<asp:BoundField DataField="Sunday1" HeaderText="Sunday" />
<asp:BoundField DataField="Monday1" HeaderText="Monday" />
<asp:BoundField DataField="Tuesday1" HeaderText="Tuesday" />
<asp:BoundField DataField="Wednesday1" HeaderText="Wednesday" />
<asp:BoundField DataField="Thursday1" HeaderText="Thursday" />
<asp:BoundField DataField="Friday1" HeaderText="Friday" />
<asp:BoundField DataField="Saturday1" HeaderText="Saturday" />
<asp:BoundField DataField="Sunday2" HeaderText="Sunday" />
<asp:BoundField DataField="Monday2" HeaderText="Monday" />
<asp:BoundField DataField="Tuesday2" HeaderText="Tuesday" />
<asp:BoundField DataField="Wednesday2" HeaderText="Wednesday" />
<asp:BoundField DataField="Thursday2" HeaderText="Thursday" />
<asp:BoundField DataField="Friday2" HeaderText="Friday" />
<asp:BoundField DataField="Saturday2" HeaderText="Saturday" />
</Columns>
</asp:GridView>
在您的代码隐藏中,您的代码必须看起来像这样。请注意,我使用Session对象来保存数据。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["TimeSheetItems"] = GetData();
BindData();
}
}
protected void TimeSheetGrid_RowCancelingEdit(
object sender
,GridViewCancelEditEventArgs e)
{
// Exit edit mode - the user clicked cancel
TimeSheetGrid.EditIndex = -1;
BindData();
}
protected void TimeSheetGrid_RowEditing(object sender, GridViewEditEventArgs e)
{
// Enter edit mode - the user clicked edit
TimeSheetGrid.EditIndex = e.NewEditIndex;
BindData();
}
protected void TimeSheetGrid_RowUpdating(
object sender
,GridViewUpdateEventArgs e)
{
var items = (IEnumerable<TimeSheetItem>)Session["TimeSheetItems"];
// Get the TimeSheetItem that was being edited
var item = items.Single(i => i.Name.Equals(e.Keys["Name"].ToString()));
// Set the TimeSheetItem values to the new values
item.Sunday1 = TryGetIntValue(e.NewValues["Sunday1"]);
item.Monday1 = TryGetIntValue(e.NewValues["Monday1"]);
item.Tuesday1 = TryGetIntValue(e.NewValues["Tuesday1"]);
item.Wednesday1 = TryGetIntValue(e.NewValues["Wednesday1"]);
item.Thursday1 = TryGetIntValue(e.NewValues["Thursday1"]);
item.Friday1 = TryGetIntValue(e.NewValues["Friday1"]);
item.Saturday1 = TryGetIntValue(e.NewValues["Saturday1"]);
item.Sunday2 = TryGetIntValue(e.NewValues["Sunday2"]);
item.Monday2 = TryGetIntValue(e.NewValues["Monday2"]);
item.Tuesday2 = TryGetIntValue(e.NewValues["Tuesday2"]);
item.Wednesday2 = TryGetIntValue(e.NewValues["Wednesday2"]);
item.Thursday2 = TryGetIntValue(e.NewValues["Thursday2"]);
item.Friday2 = TryGetIntValue(e.NewValues["Friday2"]);
item.Saturday2 = TryGetIntValue(e.NewValues["Saturday2"]);
// Exit from edit mode
TimeSheetGrid.EditIndex = -1;
// Re-bind the data
BindData();
}
private static int TryGetIntValue(object val)
{
int intVal;
Int32.TryParse(val.ToString(), out intVal);
return intVal;
}
private void BindData()
{
TimeSheetGrid.DataSource = Session["TimeSheetItems"];
TimeSheetGrid.DataBind();
}
private IEnumerable<TimeSheetItem> GetData()
{
// You can pre-populate the hours here
return new List<TimeSheetItem>()
{
new TimeSheetItem() { Name = "IN1" }
,new TimeSheetItem() { Name = "IN2" }
,new TimeSheetItem() { Name = "IN3" }
,new TimeSheetItem() { Name = "IN4" }
,new TimeSheetItem() { Name = "Personal" }
,new TimeSheetItem() { Name = "Doctor" }
,new TimeSheetItem() { Name = "Other" }
,new TimeSheetItem() { Name = "Sick" }
,new TimeSheetItem() { Name = "Vacation", Thursday2 = 8 }
,new TimeSheetItem() { Name = "Holiday" }
,new TimeSheetItem() { Name = "Meeting" }
};
}
您可以通过设置TimeSheetItems中的值或用于检索数据的任何方法来预先填充GetData方法中的小时数。
这只是您可以实现此目标的一种简单示例。您应该阅读有关ASP.NET,C#和GridView控件的基础知识的更多信息。