修改,删除:
在DataGrid里加一个编辑列和一个删除列,然后在DataGrid1_EditCommand(),DataGrid1_UpdateCommand(),DataGrid1_CancelCommand(),DataGrid1_DeleteCommand()事件中添加代码就OK了!!
添加:
可以另外拖放一些textBox(或者dropDownList什么的),其内容是相应的字段,在拖放一个Button,最后在Button_Click()事件中写添加代码就行了!!

解决方案 »

  1.   

    kinglht(爱新觉罗至尊宝)能不能说的详细一点
    DataGrid1_UpdateCommand()中的代码怎末写?
      

  2.   

    DataGrid1_UpdateCommand()
    {
    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=e.Item.Cells[2].Text;
    myCommand.Parameters["@Name"].value=e.Item.Cells[3].Text;
    ...
    ...
    myCommand.Connection.Open();try
            {
                myCommand.ExecuteNonQuery();
                
            }
            catch (SqlException)
            {
               ...
            }
    DataGrid1.EditItemIndex = -1;
    }插入,删除,参照这个!!
      

  3.   

    kinglht(爱新觉罗至尊宝),你好
      我以前用的是VB6和DELPHI对于这种DataGrid的使用和恒心方式不太熟悉,
    能不能给我发一个完整的程序研究研究,谢谢
      

  4.   

    kinglht(爱新觉罗至尊宝),你的那个e是哪里来的?
      

  5.   


    在。net中的帮助中搜索
    演练:将 XML 数据读入数据集
    这个例子比较好
      

  6.   

    还有一个建议,你可以使用第三方产品如华表,wps等实现。
      

  7.   

    UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    参数中的e!!
      

  8.   

    kinglht(爱新觉罗至尊宝) 
    我要的是WinForm的,你发表的这个e参数好像是WebForm的吧?
    能不能把这块详细的程序贴出来,谢谢
      

  9.   

    DataGrid每人用过吗?没人碰到过DataGrid更新问题吗?
      

  10.   

    private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    switch(e.CommandName)
    {
    case "Edit":
    {
    ((DataGrid)source).EditItemIndex = e.Item.ItemIndex;
    break;
    }
    case "Page":
    {
    ((DataGrid)source).CurrentPageIndex = Convert.ToInt32(e.CommandArgument)--;
    break;
    }
    case "Update":
    {
    SqlCommand comm = new SqlCommand("updqte table1 set ... where idx = "+e.CommandArgument,new SqlConnection("DNS"));
    comm.Connection.Open();
    comm.ExecuteNonQuery();
    comm.Connection.Close();
    comm.Dispose();
    break;
    }
    case "Delete":
    {
    SqlCommand comm = new SqlCommand("delete from table1 where idx = "+e.CommandArgument,new SqlConnection("DNS"));
    comm.Connection.Open();
    comm.ExecuteNonQuery();
    comm.Connection.Close();
    comm.Dispose();
    break;
    }
    default:
    break;
    }
    BindGrid((DataGrid)source);
    } private void BindGrid(DataGrid grid)
    {
    SqlDataAdapter adp = new SqlDataAdapter("sql语句","DNS");
    DataTable dt = new DataTable();
    adp.Fill(dt); grid.DataSource = dt;
    grid.DataBind(); adp.Dispose();
    dt.Dispose();
    }
      

  11.   

    我写的是伪代码,你需要把sql语句换成自己的sql语句才可以:)