表格打分题:
protected void Page_Load(object sender, EventArgs e)
{
//行值数组
string[] RowData ={ "选项描述1", "选项描述2", "选项描述3" };
//选项数组
string[] ColumnData ={ "选项1", "选项2", "选项3" };
//使用例子
ShowTablePQ(RowData, ColumnData);
}
//显示表格打分题
public void ShowTablePQ(string[] RowData, string[] ColumnData)
{
//动态添加GridView控件
Panel1.Controls.Clear();
//添加选项描述列
GridView GridView1 = new GridView();
BoundField bf = new BoundField();
//bf.HeaderText="SD";
bf.DataField = "SD";
GridView1.AutoGenerateColumns = false;
GridView1.Columns.Add(bf);
//添加模板列
for (int l = 0; l < ColumnData.Length; l++)
{
TemplateField customField1 = new TemplateField();
customField1.ShowHeader = true;
customField1.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, ColumnData[l].ToString());
customField1.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "");
GridView1.Columns.Add(customField1);
}
//绑定数据源
GridView1.DataSource = CreateDataSource(RowData);
GridView1.DataBind();
Panel1.Controls.Add(GridView1);
}
//创建表格视图
public DataView CreateDataSource(string[] RowData)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("SD");
for (int i = 0; i < RowData.Length; i++)
{
dr = dt.NewRow();
dr[0] = RowData[i].ToString();
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
public GridViewTemplate(DataControlRowType type, string colname)
{
templateType = type;
columnName = colname;
}
//模板列
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
DropDownList drr = new DropDownList();
//drr.ID = "dropdown";
drr.AppendDataBoundItems = true;
drr.Items.Add(new ListItem("--请打分--", "0"));
drr.Items.Add(new ListItem("1", "1"));
drr.Items.Add(new ListItem("2", "2"));
drr.Items.Add(new ListItem("3", "3"));
drr.Items.Add(new ListItem("4", "4"));
drr.Items.Add(new ListItem("5", "5"));
drr.Items.Add(new ListItem("6", "6"));
drr.Items.Add(new ListItem("7", "7"));
drr.Items.Add(new ListItem("8", "8"));
drr.Items.Add(new ListItem("9", "9"));
drr.Items.Add(new ListItem("10", "10"));
container.Controls.Add(drr);
break;
default:
break;
}
}
}
--------------------------------
表格单选题:
protected void Page_Load(object sender, EventArgs e)
{
//行值数组
string[] RowData ={ "选项描述1", "选项描述2", "选项描述3" };
//选项数组
string[] Selection ={ "有", "没有", "不知道" };
//使用例子
ShowTableSQ(RowData, Selection);
}
//显示表格多选题
public void ShowTableSQ(string[] RowData, string[] Selection)
{
//动态添加GridView控件
Panel1.Controls.Clear();
GridView GridView1 = new GridView();
//添加选项描述列
BoundField bf = new BoundField();
//bf.HeaderText="SD";
bf.DataField = "SD";
GridView1.AutoGenerateColumns = false;
GridView1.Columns.Add(bf);
//添加模板列
for (int l =0;l<Selection.Length; l++)
{
TemplateField customField1 = new TemplateField();
customField1.ShowHeader = true;
customField1.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, Selection[l].ToString());
customField1.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "");
GridView1.Columns.Add(customField1);
}
//绑定数据源
GridView1.DataSource = CreateDataSource(RowData);
GridView1.DataBind();
Panel1.Controls.Add(GridView1);
}
//创建表格视图
public DataView CreateDataSource(string[] RowData)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("SD");
for (int i = 0; i < RowData.Length; i++)
{
dr = dt.NewRow();
dr[0] = RowData[i].ToString();
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
public GridViewTemplate(DataControlRowType type, string colname)
{
templateType = type;
columnName = colname;
}
//模板列
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
RadioButton rb = new RadioButton();
rb.ID = "rb";
rb.Checked = false;
container.Controls.Add(rb);
break;
default:
break;
}
}
}
--------------------------
表格多选题:
protected void Page_Load(object sender, EventArgs e)
{
//行值数组
string[] RowData ={ "选项描述1", "选项描述2", "选项描述3" };
//选项数组
string[] Selection ={ "选项1", "选项2", "选项3" };
//使用例子
ShowTableMQ(RowData, Selection);
}
//显示表格多选题
public void ShowTableMQ(string[] RowData, string[] Selection)
{
//动态添加GridView控件
Panel1.Controls.Clear();
GridView GridView1 = new GridView();
//添加选项描述列
BoundField bf = new BoundField();
//bf.HeaderText="SD";
bf.DataField = "SD";
GridView1.AutoGenerateColumns = false;
GridView1.Columns.Add(bf);
//添加模板列
for (int l = 0; l < Selection.Length; l++)
{
TemplateField customField1 = new TemplateField();
customField1.ShowHeader = true;
customField1.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, Selection[l].ToString());
customField1.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "");
GridView1.Columns.Add(customField1);
}
//绑定数据源
GridView1.DataSource = CreateDataSource(RowData);
GridView1.DataBind();
Panel1.Controls.Add(GridView1);
}
//创建表格视图
public DataView CreateDataSource(string[] RowData)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("SD");
for (int i = 0; i < RowData.Length; i++)
{
dr = dt.NewRow();
dr[0] = RowData[i].ToString();
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
public GridViewTemplate(DataControlRowType type, string colname)
{
templateType = type;
columnName = colname;
}
//模板列
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
CheckBox cb = new CheckBox();
//cb.ID = "checkbox";
cb.Checked = false;
container.Controls.Add(cb);
break;
default:
break;
}
}
}