2009-11-18 89 views
1

其中一个GridView列必须存储Panel控件(其中包含一些控件)。问题是,该代码不起作用,我的意思是,Panel不出现在列中。GridView列中没有出现Panel控件

 Panel myPanel = new Panel(); 
     LinkButton zatw = new LinkButton(); 
     zatw.CommandName = "Accept"; 
     zatw.Text = "Accept"; 

     LinkButton odrz = new LinkButton(); 
     odrz.CommandName = "Deny"; 
     odrz.Text = "Deny"; 

     myPanel.Controls.Add(zatw); 
     myPanel.Controls.Add(odrz); 

     DataTable DT = new DataTable(); 
     DT.Columns.Add("Options", typeof(Panel)); 

     DataRow myRow = DT.NewRow(); 

     myRow1[0] = myPanel; 

     DT.Rows.Add(myRow1); 
     GridView1.DataSource = DT; 
     GridView1.DataBind(); 
     ... 

回答

1

这是因为DT.Columns.Add("Options", typeof(Panel));将不接受控件类型作为第二个参数。

来自文档。 DT.Columns的类型是

DataColumnCollection

,的确,有一个方法添加(字符串型),你使用它的。但是Type是列数据类型......它不接受控件。

实施例:

Private Sub AddColumn() 
    Dim columns As DataColumnCollection = _ 
     DataSet1.Tables("Orders").Columns 
    Dim column As DataColumn = columns.Add(_ 
     "Total", System.Type.GetType("System.Decimal")) 
    column.ReadOnly = True 
    column.Unique = False 
End Sub 

在这个例子中,正被创建的名称“合计”的列并输入“小数”。

1

我不能确切地告诉你想要做什么,但我不认为你可以用这种方式创建一个没有控制的网格。为什么没有你的网格使用模板列,然后根据你绑定的数据调整模板,而不是像你正在尝试的那样绑定到预先构建的UI?