<asp:DataGrid id="Dgd_student" runat="server"  PageSize="15" AllowPaging="True" OnPageIndexChanged="DataGrid_Page"  OnDeleteCommand="DataGrid_delete" DataKeyField="Student_id">
         <PagerStyle mode="NumericPages"></PagerStyle>
            <Columns>
               <asp:ButtonColumn Text="Del" CommandName="Delete"></asp:ButtonColumn>
            </Columns>
</asp:DataGrid>   public void DataGrid_delete(object sender,DataGridCommandEventArgs e)
    {
      string strconn="Server=localhost;uid=sa;pwd=;database=cms";
    
      SqlConnection cn = new SqlConnection(strconn);
    
      string strsql="delete from student where Student_id=@Student_id";
      SqlCommand cm = new SqlCommand(strsql,cn);
      cm.Parameters.Add(new SqlParameter("@Student_id",SqlDbType.VarChar,50));
      cm.Parameters["@Student_id"].Value=Dgd_student.DataKeys[(int)e.Item.ItemIndex];
      cm.Connection.Open();
      try
      {
        cm.ExecuteNonQuery();
        Lbl_note.Text="Delete OK!";
      }
      catch(SqlException)
      {
        Lbl_note.Text="Failed!";
      }
      cm.Connection.Close();
      Bindgrid();
    }为什么不能删除一行数据,页面只是刷新以下?

解决方案 »

  1.   

    设个断点,看
    Dgd_student.DataKeys[(int)e.Item.ItemIndex]的值是什么,
    建议你用e.Item.cell[0].text //索引 表示Student_id的那列
      

  2.   

    不知道你查看了数据库的数据没有?从你的情况看,如果数据库的数据还在的话,那么就应该查询具体的语句,如jerry_yuan(jerry) 所言。如果数据库已经删除了,那么应该重新绑定数据源,也即你的Bindgrid()函数里面,需要重新指定新的数据源,有可能该函数仍然是使用的数据视图,而该视图并没有被更新!!
      

  3.   

    最后二行,试一下换过来:
    cm.Connection.Close();
          Bindgrid();视图应该是先databind(),然后在关闭cm.Connection.Close();