嵌套gridview asp.net上的更新按钮c#

时间:2016-08-08 06:50:42

标签: c# asp.net gridview

我有这个嵌套的网格视图,其中的数据显示在文本框中,我也有一个更新按钮但是当我单击按钮而不是更新数据表上的数据时,它只是更新网格视图本身的新值并显示它与旧值,但我不会得到任何错误。请帮助我。

代码:

private static DataTable GetData(string query)
    {
        string strConnString = ConfigurationManager.ConnectionStrings["MandapamDatabase"].ConnectionString;
        using (OleDbConnection con = new OleDbConnection(strConnString))
        {
            using (OleDbCommand cmd = new OleDbCommand())
            {
                cmd.CommandText = query;
                using (OleDbDataAdapter sda = new OleDbDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataSet ds = new DataSet())
                    {
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }
    }

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string ID = gvSDate.DataKeys[e.Row.RowIndex].Value.ToString();
            GridView gvSDate2 = e.Row.FindControl("gvSDate2") as GridView;
            gvSDate2.DataSource = GetData(string.Format("select ID, Function, FunctionDate, FunctionTime, CelebrateName from function where ID={0}", ID));
            gvSDate2.DataBind();
        }
    }
protected void gvSDate2_RowDataBound(object sender, GridViewRowEventArgs f)
    {

        if (f.Row.RowType == DataControlRowType.DataRow)
        {
            GridView gvSDate2 = f.Row.FindControl("gvSDate2") as GridView;
            TextBox textFunction = f.Row.FindControl("textFunction") as TextBox;
            TextBox textFunctionDate = f.Row.FindControl("textFunctionDate") as TextBox;
            TextBox textFunctionTime = f.Row.FindControl("textFunctionTime") as TextBox;
            TextBox textCelebrateName = f.Row.FindControl("textCelebrateName") as TextBox;
            Label lblID = f.Row.FindControl("lblID") as Label;
            Button update1 = f.Row.FindControl("update1") as Button;
            string Function = textFunction.Text;
            string FunctionDate = textFunctionDate.Text;
            string FunctionTime = textFunctionTime.Text;
            string CelebrateName = textCelebrateName.Text;
            string ID1 = lblID.Text;
            update1.Click += (send, g) =>
            {
                string connString = ConfigurationManager.ConnectionStrings["MandapamDatabase"].ConnectionString;
                OleDbConnection connection = new OleDbConnection(connString);
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string updateQuery = "UPDATE function SET Function=@Function, FunctionDate=@FunctionDate, FunctionTime=@FunctionTime, CelebrateName=@CelebrateName WHERE ID=@ID";
                command.CommandType = CommandType.Text;
                command.CommandText = updateQuery;
                command.Parameters.AddWithValue("@ID", ID1);
                command.Parameters.AddWithValue("@Function", Function);
                command.Parameters.AddWithValue("@FunctionDate", FunctionDate);
                command.Parameters.AddWithValue("@FunctionTime", FunctionTime);
                command.Parameters.AddWithValue("@CelebrateName", CelebrateName);

            };
        }
    }

0 个答案:

没有答案