-1

我有填写我DataColumn的 s 的代码。但是我创建的 for 循环不起作用。第一个DataTable是用 SQL 查询填充的。第二个DataTable需要部分填充来自第一个的数据DataTable。所以我DataTable用for循环手动构建了第二个来填充它,但它不起作用?

using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ComponentModel;

public partial class _Planning : System.Web.UI.Page
{
ConnectionStringSettings Leerstoftabel;
SqlConnection connection;
int cursusID = 2;
DataTable LeerstofTabel;
DataTable Planning;


protected void Page_Load(object sender, EventArgs e)
{
Leerstoftabel = ConfigurationManager.ConnectionStrings["CursusPlanner"];
connection = new SqlConnection(Leerstoftabel.ConnectionString);

SqlCommand cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT cursus.naam AS [Cursus Naam], vak.vaknaam, cursusvak.volgnummer, les.dagnummer, les.hoofdstuknummer, les.naam AS [Les Naam], les.lesomschrijving FROM cursus INNER JOIN cursusvak ON cursus.cursusID = cursusvak.cursusID INNER JOIN vak ON cursusvak.vakID = vak.vakID INNER JOIN les ON vak.vakID = les.vakID WHERE(cursus.cursusID = @cursusID)ORDER BY cursusvak.volgnummer";

//int cursusID = Convert.ToInt32(DropDownList1.SelectedValue);
cmd.Parameters.Add("@cursusID", SqlDbType.Int, 0).Value = cursusID;

connection.Open();

//het creeëren van een datatabel
DbDataReader rdr = cmd.ExecuteReader();
LeerstofTabel = new DataTable();
LeerstofTabel.Load(rdr);

connection.Close();
}


protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
// weekend days can't be selected in calendar
if (e.Day.IsWeekend)
    e.Day.IsSelectable = false;
}

protected void Button1_Click(object sender, EventArgs e)
{
//create the DataTable named "Planning"
Planning = new DataTable ("Planning");

//Add the DataColumn using defaults
DataColumn datum = new DataColumn("Datum");
datum.DataType = typeof(DateTime);
datum.AllowDBNull = false;
datum.Caption = "Datum";
Planning.Columns.Add(datum);

//Add the DataColumn using defaults
DataColumn hoofdstuk = new DataColumn("Hoofdstuk");
hoofdstuk.AllowDBNull = false;
hoofdstuk.Caption = "Hoofdstuk";
Planning.Columns.Add(hoofdstuk);

//Add the DataColumn using defaults
DataColumn omschrijving = new DataColumn("Omschrijving");
omschrijving.AllowDBNull = false;
hoofdstuk.Caption = "Omschrijving";
Planning.Columns.Add(omschrijving);

//Add the DataColumn using defaults
DataColumn lessen = new DataColumn("Lessen");
lessen.AllowDBNull = false;
lessen.Caption = "Lessen";
Planning.Columns.Add(lessen);


for (int i = 0; i < LeerstofTabel.Rows.Count; i++)
{
    if (LeerstofTabel ["vaknaam"]).ToString() != vorigvak  // this statement doesn't work
    {
        DataRow newplannning = Planning.NewRow();
        newplannning["Datum"] = "";
        newplannning["Hoofdstuk"] = "*" + LeerstofTabel["vaknaam"];
        newplannning["Omschrijving"] = "";
        newplannning["Lessen"] = "";
        Planning.Rows.Add(newplannning);

    }

    Vorigvak = (LeerstofTabel["vaknaam"])ToString();
}


GridView1.DataSource = Planning;
GridView1.DataBind();
4

2 回答 2

0

首先抱歉,如果这是错误的。由于语言的原因,很难理解您的代码。

 if (LeerstofTabel ["vaknaam"]).ToString() != vorigvak 

应该

 if (LeerstofTabel.Rows[i]["vaknaam"]).ToString() != vorigvak 
于 2012-10-29T14:48:55.470 回答
0

如果 vaknaam 是您的列名,则需要连续使用它

var r = LeerstofTabel.Rows[i]
if (r["vaknaam"]).ToString() != vorigvak) { // ...
于 2012-10-29T14:47:59.753 回答