多谢各位,我是用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: 没有找到适合的方法来重写.",请问该怎么处理?
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: 没有找到适合的方法来重写.",请问该怎么处理?
我在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();
}
或者你用ajax实现……
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];
}
不是单纯这么加的,可以这样写
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