我用VS中的GirdView控件做删除信息的功能,我是通过取到鼠标所点到行的ID值,来找到要删除的信息,所以在GirdView的DataKeyNames属性中将值改为了ID,同时代码如下:public partial class xwlb : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Bind();
      
    }
    void Bind()
    {
        SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=0;database=用户");
        string SQL = "select Title,Postdate from News";
        SqlCommand cmd = new SqlCommand(SQL,conn);
        conn.Open();
        GV1.DataSource = cmd.ExecuteReader();
        GV1.DataBind();
        conn.Close();
    }    protected void GV1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int bh = Convert.ToInt32(GV1.DataKeys[e.RowIndex].Value);
        SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=0;database=用户");
        string SQL = "delete News where ID="+bh+"";
        SqlCommand cmd = new SqlCommand(SQL,conn);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();        Bind();
    }
}但当运行程序时,系统报错:DataBinding:“System.Data.Common.DbDataRecord”不包含名为“ID”的属性。 
因为是新手,对这个不太熟悉,不知道应该如何解决,请高手们详细说明,万分感谢!

解决方案 »

  1.   

    string sshanchu = dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString();
                    public_sql.sqlshanchu("delete from T_BasicInformation_Customer where FCustomerNumbers = " + sshanchu);
      

  2.   

     SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=0;database=用户");
            string SQL = "select Title,Postdate from News";
            SqlCommand cmd = new SqlCommand(SQL,conn);
            conn.Open();
    dataTable dt=new dataTable();
            sqldataAdpter sdr=new sqldataAdpter(cmd,conn);
    sdr.Fill(dataTable);
    GV1.DataSource = dt;        GV1.DataBind();
            conn.Close();
    让datatable做数据源 然后那一列为主键 
      

  3.   

    if (dataGridView1.SelectedRows.Count != 0) 在加个判断
      

  4.   

    绑定数据源 可以用dataset;你绑定错误了  并且确认有ID一列!
      

  5.   

    绑定数据源 可以用dataset;你绑定错误了  并且确认有ID一列!
      

  6.   

     string SQL = "delete News where ID="+bh+"";这里写错了,delete from News where ID
    另外 这个表有无ID这列,bh是否为空?
      

  7.   

    if(!IsPostBack)Bind();
          
    string SQL = "select Id,Title,Postdate from News";
      

  8.   


    protected void Page_Load(object sender, EventArgs e)
        {
              if(!IsPostback)
              {
                   Bind();
              }
        }先改成这样试试!