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

有关gridview控件编辑不显示数据问题

做一个关联两张数据库表的gridview控件,绑定后编辑不能更新数据,可以查到数据库中数据变化,但是页面无显示更新的数据。下面是主要代码:

public partial class sub : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand com;
    string strCon = "Data source =localhost;Initial Catalog =adong;User ID=sa;Password=123456";
    public static string PK1;
    public static string SID;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!this.IsPostBack)
        {
            bind();
           bind1(SID);
        }
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow) return;

        if (e.Row.FindControl("Button1") != null)
        {
            Button CtlButton = (Button)e.Row.FindControl("Button1");
            CtlButton.Click += new EventHandler(CtlButton_Click);
        }

    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView2.PageIndex = e.NewPageIndex;
        bind1(SID);

    }
    protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView2.EditIndex = -1;
        bind1(SID);

    }
    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sqlstr = "delete from component where id='" + GridView2.DataKeys[e.RowIndex].Value.ToString() + "'";
        con = new SqlConnection(strCon);
        com = new SqlCommand(sqlstr, con);
        con.Open();
        com.ExecuteNonQuery();
        con.Close();
        bind1(SID);

    }
    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView2.EditIndex = e.NewEditIndex;
        bind1(SID);

    }
    protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        con = new SqlConnection(strCon);
        string sqlstr = "update subcomponent set Pronum ='"
            + ((TextBox)(GridView2.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',SubProNum='"
            + ((TextBox)(GridView2.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',Name='"
            + ((TextBox)(GridView2.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',Path='"
            + ((TextBox)(GridView2.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "',' where id='"
            + GridView2.DataKeys[e.RowIndex].Value.ToString() + "'";

        //   string sql1 = "update jobse set jobno='" + tb.Text.ToString() + "' where id='" + dkvalue + "'";
        com = new SqlCommand(sqlstr, con);
        con.Open();
        com.ExecuteNonQuery();
        con.Close();
        // GridView2.EditIndex = -1;
        this.GridView2.EditIndex = -1;
        bind1(SID);

    }
    protected void CtlButton_Click(object sender, EventArgs e)
    {

        Button button = (Button)sender;
        GridViewRow gvr = (GridViewRow)button.Parent.Parent;
        string PK1 = GridView1.DataKeys[gvr.RowIndex].Value.ToString();
        SID = PK1;
        bind1(SID);
    }
    //绑定
    public void bind()
    {
        string sqlstr = "select * from component";
        con = new SqlConnection(strCon);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, con);
        DataSet myds = new DataSet();
        con.Open();
        myda.Fill(myds, "component");
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "ProNum" };//主键
        GridView1.DataBind();
        GridView1.HeaderRow.Cells[1].Text = "序号";
        GridView1.HeaderRow.Cells[2].Text = "资源名";
        GridView1.HeaderRow.Cells[3].Text = "产品编号";
        con.Close();
    }
    public void bind1(string str)
    {
        string sqlstr = "SELECT ID,ProNum,SubProNum,Name,Path FROM subcomponent  where ProNum = '"+ str +"'";
      con = new SqlConnection(strCon);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, con);
        DataSet myds = new DataSet();
        con.Open();
        myda.Fill(myds, "subcomponent");
                GridView2.DataSource = myds;
                GridView2.DataKeyNames = new string[] { "ID" };//主键
                GridView2.DataBind();       
               GridView2.HeaderRow.Cells[1].Text = "序号";
               GridView2.HeaderRow.Cells[2].Text = "产品总类";
               GridView2.HeaderRow.Cells[3].Text = "产品编号";
               GridView2.HeaderRow.Cells[4].Text = "产品名称";
               con.Close();

    }
}
因为band1是两张表关联后形成的,所以在页面载入的时候,band1还没有形成,因此会有问题。所以问题是我不清楚在哪个位置加入绑定gridview2,也就是band1. --------------------编程问答-------------------- 可以查到数据库中数据变化,但是页面无显示更新的数据。
既然数据库的数据已经update了,页面没显示更新的数据,那你就刷新一下页面撒。
补充:.NET技术 ,  组件/控件开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,