DataGrid dg = new DataGrid();
this.Controls.Add(dg);
dg.DataSource = ...;
dg.DataBind();

解决方案 »

  1.   

    DataGrid dg = new DataGrid();
    this.Controls.Add(dg);dg.AutoGenerateColumns = false;BoundColumn bc = new BoundColumn();
    bc.DataField = "YourColumnName";
    dg.Columns.Add(bc);
    ...dg.DataSource = ...;
    dg.DataBind();
      

  2.   

    如果需要在动态生成的datagrid中添加一列radio button,让用户每次只能选择一行,该怎么做?
    如果使用动态生成的技术,在postback中如何保持radio button的状态?
      

  3.   

    看看这个吧。以前我的贴子:
    http://expert.csdn.net/Expert/TopicView1.asp?id=1748663动态的生成DagaGrid,理论上是无限个。
      

  4.   

    -------------------------------------------------------------------------
    项目名称:Dict
    使用工具:Visual Studio .NET 2002
    -------------------------------------------------------------------------
    页面文件:[使用Panel调用显示]
    -------------------------------------------------------------------------
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb" Inherits="Dict.WebForm2"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm2</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.0">
    <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:Panel id="Panel1" style="Z-INDEX: 101; LEFT: 36px; POSITION: absolute; TOP: 46px" runat="server" /><FONT face="宋体"></FONT>
    <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 204px; POSITION: absolute; TOP: 15px" runat="server" Text="Load"></asp:Button>
    <asp:TextBox id="TextBox1" style="Z-INDEX: 103; LEFT: 37px; POSITION: absolute; TOP: 15px" runat="server"></asp:TextBox>
    </form>
    </body>
    </HTML>
    ------------------------------------------------------------------------------
    后台代码
    -----------------------------
    Public Class WebForm2
      Inherits System.Web.UI.Page
      Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel
      Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
      Protected WithEvents Button1 As System.Web.UI.WebControls.Button#Region " Web 窗体设计器生成的代码 "  '该调用是 Web 窗体设计器所必需的。
      <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()  End Sub  Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
      End Sub#End Region  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '在此处放置初始化页的用户代码
      End Sub  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim MyConnection As New SqlClient.SqlConnection("data source=localhost;initial catalog=Northwind;password=sa;user id=sa")
        Dim MySqlCommand As New SqlClient.SqlCommand("sp_help", MyConnection)
        Dim MyDataAdapter As New SqlClient.SqlDataAdapter(MySqlCommand)    MySqlCommand.CommandType = CommandType.StoredProcedure    Dim paramHelpText As New SqlClient.SqlParameter("@objname", SqlDbType.NVarChar, 776)
        paramHelpText.Value = TextBox1.Text.Trim
        MySqlCommand.Parameters.Add(paramHelpText)    MyConnection.Open()    Dim ds As New DataSet()
        MyDataAdapter.Fill(ds)
        MyConnection.Close()    Dim datagrid1 As System.Web.UI.WebControls.DataGrid
        Dim TextBox2 As System.Web.UI.WebControls.Label
        Dim i As Integer
        If ds.Tables.Count > 0 Then
          For i = 0 To ds.Tables.Count - 1
            datagrid1 = New System.Web.UI.WebControls.DataGrid()
            TextBox2 = New System.Web.UI.WebControls.Label()
            datagrid1.Font.Name = "Tahoma"
            datagrid1.Font.Size = System.Web.UI.WebControls.FontUnit.Point(9)
            datagrid1.DataSource = ds.Tables(i)
            datagrid1.DataBind()        TextBox2.Font.Name = "Tahoma"
            TextBox2.Font.Size = System.Web.UI.WebControls.FontUnit.Point(9)
            TextBox2.Text = "---------------------------" & ds.Tables(i).TableName & "---------------------------"
            Panel1.Controls.Add(TextBox2)
            Panel1.Controls.Add(datagrid1)
          Next
        End If
      End Sub
    End Class
      

  5.   

    这里数据库:Northwind 用户名sa 密码sa其中关键的填充Dataset的代码:
            Dim MyConnection As New SqlClient.SqlConnection("data source=localhost;initial catalog=Northwind;password=sa;user id=sa")
            Dim MySqlCommand As New SqlClient.SqlCommand("sp_help", MyConnection)'这里执行了一个存储过程,能返回不固定数量的记录集,以次举例
            Dim MyDataAdapter As New SqlClient.SqlDataAdapter(MySqlCommand)        MySqlCommand.CommandType = CommandType.StoredProcedure        Dim paramHelpText As New SqlClient.SqlParameter("@objname", SqlDbType.NVarChar, 776)--这里举例,他可以返回不限制数量的集合
            paramHelpText.Value = TextBox1.Text.Trim'获取文本框输入的Northwind数据库里面的对象名,可以是表,存储过程,视图等等
            MySqlCommand.Parameters.Add(paramHelpText)        MyConnection.Open()        Dim ds As New DataSet()
            MyDataAdapter.Fill(ds)'这里填充一个Dataset
            MyConnection.Close()
    '最后运行:在文本框输入一个Northwind数据库中的对象名称[可以是表、视图、存储过程、函数等等],列出该对象的所有属性,这是多个记录集,并且没有固定结果。
    --------------
    设计参考了微软的软件:DataWebAdmin,看似简单,却很牛。