datagrid不是允许修改的吗?
ASP.Net数据库入门——C#编程中的一个实例如下:<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %><html>
  <head>
    <title>Updating Beverages</title>
  </head>
  <body>
    <form method="post" runat="server">
      <asp:DataGrid id="dgProducts" runat="server"
                    CellPadding="5" AutoGenerateColumns="False"
                    OnEditCommand="EditRecord"
                    OnCancelCommand="CancelEdit"
                    OnUpdateCommand="UpdateRecord">
        <Columns>
          <asp:BoundColumn DataField="ProductID" ReadOnly="True"
                        Visible="False" />
          <asp:BoundColumn DataField="ProductName" ReadOnly="True"
                        HeaderText="Name" />
          <asp:BoundColumn DataField="UnitPrice" HeaderText="Price" />
          <asp:EditCommandColumn ButtonType="LinkButton"
                        UpdateText="Save" CancelText="Cancel"
                        EditText="Edit" />
        </Columns>
      </asp:DataGrid>
    </form>
  </body>
</html><script language="c#" runat="server">
private String strConnection = ConfigurationSettings.AppSettings["NWind"];
private String strSQLSelect = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE CategoryID = 1";
private String ProductTableName = "ProductTable";
private SqlConnection objConnection;private void Page_Load(object sender, System.EventArgs e)
{
  if (!IsPostBack)
    LoadGrid();
}private void LoadGrid()
{
  Connect();
  SqlDataAdapter adapter = new SqlDataAdapter(strSQLSelect, objConnection);
  DataSet ds = new DataSet();
  adapter.Fill(ds, ProductTableName);
  Disconnect();  dgProducts.DataSource = ds.Tables[ProductTableName];
  dgProducts.DataBind();
}private void Connect()
{
  if (objConnection == null)
    objConnection = new SqlConnection(strConnection);  if (objConnection.State == ConnectionState.Closed)
    objConnection.Open();
}private void Disconnect()
{
  objConnection.Close();
}public void EditRecord(object sender, DataGridCommandEventArgs e)
{
  dgProducts.EditItemIndex = e.Item.ItemIndex;
  LoadGrid();
}public void CancelEdit(object sender, DataGridCommandEventArgs e)
{
  dgProducts.EditItemIndex = -1;
  LoadGrid();
}public void UpdateRecord(object sender, DataGridCommandEventArgs e)
{
  // Retrieve the field values in the edited row
  int ProductID = Convert.ToInt32(e.Item.Cells[0].Text);
  TextBox PriceTextBox = (TextBox)e.Item.Cells[2].Controls[0];
  decimal Price = Convert.ToDecimal(PriceTextBox.Text);  dgProducts.EditItemIndex = -1;
  UpdateProduct(ProductID, Price);  DataSet ds = new DataSet();
  dgProducts.DataSource = ds.Tables[ProductTableName];
  dgProducts.DataBind();
}private void UpdateProduct(int ProductID, decimal Price)
{
  // Create and load a DataSet with records from Northwind.Products table
  Connect();
  SqlDataAdapter adapter = new SqlDataAdapter(strSQLSelect, objConnection);
  DataSet ds = new DataSet();
  adapter.Fill(ds, ProductTableName);
  Disconnect();  // Modify the in-memory records in the DataSet
  DataTable tbl = ds.Tables[ProductTableName];
  tbl.PrimaryKey = new DataColumn[] {tbl.Columns["ProductID"]};
  DataRow row = tbl.Rows.Find(ProductID);
  row["UnitPrice"] = Price;  // Reconnect the DataSet and update the database
  SqlCommandBuilder cb = new SqlCommandBuilder(adapter);
  Connect();
  adapter.Update(ds, ProductTableName);
  Disconnect();
}
</script>

解决方案 »

  1.   

    seeA Truly Excel-like Grid Control
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndive/html/data02142002.asp
      

  2.   

    我想在下面的textbox中进行修改 删除 添加 不是在datagrid表中
      

  3.   

    建议使用 repeater,然后帮定数据到<asp:repeater id="repeater_adjlist" Runat="server">
    <HeaderTemplate>
    <table border="0" width="100%">
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td><asp:imagebutton id="ImagebuttonDelItem" CommandName="DelItem" runat="server" AlternateText="删除" ImageUrl="~/images/drop.gif"></asp:imagebutton></td>
    <td align="left">
    <a href='<%# DataBinder.Eval(Container.DataItem,"Url") %>' target=_blank>'<%# DataBinder.Eval(Container.DataItem,"Name") %>'</a>
    </td>
    <td><asp:TextBox ID=lbUrl Visible=False Runat=server Text='<%# DataBinder.Eval(Container.DataItem, "Url") %>' ></asp:TextBox></td>
    <td><asp:Label ID=lbName  Visible=False Runat=server  Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' ></asp:Label></td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </TABLE>
    </FooterTemplate> </asp:repeater>
    需要修改的地方使用textbox,不需要修改的地方使用 label或者 textbox 的 readonly=true
    榜定的数据全部在textbox中,修改其中的值,提交的时候将reapter的行循环读出foreach(ReeaterItem item in repeater1.Items)
    {
      .....
      每行数据处理....
    }
    ,关于删除,在repeater前面一列有个删除,你写入事件去控制删除。if(e.Command == 'Delete')
    {
      ...
      控制该行删除....
    }
      

  4.   

    datagrid的selectedindexchanged事件不可以吗
      

  5.   

    你的datagrid是表名还是数据网格控件?
    我看你的意思是表名,
    如果你要将一个表的记录绑定到textbox的text属性上,
    如果你读取出来的记录是多条的话,
    默认情况下在text上显示的是首行记录,
    如果你没有控制让它显示具体那一行记录,
    你每次只能修改首行记录,这样就达不到你想要的效果了,
    要不然你可以控制每次只取出一条记录。
    你可以利用一个数据网格控件datagrid来显示数据,
    用几个textbox来用做修改数据(暂且不谈直接在datagrid上修改的情况),
    当你选中datagrid上的第几行记录时,textbox上就会显示相应的第几条记录。
      

  6.   

    我给你代码:
    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 62px; POSITION: absolute; TOP: 73px" runat="server" Height="25px" Width="367px" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" BackColor="White" GridLines="Horizontal" DataKeyField="xue_id" CellPadding="3" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#ccff33" OnEditCommand="MyDataGrid_Edit" OnCancelCommand="MyDataGrid_Cancel" OnUpdateCommand="MyDataGrid_Update" AutoGenerateColumns="False" AllowSorting="True" OnSortCommand="MyDataGrid_Sort" Font-Names="Verdana" BackImageUrl="file:///F:\Inetpub\wwwroot\web.gif" PageSize="3" AllowPaging="True">
    <SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle>
    <AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>
    <ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
    <HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
    <FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
    <Columns>
    <asp:BoundColumn DataField="username" ReadOnly="True" HeaderText="姓名"></asp:BoundColumn>
    <asp:BoundColumn DataField="xue_id" HeaderText="学号"></asp:BoundColumn>
    <asp:BoundColumn DataField="yuwen" HeaderText="语文"></asp:BoundColumn>
    <asp:BoundColumn DataField="shuxue" HeaderText="数学"></asp:BoundColumn>
    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" HeaderText="编辑命令" CancelText="取消" EditText="编辑">
    <HeaderStyle Wrap="False"></HeaderStyle>
    <ItemStyle Wrap="False"></ItemStyle>
    </asp:EditCommandColumn>
    </Columns>
    <PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>
    </asp:DataGrid>几个事件代码:
    public void MyDataGrid_Edit(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    this.DataGrid1.EditItemIndex=(int)e.Item.ItemIndex;
    kk();
    } public void MyDataGrid_Cancel(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    this.DataGrid1.EditItemIndex=-1;
    kk();
    } public void MyDataGrid_Update(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    int id=Convert.ToInt32(((TextBox)e.Item.Cells[1].Controls[0]).Text);
    int yuwen=Convert.ToInt32(((TextBox)e.Item.Cells[2].Controls[0]).Text);
    int shuxue=Convert.ToInt32(((TextBox)e.Item.Cells[3].Controls[0]).Text);
    WFDG.update(id,yuwen,shuxue);//是对数据库操作的函数
    }