当前位置: 代码迷 >> .NET组件控件 >> 有关gridview控件编辑不显示数据有关问题
  详细解决方案

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

热度:119   发布时间:2016-05-04 23:22:24.0
有关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;