运行后,每次增加的列名都是10-31,而不是10-1,……10-31
页面代码
[code=C#]
protected void Page_Load(object sender, EventArgs e)
{
int ddays = Convert.ToInt32(DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month))//获取本月的天数;
if (!IsPostBack)
{
TemplateField customField = new TemplateField();
customField.ShowHeader = true;
for (int i = 1; i <= ddays; i++)
{
string datetime = DateTime.Now.Month + "-" + i;
customField.HeaderTemplate = new MyTemplate(datetime, DataControlRowType.Header);
customField.ItemTemplate = new MyTemplate("", DataControlRowType.DataRow);
GridView1.Columns.Add(customField);
}
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
}
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("id", typeof(Int32)));
dt.Columns.Add(new DataColumn("text", typeof(string)));
for (int i = 0; i < 6; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "列表项目 " + i.ToString();
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
[/code]
接口:
[code=C#]
public class MyTemplate:ITemplate
{
#region ITemplate 成员
private string strColumnName; //列名字
private DataControlRowType dcrtColumnType; //数据行功能
public MyTemplate(string strColumnName,DataControlRowType dcrtColumnType)
{
this.strColumnName = strColumnName;
this.dcrtColumnType = dcrtColumnType;
}
public void InstantiateIn(Control container)
{
switch(dcrtColumnType)
{
case DataControlRowType.Header:
Literal ltr = new Literal();
ltr.Text = strColumnName;
container.Controls.Add(ltr);
break;
case DataControlRowType.DataRow:
Label labe = new Label();
labe.ID = "lab1";
labe.Text = "";
container.Controls.Add(labe);
//DropDownList drr = new DropDownList();
//drr.ID = "dropdown";
//drr.AppendDataBoundItems = true;
//drr.Items.Add(new ListItem("-----请选择------", ""));
//drr.Items.Add(new ListItem("AA", "a"));
//drr.Items.Add(new ListItem("BB", "b"));
//drr.Items.Add(new ListItem("CC", "c"));
//container.Controls.Add(drr);
break;
}
}
#endregion
}
[/code]
for (int i = 1; i <= ddays; i++)
{
string datetime = DateTime.Now.Month + "-" + i;
customField.HeaderTemplate = new MyTemplate(datetime, DataControlRowType.Header);
customField.ItemTemplate = new MyTemplate("", DataControlRowType.DataRow);
GridView1.Columns.Add(customField);
}
customFiled只声明了一个,后面的Add不过是把这一个重复添加到GridView里,这个循环,每一次进去都会重新设置customField.HeaderTemplate,直到最后一个数字31,所以就全是10-031了,把前面customField的声明放到循环里应该会好,就是下面这两个:
TemplateField customField = new TemplateField();
customField.ShowHeader = true;
恩,是的,已经解决了。现在的问题是怎样能将其绑定数据
public class GridViewTemplateTextBox : ITemplate
{
private DataControlRowType templateType;
private string columnName;
private string dataField;
public GridViewTemplateTextBox(DataControlRowType type, string colname, string datafield)
{
templateType = type;
columnName = colname;
dataField = datafield;
}
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:
TextBox tb = new TextBox();
tb.ID = container.ClientID;
tb.DataBinding += new EventHandler(tb_DataBinding);
container.Controls.Add(tb);
break;
default:
break;
}
}
private void tb_DataBinding(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
tb.Width = Unit.Percentage(100);
GridViewRow container = (GridViewRow)tb.NamingContainer;
tb.Text = ((DataRowView)container.DataItem)[dataField].ToString();
tb.Width = Unit.Pixel(70);
tb.Style.Add("TEXT-ALIGN", "right");
}
}x网上看到的 怎么调用
@一缕秋丝: public class GridViewTemplateTextBox : ITemplate
{
private DataControlRowType templateType;
private string columnName;
private string dataField;
public GridViewTemplateTextBox(DataControlRowType type, string colname, string datafield)
{
templateType = type;
columnName = colname;
dataField = datafield;
}
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:
TextBox tb = new TextBox();
tb.ID = container.ClientID;
tb.DataBinding += new EventHandler(tb_DataBinding);
container.Controls.Add(tb);
break;
default:
break;
}
}
private void tb_DataBinding(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
tb.Width = Unit.Percentage(100);
GridViewRow container = (GridViewRow)tb.NamingContainer;
tb.Text = ((DataRowView)container.DataItem)[dataField].ToString();
tb.Width = Unit.Pixel(70);
tb.Style.Add("TEXT-ALIGN", "right");
}
}