首页 新闻 搜索 专区 学院

关于gridview动态添加列的问题

0
悬赏园豆:20 [待解决问题]

运行后,每次增加的列名都是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]

一缕秋丝的主页 一缕秋丝 | 初学一级 | 园豆:3
提问于:2011-10-31 16:42
< >
分享
所有回答(1)
0

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;

丁学 | 园豆:18530 (专家六级) | 2011-10-31 20:57

恩,是的,已经解决了。现在的问题是怎样能将其绑定数据

支持(0) 反对(0) 一缕秋丝 | 园豆:3 (初学一级) | 2011-10-31 21:14

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网上看到的 怎么调用

支持(0) 反对(0) 一缕秋丝 | 园豆:3 (初学一级) | 2011-10-31 21:17

@一缕秋丝: 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");

   }
}

支持(0) 反对(0) 一缕秋丝 | 园豆:3 (初学一级) | 2011-10-31 21:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册