多谢各位,我是用DATASOURCE绑定数据表,但是当在程序中添加表的字段时,datagridview显示不出增加之后的,于是我按网上有人提供的方法添加如下代码:
 public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                if ((value != null) && !(value is DataGridViewTextBoxCell))
                {
                    throw new InvalidCastException(SR.GetString("DataGridViewTypeColumn_WrongCellTemplateType", new object[] { "System.Windows.Forms.DataGridViewTextBoxCell" }));
                }
                base.CellTemplate = value;
            }
        }结果出现错误"Form2.CellTemplate: 没有找到适合的方法来重写.",请问该怎么处理?

解决方案 »

  1.   

    是不是WINFORM的和WEBFORM不一样?
    我在WEB中直接加COLUMN就行了:
    导入时: 
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    string sql = "select a,b,c from csdn3";
                    SqlCommand command = new SqlCommand(sql, conn);
                    SqlDataAdapter da = new SqlDataAdapter(command);
                    ds = new DataSet();
                    ViewState["authors"] = ds;
                    conn.Open(); ;
                    da.Fill(ds);                GridView2.DataSource = ds.Tables[0];
                    GridView2.DataBind();
                }加列
               using (SqlConnection conn = new SqlConnection(connString))
                {
                    string sql = "select * from csdn3";
                    SqlCommand command = new SqlCommand(sql, conn);
                    SqlDataAdapter da = new SqlDataAdapter(command);
                    ds = new DataSet();
                    conn.Open(); ;
                    da.Fill(ds);                BoundField bf = new BoundField();
                    bf.DataField = "d";
                    bf.HeaderText = "d";
                    GridView2.Columns.Add(bf);
                    GridView2.DataSource = ds.Tables[0];
                    GridView2.DataBind();
                }
      

  2.   

    不知道你说的是不是DataGrid,如果是的话,你要显示数据源的变化,必须从新绑定DataGrid.DataBind()
    或者你用ajax实现……
      

  3.   

    ------------------初始------------------------            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.a,
                this.b,
                this.c});
                this.dataGridView1.Location = new System.Drawing.Point(0, 0);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.Size = new System.Drawing.Size(462, 155);
                this.dataGridView1.TabIndex = 2;
                this.dataGridView1.AutoGenerateColumns = false;
                // 
                // a
                // 
                this.a.DataPropertyName = "a";
                this.a.HeaderText = "a";
                this.a.Name = "a";
                // 
                // b
                // 
                this.b.DataPropertyName = "b";
                this.b.HeaderText = "b";
                this.b.Name = "b";
                // 
                // c
                // 
                this.c.DataPropertyName = "c";
                this.c.HeaderText = "c";
                this.c.Name = "c";             using (SqlConnection conn = new SqlConnection(connString))
                {
                    string sql = "select a,b,c from csdn3";
                    SqlCommand command = new SqlCommand(sql, conn);
                    SqlDataAdapter da = new SqlDataAdapter(command);
                    ds = new DataSet();
                    conn.Open(); ;
                    da.Fill(ds);                dataGridView1.DataSource = ds.Tables[0];
                }-------------------------加列------------------
               DataSet ds = new DataSet();
                string connString = ConfigurationSettings.AppSettings["CSDN"].ToString();
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    string sql = "select a,b,c,d from csdn3";
                    SqlCommand command = new SqlCommand(sql, conn);
                    SqlDataAdapter da = new SqlDataAdapter(command);
                    ds = new DataSet();
                    conn.Open(); ;
                    da.Fill(ds);                DataGridViewTextBoxColumn textboxColumn = new DataGridViewTextBoxColumn();
                    textboxColumn.HeaderText = "new";
                    textboxColumn.Name = "d";
                    textboxColumn.DataPropertyName = "d";
                    dataGridView1.Columns.Add(textboxColumn);                dataGridView1.DataSource = ds.Tables[0];
                }
      

  4.   

    多谢各位,我用的dataGridView1中没Columns属性.我重新指定数据源也不行.
      

  5.   

    vb.net的:
    不是单纯这么加的,可以这样写
    dim Name(4) as string
    Name(0)="a"
    Name(1)="b"
    Name(2)="c"
    Name(3)="d"
    dataGridView1.Columns.Clear()
    dataGridView1.AutoGenerateColumns = false
    string sql = "select a,b,c,d from csdn3";
    SqlCommand command = new SqlCommand(sql, conn)
    SqlDataAdapter da = new SqlDataAdapter(command)
    ds = new DataSet()
    conn.Open()
    da.Fill(ds)
    dataGridView1.DataSource = ds.Tables[0]for i=0 to Name.length-1
       textboxColumn =new DataGridViewTextBoxColumn()
       textboxColumn.HeaderText = Name(i)
       textboxColumn.Name = Name(i)
       textboxColumn.DataPropertyName = Name(i)
       dataGridView1.Columns.Add(textboxColumn)
    next
      

  6.   

    多谢以上各位,我的意思是数据源没有变,我动态改变数据表的字段后,DATAGRID或DATAGRIDVIEW里面显示的字段随着表而变换,也就是加的列是表里面的.我重新绑定也不行.
      

  7.   

    如果不想重新获取数据,在表中添加完列之后再在当前DataTable中增加一列,并设置默认值,DataGrid会自动增加一列