当前位置:编程学习 > C#/ASP.NET >>

gridview选择行,删除出错的问题

gridview在模板列上定义了选择行的按钮,即点选择,可以选择相应的行。
我的目地是点选择按钮,将gridview中行的字段读入到下方的几个TextBox中。下方有一个Button删除按钮,点删除就删除gridview的选择行。
问题:点击选择,可以选择gridview的相应要行(如第二条),点Button删除,也可以删除gridview的相应要行(第二条)。但是gridview会自动跳到下一条(第三条),并选择上了相应的行(第三条),这时如果再点击选择(第三条)就会出错。下面附上代码,请高手帮忙!


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class base_system_Company_add_Mange : System.Web.UI.Page
{
      
    //绑定操作
    public void bind()
    {
        SqlConnection strcon = new SqlConnection (System.Configuration.ConfigurationManager.AppSettings["strcon"]);
        strcon.Open();
        SqlDataAdapter sda = new SqlDataAdapter("select * from tb_Commpany order by id desc", strcon);
        DataSet ds = new DataSet();
        sda.Fill(ds, "tb_Commpany");
       // GridView1.DataSource = ds.Tables["tb_Commpany"];
        GridView1.DataKeyNames = new string[] { "id" };
        GridView1.DataBind();
    }
    //选择操作
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
      
          if (GridView1.SelectedIndex!=-1)
          {
                    
           keyid.Text=this.GridView1.SelectedRow.Cells[2].Text.ToString();
           CompanyName.Text = this.GridView1.SelectedRow.Cells[3].Text.ToString();
           CompanyType.Text = this.GridView1.SelectedRow.Cells[4].Text.ToString();
           CompanyShort.Text = this.GridView1.SelectedRow.Cells[5].Text.ToString();
           CompanyAddress.Text = this.GridView1.SelectedRow.Cells[6].Text.ToString();
           CompanyTel.Text = this.GridView1.SelectedRow.Cells[7].Text.ToString();
           LinkMan.Text = this.GridView1.SelectedRow.Cells[8].Text.ToString();
           UserName.Text = GridView1.SelectedDataKey["UserName"].ToString();
           AddTime.Text = GridView1.SelectedDataKey["AddTime"].ToString();
           Memory.Text = GridView1.SelectedDataKey["Memory"].ToString();
           
       }  

               
    }


    //对GridView1进行限字符操作

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
   
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            
            if((e.Row.Cells[3].Text).Length>6)
            {
                e.Row.Cells[3].Text = (e.Row.Cells[3].Text).Substring(0,3)+"…";
            }
            if ((e.Row.Cells[6].Text).Length > 6)
            {
                e.Row.Cells[6].Text = (e.Row.Cells[6].Text).Substring(0, 3) + "…";
            }
            if ((e.Row.Cells[5].Text).Length > 5)
            {
                e.Row.Cells[5].Text = (e.Row.Cells[5].Text).Substring(0, 3) + "…";
            }
        }
    
    }
    //重置操作
    protected void Button2_Click(object sender, EventArgs e)
    {
        keyid.Text = "";
        CompanyName.Text = "";
        CompanyType.Text = "";
        CompanyShort.Text = "";
        CompanyAddress.Text = "";
        CompanyTel.Text = "";
        LinkMan.Text = "";
        UserName.Text = "";
        AddTime.Text = "";
        Memory.Text = "";
    }
   //删除操作
        
    
    protected void Button4_Click(object sender, EventArgs e)
    {
        if (GridView1.SelectedIndex!=-1)
        {
          SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);
          strcon.Open();
          string id = GridView1.SelectedDataKey["id"].ToString();
          SqlCommand scd = new SqlCommand("delete from tb_Commpany where id=" + id, strcon);
          scd.ExecuteNonQuery();
          this.bind();
          keyid.Text = "";
          CompanyName.Text = "";
          CompanyType.Text = "";
          CompanyShort.Text = "";
          CompanyAddress.Text = "";
          CompanyTel.Text = "";
          LinkMan.Text = "";
          UserName.Text = "";
          AddTime.Text = "";
          Memory.Text = "";
                 
            
        }
        
    }
 //新增操作
         protected void Button1_Click(object sender, EventArgs e)
        {
            if (IsValid == true)
            {
                string CompanyName = this.CompanyName.Text.ToString();
                string keyid = this.keyid.Text.ToString();
                string CompanyType = this.CompanyType.Text.ToString();
                string CompanyShort = this.CompanyShort.Text.ToString();
                string CompanyAddress = this.CompanyAddress.Text.ToString();
                string CompanyTel = this.CompanyTel.Text.ToString();
                string LinkMan = this.LinkMan.Text.ToString();
                string UserName = Convert.ToString(Session["username"]);
                string AddTime = Convert.ToString(DateTime.Today.ToLongDateString());
                string Memory = this.Memory.Text.ToString();
                SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);
                strcon.Open();
                SqlCommand cmd = new SqlCommand("insert into tb_Commpany (keyid,CompanyName,CompanyType,CompanyShort,CompanyAddress,CompanyTel,LinkMan,UserName,AddTime,Memory)values('" + keyid + "','" + CompanyName + "','" + CompanyType + "','" + CompanyShort + "','" + CompanyAddress + "','" + CompanyTel + "','" + LinkMan + "','" + UserName + "','" + AddTime + "','" + Memory + "')", strcon);
                cmd.ExecuteNonQuery();
                strcon.Close();
                Response.Write("<script language=javascript>alert('祝贺你,你己经成功添加了一条记录');location='Company_add_Mange.aspx'</script>");
                this.bind();

             }
    
        }
   
}


--------------------编程问答-------------------- 未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误: 


行 40:            CompanyTel.Text = this.GridView1.SelectedRow.Cells[7].Text.ToString();
行 41:            LinkMan.Text = this.GridView1.SelectedRow.Cells[8].Text.ToString();
行 42:            UserName.Text = GridView1.SelectedDataKey["UserName"].ToString();
行 43:            AddTime.Text = GridView1.SelectedDataKey["AddTime"].ToString();
行 44:            Memory.Text = GridView1.SelectedDataKey["Memory"].ToString();
 

源文件: d:\xxsbwh\base_system\Company_add_Mange.aspx.cs    行: 42 

注明一下: 我的gridview中UserName,AddTime,Memory。为隐藏列。并加入到了datakeyname中了。请高手赐教!,谢谢 --------------------编程问答-------------------- 删除后要bind一下 --------------------编程问答-------------------- 同意楼上。 --------------------编程问答-------------------- 严重同意楼上 --------------------编程问答-------------------- protected void Button4_Click(object sender, EventArgs e)

bind();
还是不行。
gridview会自动跳到下一条(第三条),并选择上了相应的行(第三条),这时如果再点击选择(第三条)就会出错。下面附上代码,请高手帮忙!
--------------------编程问答-------------------- Databind()一下就ok --------------------编程问答-------------------- Databind()了不行呀,高手帮忙呀 --------------------编程问答-------------------- 是不是你的DataView1里面的那几个单元格为空啊!
你可以跟踪一下看看,既然上面的可以赋值,那就不存在设置上的问题,可能是你的DataView1里面的那几个单元格都是空的。 --------------------编程问答-------------------- protected void Button2_Click(object sender, EventArgs e)
    {
        keyid.Text = "";
        CompanyName.Text = "";
        CompanyType.Text = "";
        CompanyShort.Text = "";
        CompanyAddress.Text = "";
        CompanyTel.Text = "";
        LinkMan.Text = "";
        UserName.Text = "";
        AddTime.Text = "";
        Memory.Text = "";
        GridView1.SelectedIndex = -1;
    } --------------------编程问答-------------------- 点击选择,可以选择gridview的相应要行(如第二条),点Button删除,也可以删除gridview的相应要行(第二条)。但是gridview会自动跳到下一条(第三条),并选择上了相应的行(第三条),这时如果再点击选择(第三条)就会出错。



高手呢? --------------------编程问答-------------------- 点击选择,可以选择gridview的相应要行(如第二条),点Button删除,也可以删除gridview的相应要行(第二条)。但是gridview会自动跳到下一条(第三条),并选择上了相应的行(第三条),如果点击删除,也可以删除。但是如果不点删除,点击选择(第三条)就会出错。


提示:


未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误: 


行 40:            CompanyTel.Text = this.GridView1.SelectedRow.Cells[7].Text.ToString();
行 41:            LinkMan.Text = this.GridView1.SelectedRow.Cells[8].Text.ToString();
行 42:            UserName.Text = GridView1.SelectedDataKey["UserName"].ToString();
行 43:            AddTime.Text = GridView1.SelectedDataKey["AddTime"].ToString();
行 44:            Memory.Text = GridView1.SelectedDataKey["Memory"].ToString();
 

源文件: d:\xxsbwh\base_system\Company_add_Mange.aspx.cs    行: 42 

注明一下: 我的gridview中UserName,AddTime,Memory。为隐藏列。并加入到了datakeyname中了。请高手赐教!,谢谢

--------------------编程问答-------------------- 分不够要以加,高手在哪 里? --------------------编程问答--------------------  this.GridView1.SelectedRow.Cells[7].FindControl("Label2") as Label 来获取值 --------------------编程问答-------------------- up
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,