62,168
社区成员
发帖
与我相关
我的任务
分享
protected void Page_Load(object sender, EventArgs e)
{
//if (!IsPostBack)
//{
GridView1.Columns.Clear();
tf4 = new TemplateField();
tf4.ItemTemplate = new GridViewTemplate("Label", "计算机");
tf4.EditItemTemplate = new GridViewTemplate("TextBox", "计算机");
tf4.HeaderText = "计算机";
GridView1.Columns.Add(tf4);
tf4 = new TemplateField();
tf4.ItemTemplate = new GridViewTemplate("Label", "数学");
tf4.EditItemTemplate = new GridViewTemplate("TextBox", "数学");
tf4.HeaderText = "数学";
GridView1.Columns.Add(tf4);
cf = new CommandField();
cf.ShowDeleteButton = false;
cf.ShowCancelButton = false;
cf.ShowEditButton = true;
GridView1.Columns.Add(cf);
databind();
Label l = (Label)GridView1.Rows[3].FindControl("label计算机");
if (l != null) TextBox1.Text = "GridView1.Rows[3] label.text = " + l.Text;
Label l2 = (Label)GridView1.Rows[3].FindControl("labeltext");
if (l2 != null) TextBox2.Text = "GridView1.Rows[3] l2.text" + l2.Text;
//}
}
public class GridViewTemplate : ITemplate
{
private string temptype;
private string columnName;
public GridViewTemplate(string type, string colname)
{
temptype = type;
columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (temptype.ToLower())
{
case "textbox":
TextBox textbox = new TextBox();
textbox.ID = "textbox" +columnName;
textbox.Text = "test";
//container.Controls.Add(textbox);
textbox.DataBinding += new EventHandler(this.OnDataBinding);
textbox.TextChanged += new EventHandler(this.OnTextChanged);
container.Controls.Add(textbox);
break;
case "label":
Label label = new Label();
label.ID = "label" +columnName;
//container.Controls.Add(label);
label.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(label);
break;
default:
break;
}
}
private void OnTextChanged(object sender, EventArgs e)
{
((TextBox)sender).Text += ":";
}
public void OnDataBinding(object sender, EventArgs e)
{
GridViewRow container;
switch(temptype.ToLower())
{
case "label":
Label l1 = (Label)sender;
container = (GridViewRow)l1.NamingContainer;
//l1.Text = ((DataRowView)container.DataItem)[columnName].ToString();
l1.Text = DataBinder.Eval(container.DataItem, columnName).ToString();
break;
case "textbox":
TextBox l = (TextBox)sender;
container = (GridViewRow)l.NamingContainer;
//l.Text = ((DataRowView)container.DataItem)[columnName].ToString();
l.Text = DataBinder.Eval(container.DataItem, columnName).ToString();
break;
default:
break;
}
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Label l = (Label)GridView1.Rows[e.NewEditIndex].FindControl("label");
TextBox lt = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("textbox计算机");
if (l == null) TextBox1.Text = "l == null is true;";
if (l != null) TextBox1.Text = "l = " + l.Text;
Label l3 = (Label)GridView1.Rows[e.NewEditIndex].FindControl("label数学");
if (l3 != null) TextBox1.Text += "l3 = " + l3.Text;
GridView1.EditIndex = e.NewEditIndex;
databind();
}
ICollection CreateDataSource()
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal)));
for (int i = 0; i < 8; i++)
{
System.Random rd = new System.Random(Environment.TickCount * i); ;
dr = dt.NewRow();
dr[0] = "班级" + i.ToString();
dr[1] = "学生" + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
dt.Rows.Add(dr);
}
System.Data.DataView dv = new System.Data.DataView(dt);
return dv;
}