Please read this article:Creating a Fully Editable DataGrid 
http://www.dotnetjunkies.com/tutorials.aspx?tutorialid=813
--------------------------------
AspNetPager 免费分页控件4.0版发布,欢迎下载使用:http://www.webdiyer.com

解决方案 »

  1.   

    自己定义一个模板列从ITemplate继承
      

  2.   

    你在DataGrid中添加按钮列(编辑,更新,取消),然后在DataGrid的EditCommand,UpdateCommand,CancalCommand事件中添加代码,你参考一下
    private void myDataGrid_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    myDataGrid.EditItemIndex=e.Item.ItemIndex;
    BindDataGrid();

    }private void myDataGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    TextBox ctrlData1 = (TextBox)e.Item.Cells[1].Controls[0];
    TextBox ctrlData2 = (TextBox)e.Item.Cells[2].Controls[0];
    TextBox ctrlData3 = (TextBox)e.Item.Cells[3].Controls[0];
    TextBox ctrlData4 = (TextBox)e.Item.Cells[4].Controls[0];
    string qq=ctrlData4.Text;
    TextBox ctrlData5 = (TextBox)e.Item.Cells[5].Controls[0];
    //string ctrlData5 = e.Item.Cells[5].Text; string ConnectString="data source=MATAA;initial catalog=plantask;persist security info=False;user id=sa;password=pppp;workstation id=MATAA;packet size=4096";
    SqlConnection cn=new SqlConnection(ConnectString);
    cn.Open();
    //string strsql="update plan1 set projectname='"+ctrlData1.Text+"',"+"principal='"+ctrlData2.Text+"',"+"foreseestart='"+ctrlData3.Text+"',"+"foreseeend='"+ctrlData4.Text+"',"+"content='"+ctrlData5+"'where projectno='"+e.Item.Cells[0].Text+"'";
    string strsql="UPDATE plan1 SET projectname='"+ctrlData1.Text+"',principal='"+ctrlData2.Text+"',foreseestart='"+ctrlData3.Text+"',foreseeend='"+ctrlData4.Text+"',content='"+ctrlData5.Text+"' where projectno='"+e.Item.Cells[0].Text+"'";
    //string strsql="update plan1 set projectname='"+ctrlData1.Text+"',principal='ctrlData2.',foreseestart='"+ctrlData3.Text+"',foreseeend='"+ctrlData4.Text+"',content='"+ctrlData5.Text+"'where projectno='"+e.Item.Cells[0].Text+"'";
    SqlCommand cmd=new SqlCommand(strsql,cn);
    cmd.ExecuteNonQuery();

    cn.Close();
    myDataGrid.EditItemIndex=-1;
    BindDataGrid();
    }
    private void myDataGrid_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    myDataGrid.EditItemIndex=-1;
    BindDataGrid();

    }