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;
using hfnet;public partial class admin_GoodsClass : System.Web.UI.Page
{
    public GridView GridView1;
    //public string init;
    //public static string a;
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.RegisterStartupScript("MyScript", "<Script language=javascript defer='true'> autoscroll() </script>"); 
        //if (!IsPostBack)
        //{
        //    bindGridView("商品分类", "商品大类");
        //    ////bindGridView("品牌", "品牌");
        //    //a = "商品大类";        //} 
        
    }    public void bindGridView(string tablename, string colsname)
    {
        GridView1 = new GridView();
        string sql = @"select ID as 编号," + colsname + " from " + tablename;
        DataSet ds = BLL.ExecuteDataSet(BLL.ConnectString, CommandType.Text, sql);
        GridView1.DataSource = ds.Tables[0].DefaultView;
        GridView1.AutoGenerateColumns = false;
        GridView1.RowDataBound+=new GridViewRowEventHandler(GridView1_RowDataBound);        
        for (int i = 0; i <ds.Tables[0].Columns.Count; i++)
        {
            BoundField bf = new BoundField();
            bf.DataField = ds.Tables[0].Columns[i].ToString();
            bf.HeaderText = ds.Tables[0].Columns[i].Caption.ToString();
            GridView1.Columns.Add(bf);
        }
        TemplateField tf = new TemplateField();
        tf.ItemTemplate = new MyTemplate("", DataControlRowType.DataRow);
        tf.HeaderTemplate = new MyTemplate("", DataControlRowType.Header);
        tf.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
        GridView1.Columns.Add(tf);        GridView1.DataKeyNames = new string[] { "编号" };
        GridView1.DataBind();        //form1.Controls.Add(GridView1);
        form1.FindControl("gridview1").Controls.Add(GridView1);
        //return GridView1;
        //int ii = (int)GridView1.Columns.Count;    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string a = ListBox1.SelectedValue;
            string b = a;
            if (a == "商品分类")
            {
                b = "商品大类";
            }            isinsert(a, b);        }
        catch
        {
            
        }
    }    private void isinsert(string tablename, string colsname)
    {
        string sql = @"insert into "+tablename+"("+colsname+")  values ('" + TextBox1.Text + "')";
        if (BLL.ExecuteNonQuery(BLL.ConnectString, CommandType.Text, sql, null) > 0)
        {
            bindGridView(tablename,colsname);
        }
    }    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "if(this!=prevselitem){this.style.backgroundColor='#Efefef'}");//当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseout", "if(this!=prevselitem){this.style.backgroundColor='#ffffff'}");//当鼠标移开时还原背景色
            e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this)");
        }
        
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        
        isdelete(ListBox1.SelectedValue);
    }    private void isdelete(string str)
    {
       
            int j = -1;
            for (int i = 0; i < GridView1.Rows.Count - 1; i++)   //未将对象引用设置到对象的实例  
            {
                CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                if (cbox.Checked == true)
                {
                    string sqlstr = @"delete from " + str + " where ID='" + GridView1.DataKeys[i].Value + "'";
                    j = BLL.ExecuteNonQuery(BLL.ConnectString, CommandType.Text, sqlstr, null);
                }
            }
            if (j > 0)
            {
                Response.Write("<script>alert('成功')</script>");
                //bindGridView();
            }
        
    }
    
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            string a = ListBox1.SelectedValue;
            string b = a;
            if (a == "商品分类")
            {
                b = "商品大类";
            }
            bindGridView(a, b);        }
        catch
        {        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {    }
}这个问题应该怎么解决呢?

解决方案 »

  1.   

    CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
    ------------------------
    CheckBox cbox = (CheckBox)this.GridView1.Rows[i].Cells["列名"].FindControl("CheckBox1");
      

  2.   

    GridView1 为 nullpublic GridView GridView1;你这里定义 并没赋值,如果  isdelete   在 bindGridView 之前运行 ,就会报错
      

  3.   


    还要初始化下 CheckBox cbox=new CheckBox();
    不要直接CheckBox cbox=(CheckBox)this.GridView1.Rows[i].Cells["列名"].FindControl("CheckBox1");
      

  4.   

    gridview没有new GridView();
    出错
    if(gridview!=null){}
    Page_Load中new GridView();
      

  5.   

    一般可能有两中情况的,如果 isdelete 在 bindGridView 之前运行会报错的,因为你还没有绑定。。
    第二个可能就是回发造成的影响,具体怎么样我也不太说的清楚,但是解决的办法就是在声明的时候字段的时候直接New出来先。
      

  6.   

    调试的时候,也感觉到是这个问题,问题是,怎么来修改一下呢。在这里还有一个问题,就是,我没有在gridview中直接添加增删改,而是另外定义了一个LinkButton来处理删除操作。请指教,谢谢。
      

  7.   

    在 Page_Load 的时候调用 bindGridView 这个方法,需要注意的是不考虑 IsPostBack 的情况就是每次都调用。删除有什么问题?
      

  8.   

    直接在page_load里调用bindGridView用linkbutton删除
    <asp:LinkButton ID="lbtn" runat="server" commandAgrument='<%# Eval("字段名")%'></asp:LinkButton>click事件接收
    LinkButton lb=(LinkButton)sender;
    string a=lb.commandArgrument;
      

  9.   

    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {       
                bindGridView(a, b);
        }我在这里已经绑定过一次了。先于isdelete()执行。为什么在isdelete中。gridview1中数据为空呢?测试了一下,数据是为空。楼上的,每次可以删一行数据。不能把多项删除,试了一下,好像这样,
      

  10.   


    在一个方法里面定义了一个GridView 并进行了绑定,在另一外方法里能不能直接引用对象,有哪些方法可以调用这个GridView 里的数据呢,在另一个方法里。谢谢
      

  11.   

    GridView1.Rows 包含页眉,页尾so判定前请先确定row是否是datarow
      

  12.   


    我在Page_Load中测试过。那里面GridView1.Rows.Count是有数据的。
      

  13.   


    和count无关,我指的是rowType,而非啥count if (gv.Rows[i].RowType== DataControlRowType.DataRow)
                {
                
                }
      

  14.   

    唉,还是解决不了!GridView能不能进行深拷贝的吗?