Forum: ASP.NET2.0 |
Thema:
AW: AW: AW: AW: AW: Belegt Status anzeigen ! |
Von:
Thomas Hauser (
15.08.2007 23:46) |
Irgendwo musst Du was beim Kopieren des Codes vergessen haben. Hier nochmals der ganze Code. Sorry, aber bei mir funscht das!
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableViewState="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Frei-/Belegtinformationen</title>
<style type="text/css">
div {
float: none;
}
div.filterField {
float: left;
}
.Grid {
width: 100%;
}
.RessourceFree {
background-color: Green;
}
.RessourceNotFree {
background-color: Red;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="filterField">
<asp:Label ID="BeginLabel" runat="server" Text="Startdatum" AssociatedControlID="Calendar1"/>
<asp:calendar ID="Calendar1" runat="server"></asp:calendar>
</div>
<div class="filterField">
<asp:Label ID="EndLabel" runat="server" Text="Enddatum" AssociatedControlID="Calendar2"/>
<asp:calendar ID="Calendar2" runat="server"></asp:calendar>
</div>
</div>
<div>
<asp:GridView ID="GridView1" runat="server" CssClass="Grid" DataSourceID="ObjectDataSource1"></asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetDataTable"
TypeName="Ressourcen">
<SelectParameters>
<asp:ControlParameter ControlID="Calendar1" DefaultValue="DateTime.Now" Name="Begin"
PropertyName="SelectedDate" Type="DateTime" />
<asp:ControlParameter ControlID="Calendar2" DefaultValue="DateTime.Now" Name="End"
PropertyName="SelectedDate" Type="DateTime" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>
CODE BEHIND:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Aktuelle Woche:
if (!Page.IsPostBack)
{
int day = (int)DateTime.Now.DayOfWeek;
Calendar1.SelectedDate = DateTime.Now.AddDays(-1 * (day - 1));
Calendar2.SelectedDate = DateTime.Now.AddDays(7 - day);
}
GridView1.DataBound += new EventHandler(GridView1_DataBound);
}
void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow gridrow in GridView1.Rows)
{
foreach (TableCell cell in gridrow.Cells)
{
if (cell.Text == "belegt")
cell.CssClass = "RessourceNotFree";
else
{
if (cell.Text == " ")
{
cell.CssClass = "RessourceFree";
cell.Text = "frei";
}
}
}
}
}
}
KLASSE RESSOURCEN:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public class Ressourcen
{
private DateTime begin;
public DateTime Begin
{
get { return begin; }
set { begin = value; }
}
private DateTime end;
public DateTime End
{
get { return end; }
set { end = value; }
}
public Ressourcen()
{
int day = (int)DateTime.Now.DayOfWeek;
this.Begin = DateTime.Now.AddDays(-1 * (day - 1));
this.End = DateTime.Now.AddDays(7 - day);
}
public Ressourcen(DateTime Begin, DateTime End)
{
this.Begin = Begin;
this.End = End;
}
public DataTable GetDataTable(DateTime Begin, DateTime End)
{
this.Begin = Begin;
this.End = End;
return GetDataTable();
}
public DataTable GetDataTable()
{
DataTable belegungen = new DataTable("Belegungen");
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["myConString"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
//Möglichkeit 1:
cmd.CommandText = @"select
RessourcenId, Startdatum, Stopdatum
from
belegung
where
Startdatum between @Start and @End
or Stopdatum between @Start and @End
union
select
distinct RessourcenId, null, null
from
belegung
where
not(Startdatum between @Start and @End
or Stopdatum between @Start and @End)";
cmd.Parameters.AddWithValue("@Start", Begin);
cmd.Parameters.AddWithValue("@End", End);
//Möglichkeit 2: cmd.CommandText = "select * from Belegung";
//cmd.CommandText = "select * from Belegung where Startdatum between @Start and @End or Stopdatum between @Start and @End";
//cmd.Parameters.AddWithValue("@Start", Begin);
//cmd.Parameters.AddWithValue("@End", End);
DataTable data = new DataTable("Daten");
data.Load(cmd.ExecuteReader());
con.Close();
belegungen.Columns.Add("Raum", typeof(string));
TimeSpan span = (TimeSpan)(End - Begin);
for (int day = 0; day <= span.TotalDays; day++)
{
string colname = Begin.AddDays(day).ToShortDateString();
belegungen.Columns.Add(colname, typeof(string));
}
foreach (DataRow row in data.Rows)
{
//Hier habe ich was geändert, damit Spalten mit NULL-Werten berücksichtigt werden können (für Möglichkeit 1)
DateTime start = DateTime.MinValue;
DateTime end = DateTime.MinValue;
if (row["Startdatum"] != DBNull.Value)
start = (DateTime)row["Startdatum"];
if (row["Stopdatum"] != DBNull.Value)
end = (DateTime)row["Stopdatum"];
TimeSpan spanRow = (TimeSpan)(end - start);
DataRow resRow;
resRow = SearchRessource(row["RessourcenId"], belegungen);
if (resRow == null)
{
resRow = belegungen.NewRow();
belegungen.Rows.Add(resRow);
}
resRow["Raum"] = string.Format("Raum {0}", row["RessourcenId"].ToString());
for (int day = 0; day <= spanRow.TotalDays; day++)
{
string colname = start.AddDays(day).ToShortDateString();
if (belegungen.Columns.Contains(colname))
resRow[colname] = "belegt";
}
}
}
return belegungen;
}
private DataRow SearchRessource(object Ressource, DataTable belegungen)
{
foreach (DataRow row in belegungen.Rows)
{
if (row["Raum"].ToString() == string.Format("Raum {0}", Ressource.ToString()))
{
return row;
}
}
return null;
}
}
Thomas Hauser (Trainer und Consultant, Blog: http://thomashauser.spaces.live.com, Forum: http://www.sharepointbook.de - Die Forum-Webseite zum Buch!)
Antworten
Vorsicht bei der Eingabe: Die Zeichen ' oder -- sind nicht erlaubt!