在这里修改数据好吗?我觉得还是在另外的地方,处理这个比较好,一般的情况下我主要是用datagrid显示数据,比如说双击某记录,在弹出的具体内容中修改

解决方案 »

  1.   

    dataGrid跟数据绑定,显示应该没什么问题吧?
    设置ReadOnly=false,你可以修改了,
    删除你按Delete键试试?
    这些操作完了,你要保存到数据库去,就可以用。
    DataSet ds1 = ds.GetChanges();
    ds1中就是你新增、修改、删除了的。
    然后根据数据表中每个DataRow的RowState(Added,Modified,Deleted)
    写相应的Sql就可以了。
      

  2.   

    1.DataGrid中显示的数据是你取出到DataSet中的Table(你所帮定的表),当设置DataGrid的ReadOnly属性为false时,当你点中DataGrid中的某一行时,可以按Delete键直接删除,但这只是山除掉DataSet中的数据,并没有删除数据库中的数据,所以如果要改变数据库中的数据,还必须使用读取数据时的数据适配器的update方法。当然,在使用update方法之前好像还得加上这一句:
    this.BindingContext[this.myDataSet,"myTableName"].EndCurrentEdit();
    2.另一种方法就是使用SQL语句,就是当你点中DataGrid的某一行时,可以获取此行的所对应的表的ID:
    int iIndex=this.dataGrid1.CurrentCell.RowNumber;
    int iID=int.Pare(this.dataGrid1[0,iIndex].ToString());//0表示你的ID是放在DataGrid中的第一列。(不建议用这种方法)。
      

  3.   

    同样你修改完数据以后,也可以用数据适配器的update方法来保存数据,而且也需要使用:
    this.BindingContext[this.myDataSet,"myTableName"].EndCurrentEdit();
      

  4.   

    看看这个,也许有用:
    <%@ Page Language="C#" debug=true%>
    <%@ Import namespace="System.Data"%>
    <%@ Import namespace="System.Data.SqlClient"%>
    <html>
    <head>
    <script runat="server">
      DataTable dt = new DataTable("emp");
      SqlConnection conn = new SqlConnection("server=localhost;user id=sa;pwd=;database=pubs");
          void Page_Load(object sender, EventArgs e) 
          { 
    if(!IsPostBack)
    {
    Bind();
    }
          }
          void Page_C(object send,DataGridPageChangedEventArgs e)
          {
    DataGrid1.CurrentPageIndex = e.NewPageIndex;
    Bind();
          }
          void Bind()
          {
    SqlDataAdapter ad = new SqlDataAdapter("Select * from employee",conn); 
    ad.Fill(dt);
        DataView dv = new DataView(dt);
        DataGrid1.DataSource = dv;
        DataGrid1.DataBind();
          }
          void Edit(object se,DataGridCommandEventArgs e)
          {
    DataGrid1.EditItemIndex = e.Item.ItemIndex;
    Bind();
          }
          void Cancel(object se,DataGridCommandEventArgs e)
          {
    DataGrid1.EditItemIndex = -1;
    Bind();
          }
          void Update(object se,DataGridCommandEventArgs e)
          {
    TextBox fnt = (TextBox)e.Item.Cells[3].Controls[0];
    TextBox lnt = (TextBox)e.Item.Cells[4].Controls[0];
    string emp_id = e.Item.Cells[2].Text;
    string fname = fnt.Text;
    string lname = lnt.Text;
    string update = "Update employee set fname='"+fname+"' where emp_id='"+emp_id+"'";
    Exec(update);
    update = "Update employee set lname='"+lname+"' where emp_id='"+emp_id+"'";
    Exec(update);
    DataGrid1.EditItemIndex = -1;
    Bind();
          }
          
          void Delete(object se,DataGridCommandEventArgs e)
          {
    string emp_id = e.Item.Cells[2].Text;
    string delete = "Delete employee where emp_id='"+emp_id+"1'";
    Exec(delete);
    DataGrid1.EditItemIndex = -1;
    Bind();
          }
          void Exec(string state)
          {
    SqlCommand comm = new SqlCommand(state,conn);
    conn.Open();
    comm.ExecuteNonQuery();
    conn.Close();
          }
    </script>
    </head>
    <body>
    <form runat="server" ID="Form1">
    <asp:DataGrid ID="DataGrid1" OnEditCommand="Edit" OnCancelCommand="Cancel" OnUpdateCommand="Update" OnDeleteCommand="Delete" BorderColor="black" AutoGenerateColumns="False" AllowPaging="True" PageSize="10" Runat="server" OnPageIndexChanged="Page_C">
    <HeaderStyle BackColor="#aaaadd"></HeaderStyle>
    <PagerStyle NextPageText="Next" PrevPageText="Prev" Position="top"></PagerStyle>
    <EditItemStyle BackColor="#aaaadd"></EditItemStyle>
    <Columns>
    <asp:EditCommandColumn ButtonType="LinkButton" HeaderText="Edit" HeaderStyle-Wrap="False" ItemStyle-Wrap="false" CancelText="Cancel" EditText="Edit" UpdateText="Update"></asp:EditCommandColumn>
    <asp:ButtonColumn HeaderText="Delete" CommandName="Delete" ButtonType="LinkButton" Text="Delete"></asp:ButtonColumn>
    <asp:BoundColumn DataField="emp_id" HeaderText="&Ocirc;±&sup1;¤&ordm;&Aring;" ReadOnly="True"></asp:BoundColumn>
    <asp:BoundColumn DataField="fname" HeaderText="&micro;&Uacute;&Ograve;&raquo;&cedil;&ouml;&Atilde;&ucirc;×&Ouml;"></asp:BoundColumn>
    <asp:BoundColumn DataField="lname" HeaderText="×&icirc;&ordm;ó&Ograve;&raquo;&cedil;&ouml;&Atilde;&ucirc;×&Ouml;"></asp:BoundColumn>
    <asp:TemplateColumn HeaderText="temp">
    <EditItemTemplate>
    <Button id="bt" runat="server" text="link">Link</Button></EditItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    </form>
    </body>
    </html>
      

  5.   

    <%@ Page Language="C#" debug=true%>
    <%@ Import namespace="System.Data"%>
    <%@ Import namespace="System.Data.SqlClient"%>
    <html>
    <head>
    <script runat="server">
    DataTable dt = new DataTable("emp");
    SqlConnection conn = new SqlConnection("server=localhost;user id=sa;pwd=;database=pubs");void Page_Load(object sender, EventArgs e) 

    if(!IsPostBack)
    {
    Bind();
    }
    }void Page_C(object send,DataGridPageChangedEventArgs e)
    {
    DataGrid1.CurrentPageIndex = e.NewPageIndex;
    Bind();
    }void Bind()
    {
    SqlDataAdapter ad = new SqlDataAdapter("Select * from employee",conn); 
    ad.Fill(dt);
    DataView dv = new DataView(dt);
    DataGrid1.DataSource = dv;
    DataGrid1.DataBind();
    }void Edit(object se,DataGridCommandEventArgs e)
    {
    DataGrid1.EditItemIndex = e.Item.ItemIndex;
    Bind();
    }void Cancel(object se,DataGridCommandEventArgs e)
    {
    DataGrid1.EditItemIndex = -1;
    Bind();
    }void Update(object se,DataGridCommandEventArgs e)
    {
    TextBox fnt = (TextBox)e.Item.Cells[3].Controls[0];
    TextBox lnt = (TextBox)e.Item.Cells[4].Controls[0];
    string emp_id = e.Item.Cells[2].Text;
    string fname = fnt.Text;
    string lname = lnt.Text;
    string update = "Update employee set fname='"+fname+"' where emp_id='"+emp_id+"'";
    Exec(update);
    update = "Update employee set lname='"+lname+"' where emp_id='"+emp_id+"'";
    Exec(update);
    DataGrid1.EditItemIndex = -1;
    Bind();
    }void Delete(object se,DataGridCommandEventArgs e)
    {
    string emp_id = e.Item.Cells[2].Text;
    string delete = "Delete employee where emp_id='"+emp_id+"1'";
    Exec(delete);
    DataGrid1.EditItemIndex = -1;
    Bind();
    }void Exec(string state)
    {
    SqlCommand comm = new SqlCommand(state,conn);
    conn.Open();
    comm.ExecuteNonQuery();
    conn.Close();
    }</script>
    </head>
    <body>
    <form runat="server" ID="Form1"><asp:DataGrid ID="DataGrid1" OnEditCommand="Edit" OnCancelCommand="Cancel" OnUpdateCommand="Update" OnDeleteCommand="Delete" BorderColor="black" AutoGenerateColumns="False" AllowPaging="True" PageSize="10" Runat="server" OnPageIndexChanged="Page_C"><HeaderStyle BackColor="#aaaadd"></HeaderStyle>
    <PagerStyle NextPageText="Next" PrevPageText="Prev" Position="top"></PagerStyle>
    <EditItemStyle BackColor="#aaaadd"></EditItemStyle><Columns>
    <asp:EditCommandColumn ButtonType="LinkButton" HeaderText="Edit" HeaderStyle-Wrap="False" ItemStyle-Wrap="false" CancelText="Cancel" EditText="Edit" UpdateText="Update">
    </asp:EditCommandColumn><asp:ButtonColumn HeaderText="Delete" CommandName="Delete" ButtonType="LinkButton" Text="Delete">
    </asp:ButtonColumn><asp:BoundColumn DataField="emp_id" HeaderText="员工号" ReadOnly="True">
    </asp:BoundColumn><asp:BoundColumn DataField="fname" HeaderText="第一个名字">
    </asp:BoundColumn>
    <asp:BoundColumn DataField="lname" HeaderText="最后一个名字">
    </asp:BoundColumn><asp:TemplateColumn HeaderText="temp">
    <EditItemTemplate>
    <Button id="bt" runat="server" text="link">Link</Button>
    </EditItemTemplate>
    </asp:TemplateColumn></Columns>
    </asp:DataGrid>
    </form>
    </body>
    </html>
      

  6.   

    to 91bct(菠菜),marising(垃圾桶):我找不到Delete键,不是自己添上去的吧?
      

  7.   

    dataGrid中修改完数据后,必须用Update方法更新到数据库
      

  8.   

    谢谢各位!
    修改完数据后,用什么事件来执行Update方法呢?
      

  9.   

    如果用button2_Click的话,好象不行?
    private void button2_Click(object sender, System.EventArgs e)
    {
        ....               
        this.dataGrid1.Update();
        ...
    }
    该怎么写?
      

  10.   

    同志,一个那么大sample再那里,你怎么不看,里面说得好明白呀。