Hej! Det borde väl räcka med att du istället för att "manuellt" skapa tre rader i din BuildSocialEventTable-sub anropar din databas och lägger resultatet från anropet i socialEvents-tabellen (förutsatt att du i databasen har kolumner som heter Date, Description och Url). Litehjälp att göra denna till databasdriven kalender
Jag har hittat följande kodexempel
<code>
namespace calendar
{
/// <summary>
/// Summary description for MondayFriday.
/// </summary>
public class Socials : Page
{
protected System.Web.UI.WebControls.Calendar Calendar1;
private void Page_Load(object sender, System.EventArgs e)
{
BuildSocialEventTable();
}
private void BuildSocialEventTable()
{
// to simulate a database query
socialEvents = new DataTable();
socialEvents.Columns.Add(new DataColumn("Date", typeof(DateTime)));
socialEvents.Columns.Add(new DataColumn("Description", typeof(string)));
socialEvents.Columns.Add(new DataColumn("Url", typeof(string)));
DataRow row;
row = socialEvents.NewRow();
row["Date"] = DateTime.Now.AddDays(-5);
row["Description"] = "Poker game at Scott's";
row["Url"] = "http://www.OdeToCode.com/blogs/scott/";
socialEvents.Rows.Add(row);
row = socialEvents.NewRow();
row["Date"] = DateTime.Now.AddDays(3);
row["Description"] = "LAN party at Poonam's";
row["Url"] = "http://www.OdeToCode.com/blogs/poonam/";
socialEvents.Rows.Add(row);
row = socialEvents.NewRow();
row["Date"] = DateTime.Now.AddDays(7);
row["Description"] = "Dart throwing at Ode To Code";
row["Url"] = "http://www.OdeToCode.com";
socialEvents.Rows.Add(row);
row = socialEvents.NewRow();
row["Date"] = DateTime.Now.AddDays(7);
row["Description"] = "Softball tournament at Ode To Code";
row["Url"] = "http://www.OdeToCode.com";
socialEvents.Rows.Add(row);
}
private void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
DataRow[] rows = socialEvents.Select(
String.Format(
"Date >= #{0}# AND Date < #{1}#",
e.Day.Date.ToShortDateString(),
e.Day.Date.AddDays(1).ToShortDateString()
)
);
foreach(DataRow row in rows)
{
System.Web.UI.WebControls.Image image;
image = new System.Web.UI.WebControls.Image();
image.ImageUrl = "dot.gif";
image.ToolTip = row["Description"].ToString();
e.Cell.Controls.Add(image);
e.Cell.BackColor = Color.Firebrick;
}
}
private void Calendar1_SelectionChanged(object sender, System.EventArgs e)
{
System.Data.DataView view = socialEvents.DefaultView;
view.RowFilter = String.Format(
"Date >= #{0}# AND Date < #{1}#",
Calendar1.SelectedDate.ToShortDateString(),
Calendar1.SelectedDate.AddDays(1).ToShortDateString()
);
if(view.Count > 0)
{
DataGrid1.Visible = true;
DataGrid1.DataSource = view;
DataGrid1.DataBind();
}
else
{
DataGrid1.Visible = false;
}
}
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private DataTable socialEvents;
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Calendar1.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler(this.Calendar1_DayRender);
this.Calendar1.SelectionChanged += new System.EventHandler(this.Calendar1_SelectionChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
</code>
Hur bygger jag om denna så att den hämtar datan i från et dataset
/TyronneSv: Litehjälp att göra denna till databasdriven kalender