2 fm4399 fm4399 于 2017.11.15 14:06 提问

用gridview动态创建模板列,却只能显示一行找不到原因,求解 30C

我用网上给的方法,动态创建模板列,许多功能很实用,就是只能显示一行,其他都是空白
http://www.cnblogs.com/itshare/archive/2010/11/07/1871038.html
不知到怎么回事

 private void GeneralCols()
        {
            // 第1个模板列: Label 
            // HeaderTemplate Row
            System.Collections.Generic.Dictionary<string, ControlField> controlFieldList1 = new System.Collections.Generic.Dictionary<string, ControlField>();
            TemplateField col1 = new TemplateField();
            col1.ShowHeader = true;
            // CheckBox
            System.Web.UI.WebControls.CheckBox chk = new CheckBox();
            chk.ID = "chk";
            //
            System.Collections.Generic.Dictionary<string, string> fields0 = new System.Collections.Generic.Dictionary<string, string>();
            fields0.Add("Checked", "isYes");
            //
            ControlField controlField0 = new ControlField(ControlTypeEnum.CheckBox, chk, fields0);
            controlFieldList1.Add(chk.ID, controlField0);
            //
            MyTmeplateColumn col1_headerow = new MyTmeplateColumn(DataControlRowType.Header, "学生编号", controlFieldList1);
            col1.HeaderTemplate = col1_headerow;

            // ItemTemplate Row
            System.Collections.Generic.Dictionary<string, ControlField> controlFieldList2 = new System.Collections.Generic.Dictionary<string, ControlField>();
            //
            System.Web.UI.WebControls.Label lblID = new Label();
            lblID.ID = "lblID";
            //
            System.Collections.Generic.Dictionary<string, string> fields2 = new System.Collections.Generic.Dictionary<string, string>();
            fields2.Add("Text", "ID");
            //
            ControlField controlField1 = new ControlField(ControlTypeEnum.Label, lblID, fields2);
            controlFieldList2.Add(lblID.ID, controlField1);
            //
            // TextBox 
            System.Web.UI.WebControls.TextBox txtName = new TextBox();
            txtName.ID = "txtName";
            //
            System.Collections.Generic.Dictionary<string, string> fields3 = new System.Collections.Generic.Dictionary<string, string>();
            fields3.Add("Text", "Name");
            //
            ControlField controlField2 = new ControlField(ControlTypeEnum.TextBox, txtName, fields3);
            controlFieldList2.Add(txtName.ID, controlField2);
            //
            MyTmeplateColumn col1_itemrow = new MyTmeplateColumn(DataControlRowType.DataRow, "学生编号", controlFieldList2);
            col1.ItemTemplate = col1_itemrow;

            gvShow.Columns.Add(col1);
            //Bind();

        }

 private void Bind()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(string));
            dt.Columns.Add("name", typeof(string));
            dt.Columns.Add("isyes", typeof(bool));
            dt.Columns.Add("url", typeof(string));
            DataRow dr = dt.NewRow();
            dr[0] = "No123456";
            dr[1] = "王晓巍";
            dr[2] = true;
            dr[3] = "http://www.baidu.com";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "No123456";
            dr[1] = "王晓巍";
            dr[2] = true;
            dr[3] = "http://www.baidu.com";
            dt.Rows.Add(dr);
            //
            gvShow.AutoGenerateColumns = false;
            gvShow.HeaderStyle.BackColor = System.Drawing.Color.BlueViolet;
            gvShow.HeaderStyle.ForeColor = System.Drawing.Color.FromName("#FFFFFF");
            gvShow.HeaderStyle.Font.Name = "华文行楷";
            gvShow.HeaderStyle.Font.Bold = true;
            gvShow.RowStyle.BackColor = System.Drawing.Color.ForestGreen;
            gvShow.GridLines = GridLines.Both;
            gvShow.CellPadding = 3;
            gvShow.CellSpacing = 0;
            gvShow.DataSource = dt;
            gvShow.DataBind();
        }

显示的行,其中上面一行空白
断点显示datatable有两个

1个回答

CHENHAO19900918
CHENHAO19900918   2017.11.15 15:53

你可以后台打断点看一下你DataTable中的具体内容,看看到底是前端显示还是后台的问题,这样查找可能快点儿

fm4399
fm4399 以上就是断点看到的datatable数据,应该是显示问题,但就是找不到哪一块
7 天之前 回复
Csdn user default icon
上传中...
上传图片
插入图片