Do more with
Dim dt As New DataTable("GradeBook")
Dim dtRow As DataRow
Dim dtColumn As DataColumn
'Build Datatable columns
dtColumn = New DataColumn("StudentID")
dtColumn.DataType = System.Type.GetType("System.Int32")
dt.Columns.Add(dtColumn)
dtColumn = New DataColumn("StudentName")
dtColumn.DataType = System.Type.GetType("System.String")
dt.Columns.Add(dtColumn)
'SELECT * FROM @COURSES WHERE classid = @CourseID
For Each itm As Course In Courses
dtColumn = New DataColumn(CStr(Course.CourseID))
dtColumn.DataType = System.Type.GetType("System.Int32")
dt.Columns.Add(dtColumn)
dtColumn = New DataColumn(CStr(Course.CourseName))
dtColumn.DataType = System.Type.GetType("System.String")
dt.Columns.Add(dtColumn)
dtColumn = New DataColumn(CStr(Course.CourseGrade))
dtColumn.DataType = System.Type.GetType("System.Int32")
dt.Columns.Add(dtColumn)
Next
'SELECT * FROM STUDENTSGRADES sg INNER JOIN @COURSES c ON sg.courseid = c.courseid WHERE c.classid = @CourseID
For Each itm As Grade In Grades
dtRow = dt.NewRow()
dtRow.Item("StudentGradeID") = studentgradeid
dtRow.Item("StudentID") = studentid
dtRow.Item(CStr(courseid)) = grade
Next
Dim ds As New DataSet()
ds = New DataSet()
ds.Tables.Add(dt)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
/// <summary>
/// Summary description for GridViewTemplate
/// </summary>
public class GridViewTemplate : ITemplate
{
ListItemType itemType;
public string fieldName;
string infoType;
public GridViewTemplate(ListItemType type, string field, string info)
{
itemType = type;
fieldName = field;
infoType = info;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (itemType)
{
case ListItemType.Header:
Literal headerLiteral = new Literal();
headerLiteral.Text = "<B>" + fieldName + "</B>";
container.Controls.Add(headerLiteral);
break;
case ListItemType.Item:
switch (infoType)
{
case "Command":
LinkButton editButton = new LinkButton();
editButton.ID = "EditButton";
editButton.Text = "Edit";
editButton.CommandName = "Edit";
editButton.Click += new EventHandler(editButton_Click);
container.Controls.Add(editButton);
break;
default:
Label label = new Label();
label.ID = fieldName;
label.Text = "";
label.DataBinding += new EventHandler(OnDataBinding);
container.Controls.Add(label);
break;
}
break;
case ListItemType.EditItem:
if (infoType == "Command")
{
LinkButton updateButton = new LinkButton();
updateButton.ID = "UpdateButton";
updateButton.CommandName = "Update";
updateButton.Text = "Update";
//updateButton.OnClientClick = "return confirm('Are you sure?')";
container.Controls.Add(updateButton);
LinkButton cancelButton = new LinkButton();
cancelButton.ID = "CancelButton";
cancelButton.CommandName = "Cancel";
cancelButton.Text = "Cancel";
container.Controls.Add(cancelButton); ;
}
else
{
TextBox fieldTB = new TextBox();
fieldTB.ID = fieldName;
fieldTB.Text = "";
fieldTB.Width = 65;
fieldTB.DataBinding += new EventHandler(OnDataBinding);
container.Controls.Add(fieldTB);
}
break;
}
}
void OnDataBinding(object sender, EventArgs e)
{
object bound_value_obj = null;
Control ctrl = (Control)sender;
IDataItemContainer data_item_container = (IDataItemContainer)ctrl.NamingContainer;
bound_value_obj = DataBinder.Eval(data_item_container.DataItem, fieldName);
switch (itemType)
{
case ListItemType.Item:
Label fieldLiteral = (Label)sender;
fieldLiteral.Text = bound_value_obj.ToString();
break;
case ListItemType.EditItem:
TextBox fieldTB = (TextBox)sender;
fieldTB.Text = bound_value_obj.ToString();
break;
}
}
void editButton_Click(object sender, EventArgs e)
{
new Page().Session["InsertFlag"] = 0;
}
}
protected void BuildDataTable()
{
int classID = Convert.ToInt32(ClassDropDown.SelectedValue);
string term = TermDropDown.SelectedItem.Text;
StudentsBLL studentLogic = new StudentsBLL();
School.StudentsDataTable studentsTable = studentLogic.GetStudentsByClassID(classID);
CoursesBLL courseLogic = new CoursesBLL();
School.CoursesDataTable coursesTable = courseLogic.GetCoursesByClassIDAndTerm(classID, term);
currentCourses.Clear();
currentStudents.Clear();
GradesGridView.Columns.Clear();
StudentsGradesTable = new DataTable("StudentsGradesTable");
DataColumn column;
DataRow studentRow;
column = new DataColumn("éÝ Þéä×Ô");
column.DataType = System.Type.GetType("System.String");
StudentsGradesTable.Columns.Add(column);
column = new DataColumn("éÝ äèØÙ");
column.DataType = System.Type.GetType("System.String");
StudentsGradesTable.Columns.Add(column);
foreach (School.CoursesRow row in coursesTable)
{
if (!string.IsNullOrEmpty(row.SubjectName))
{
currentCourses.Add(row.CourseID);
column = new DataColumn(row.SubjectName);
column.DataType = System.Type.GetType("System.String");
StudentsGradesTable.Columns.Add(column);
}
}
column = new DataColumn("ÞÞÕæâ", System.Type.GetType("System.String"));
StudentsGradesTable.Columns.Add(column);
foreach (School.StudentsRow rowS in studentsTable)
{
currentStudents.Add(rowS.StudentID);
studentRow = StudentsGradesTable.NewRow();
studentRow["éÝ Þéä×Ô"] = rowS["Family"].ToString();
studentRow["éÝ äèØÙ"] = rowS.StudentName;
foreach (School.CoursesRow rowC in coursesTable)
{
int studentID = rowS.StudentID;
StudentGradesBLL studentGradesLogic = new StudentGradesBLL();
School.StudentsGradesDataTable gradesTable = studentGradesLogic.GetStudentGradesByStudentIDAndCourseID(studentID, rowC.CourseID);
if (gradesTable.Rows.Count > 0)
{
studentRow[rowC.SubjectName] = gradesTable.Rows[0]["Grade"].ToString();
}
}
string average = CalculateStudentAverage(studentRow);
studentRow["ÞÞÕæâ"] = average;
StudentsGradesTable.Rows.Add(studentRow);
}
}
protected void PopulateGridView()
{
BuildDataTable();
TemplateField butTemplateField = new TemplateField();
butTemplateField.ItemTemplate = new GridViewTemplate(ListItemType.Item, "", "Command");
butTemplateField.HeaderTemplate = new GridViewTemplate(ListItemType.Header, "", "Command");
butTemplateField.EditItemTemplate = new GridViewTemplate(ListItemType.EditItem, "", "Command");
GradesGridView.Columns.Add(butTemplateField);
for (int i = 0; i < StudentsGradesTable.Columns.Count; i++)
{
TemplateField itemTemplateField = new TemplateField();
if ((i < 2) || (i == (StudentsGradesTable.Columns.Count - 1))) // Family and Student Name
{
itemTemplateField.ItemTemplate = new GridViewTemplate(ListItemType.Item,
StudentsGradesTable.Columns[i].ColumnName,
StudentsGradesTable.Columns[i].DataType.Name);
}
else
{
if (DisplayRB.Checked)
{
itemTemplateField.ItemTemplate = new GridViewTemplate(ListItemType.Item,
StudentsGradesTable.Columns[i].ColumnName,
StudentsGradesTable.Columns[i].DataType.Name);
}
else
{
itemTemplateField.ItemTemplate = new GridViewTemplate(ListItemType.EditItem,
StudentsGradesTable.Columns[i].ColumnName,
StudentsGradesTable.Columns[i].DataType.Name);
}
}
itemTemplateField.HeaderTemplate = new GridViewTemplate(ListItemType.Header,
StudentsGradesTable.Columns[i].ColumnName,
StudentsGradesTable.Columns[i].DataType.Name);
if ((i < 2) || (i == (StudentsGradesTable.Columns.Count - 1)))
{
itemTemplateField.EditItemTemplate = new GridViewTemplate(ListItemType.Item,
StudentsGradesTable.Columns[i].ColumnName,
StudentsGradesTable.Columns[i].DataType.Name);
}
else
{
itemTemplateField.EditItemTemplate = new GridViewTemplate(ListItemType.EditItem,
StudentsGradesTable.Columns[i].ColumnName,
StudentsGradesTable.Columns[i].DataType.Name);
}
GradesGridView.Columns.Add(itemTemplateField);
}
GradesGridView.DataSource = StudentsGradesTable;
GradesGridView.DataBind();
}
Premium Content
You need an Expert Office subscription to comment.Start Free Trial