<asp:TemplateColumn>
<ItemTemplate>
   <asp:Button Runat=server text="name" CommandName='<% DataBinder.Eval(Container.DataItem,"表关键字")%>'></asp:Button>
         </ItemTemplate>
</asp:TemplateColumn>
然后在后台做事件就可以

解决方案 »

  1.   

    看看这个
    http://expert.csdn.net/Expert/topic/2397/2397807.xml?temp=.9014398
      

  2.   

    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %><html><script language="C#" runat="server">    SqlConnection myConnection;    protected void Page_Load(Object Src, EventArgs E)
        {
            myConnection = new SqlConnection("server=(local)\\NetSDK;database=pubs;Trusted_Connection=yes");        if (!IsPostBack)
                BindGrid();
        }    public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e)
        {
            MyDataGrid.EditItemIndex = (int)e.Item.ItemIndex;        BindGrid();
            
            for (int i = 0; i < e.Item.Cells.Count; i++)
            {
            
                e.Item.Cells[i].Text = Server.HtmlDecode(e.Item.Cells[i].Text);
                Trace.Write(e.Item.Cells[i].Text);
            }
        }    public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e)
        {
            MyDataGrid.EditItemIndex = -1;
            BindGrid();
        }    public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)
        {
            String updateCmd = "UPDATE Authors SET au_id = @Id, au_lname = @LName, au_fname = @FName, phone = @Phone, "
                 + "address = @Address, city = @City, state = @State, zip = @Zip, contract = @Contract where au_id = @Id";        SqlCommand myCommand = new SqlCommand(updateCmd, myConnection);        myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11));
            myCommand.Parameters.Add(new SqlParameter("@LName", SqlDbType.NVarChar, 40));
            myCommand.Parameters.Add(new SqlParameter("@FName", SqlDbType.NVarChar, 20));
            myCommand.Parameters.Add(new SqlParameter("@Phone", SqlDbType.NChar, 12));
            myCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.NVarChar, 40));
            myCommand.Parameters.Add(new SqlParameter("@City", SqlDbType.NVarChar, 20));
            myCommand.Parameters.Add(new SqlParameter("@State", SqlDbType.NChar, 2));
            myCommand.Parameters.Add(new SqlParameter("@Zip", SqlDbType.NChar, 5));
            myCommand.Parameters.Add(new SqlParameter("@Contract", SqlDbType.NVarChar,1));        myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex];        String[] cols = {"@Id","@LName","@FName","@Phone","@Address","@City","@State","@Zip","@Contract"};        int numCols = e.Item.Cells.Count;
            for (int i=2; i<numCols-1; i++) //skip first, second and last column
            {
                String colvalue =((TextBox)e.Item.Cells[i].Controls[0]).Text;            // check for null values in required fields
                if (i<6 && colvalue == "")
                {
                    Message.InnerHtml = "ERROR: Null values not allowed for Author ID, Name or Phone";
                    Message.Style["color"] = "red";
                    return;
                }            myCommand.Parameters[cols[i-1]].Value = Server.HtmlEncode(colvalue);
            }        //append last row, converting true/false values to 0/1
            if (String.Compare(((TextBox)e.Item.Cells[numCols-1].Controls[0]).Text, "true", true)==0)
                        myCommand.Parameters["@Contract"].Value =  "1";
            else
                        myCommand.Parameters["@Contract"].Value =  "0";        myCommand.Connection.Open();        try
            {
                myCommand.ExecuteNonQuery();
                Message.InnerHtml = "<b>Record Updated</b><br>" + updateCmd;
                MyDataGrid.EditItemIndex = -1;
            }
            catch (SqlException exc)
            {
                if (exc.Number == 2627)
                    Message.InnerHtml = "ERROR: A record already exists with the same primary key";
                else
                    Message.InnerHtml = "ERROR: Could not update record, please ensure the fields are correctly filled out";
                Message.Style["color"] = "red";
            }        myCommand.Connection.Close();        BindGrid();
        }    private void MyDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.EditItem)
            {
                for (int i = 0; i < e.Item.Controls.Count; i++)
                {
                    if (e.Item.Controls[i].Controls[0].GetType().ToString() == "System.Web.UI.WebControls.TextBox")
                    {
                        TextBox tb = (TextBox)e.Item.Controls[i].Controls[0];
                        tb.Text = Server.HtmlDecode(tb.Text);
                    }
                }
            }
        }
            
        public void BindGrid()
        {
            SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors", myConnection);        DataSet ds = new DataSet();
            myCommand.Fill(ds, "Authors");        MyDataGrid.DataSource=ds.Tables["Authors"].DefaultView;
            MyDataGrid.DataBind();
        }</script><body style="font: 10pt verdana">  <form runat="server">    <h3><font face="Verdana">Updating a Row of Data</font></h3>    <span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/><p>    <ASP:DataGrid id="MyDataGrid" runat="server"
          Width="800"
          BackColor="#ccccff"
          BorderColor="black"
          ShowFooter="false"
          CellPadding=3
          CellSpacing="0"
          Font-Name="Verdana"
          Font-Size="8pt"
          HeaderStyle-BackColor="#aaaadd"
          OnEditCommand="MyDataGrid_Edit"
          OnCancelCommand="MyDataGrid_Cancel"
          OnUpdateCommand="MyDataGrid_Update"
          DataKeyField="au_id"
          OnItemDataBound="MyDataGrid_ItemDataBound"
        >      <Columns>
            <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" ItemStyle-Wrap="false"/>
          </Columns>    </ASP:DataGrid>  </form></body>
    </html>
      

  3.   

    see 
    http://www.tongyi.net/article/20020121/200201213034.shtml
      

  4.   

    public DataGrid MakeGrid()
       {
        mygrid.CellPadding=2;
        mygrid.Attributes.Add("align","center");
        mygrid.CellSpacing=0;
        mygrid.Width=500;
        mygrid.BorderWidth=1;
        mygrid.BorderColor=ColorTranslator.FromHtml("Black");
        mygrid.AutoGenerateColumns=false;
        mygrid.ForeColor=ColorTranslator.FromHtml("Black");
        mygrid.Font.Size=9;
        mygrid.Font.Name="宋体";
        mygrid.AllowSorting=true;    ///sort命令的事件处理器    mygrid.SortCommand += new DataGridSortCommandEventHandler(Sort_Grid);    ///设置headerstyle
        mygrid.HeaderStyle.BackColor=ColorTranslator.FromHtml("Gold");
        mygrid.HeaderStyle.ForeColor=ColorTranslator.FromHtml("Black");
        mygrid.HeaderStyle.Font.Name="宋体";
        mygrid.HeaderStyle.Font.Size=9;
        mygrid.HeaderStyle.Font.Bold=true;
        mygrid.HeaderStyle.HorizontalAlign=HorizontalAlign.Center;
      

  5.   

    <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" Width="40px">
    <Columns>
    <asp:TemplateColumn>
    <ItemStyle Wrap="False"></ItemStyle>
    <ItemTemplate>
    <asp:Label id="lbl1" runat="server"/>
    <table>
    <tr>
    <td><asp:Literal id="lit1" runat=server /><td>
    <td><asp:Literal id="lit2" runat=server /><td>
    </tr>
    <tr>
    <td colspan="2"><asp:Literal id="lit3" runat=server /><td>
    </tr>
    </table>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>in DataGrid's ItemDataBound event handler, dovoid DataGrid1_OnItemDataBound(Object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    ListItemType.AlternatingItem)
        {
    string s1= DataBinder.Eval(e.Item.DataItem, "ID").ToString();
             Label lbl = (Label)e.FindControl("lbl1");
             lbl1.Text = s1; string s2= DataBinder.Eval(e.Item.DataItem, "user").ToString();
             Literal lit1 = (Literal)e.FindControl("lit1");
             lit1.Text = s2;
     
    string s3= DataBinder.Eval(e.Item.DataItem, "Tax").ToString();
             Literal lit2 = (Literal)e.FindControl("lit2");
             lit2.Text = s3; string s4= DataBinder.Eval(e.Item.DataItem, "addr").ToString();
             Literal lit3 = (Literal)e.FindControl("lit3");
             lit3.Text = s4;
     
        }
    }
      

  6.   

    http://chs.gotdotnet.com/quickstart/aspplus/
      

  7.   

    大家似乎不太理解我说的意思!是这样的!
    我动态产生 DataGrid mygrid = new DataGrid();
    给Grid加列 mygrid.cloumns.add(mytemplatecol);//mytemplatecol是我写一个类产生的模板列;问题是我不会产生一个按钮列,像静态DataGrid的按钮列一样有编辑更新取消按钮,还有就是这些按钮事件怎样获得!那位有这方面的资料,指点一下阿!感激不尽...
      

  8.   

    有是有办法,不过我不推荐你这样做,这种方法又把我们带回asp中的老路上了。
    我建议不要动态添加,不用时你隐藏就行了,动态添加的控件是不会引发回发事件的
      

  9.   

    例:产生按钮列
    private void CreateButtonColumn()
    {
        ButtonColumn dgc=new ButtonColumn ();
    dgc.ButtonType =ButtonColumnType.LinkButton ;
    dgc.CommandName="Select";
    dgc.HeaderStyle .BackColor =Color.RoyalBlue ;
                dgc.Text ="选择";

    dg.Columns .Add (dgc);
    }
    在为它加个事件
    this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
    private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e){}
      

  10.   

    http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20693715.html