如何在不进行编辑模式的情况下立即更新gridview中的计算列?

时间:2011-04-13 03:14:17

标签: c# asp.net

我在itemtemplate中使用autopost,但它不会立即更新结果列

我想使用multiplier_dropdownlist_SelectedIndexChanged来更新gridview,但是 没有gridview.update();如何立即更新显示结果

[解决]

  protected void Timer1_Tick(object sender, EventArgs e)
            {
                for (int i = 0; i < Calculator_GridView.Rows.Count; i++)
                {
                    string serial_no = Calculator_GridView.Rows[i].Cells[1].Text;
                    string a1_textbox = Calculator_GridView.Rows[i].Cells[2].Text;
                    string b1_textbox = Calculator_GridView.Rows[i].Cells[3].Text;
                    DropDownList mp_dropdown = (DropDownList)Calculator_GridView.Rows[i].Cells[4].Controls[1];
                    //TextBox Result_textbox = (TextBox)Calculator_GridView.Rows[e.RowIndex].Cells[5].Controls[0];


                string executestring = "";
                executestring = "Update cal set a1=" + a1_textbox;
                executestring = executestring + ", b1=" + b1_textbox;
                executestring = executestring + ", mp=" + mp_dropdown.SelectedValue;
                executestring = executestring + ", result=" + (Convert.ToDouble(mp_dropdown.SelectedValue) * Convert.ToDouble(b1_textbox)).ToString();
                executestring = executestring + " where [識別碼]=" + serial_no;
                ExecuteDatabase(executestring);
            }

            string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/db1.mdb";
            OleDbConnection conn = new OleDbConnection(connstr);
            conn.ConnectionString = connstr;

            try
            {
                conn.Open();
            }
            catch (Exception ex)
            {
                conn.Close();
            }

            OleDbCommand get_info_cmd = null;
            get_info_cmd = new OleDbCommand("SELECT [識別碼], [a1], [b1], [result], [mp] FROM [cal]", conn);

            OleDbDataReader get_info_Reader = get_info_cmd.ExecuteReader();

            store.Columns.Add(new DataColumn("識別碼", typeof(int)));
            store.Columns.Add(new DataColumn("a1", typeof(double)));
            store.Columns.Add(new DataColumn("b1", typeof(double)));
            store.Columns.Add(new DataColumn("mp", typeof(double)));
            store.Columns.Add(new DataColumn("result", typeof(double)));

            DataRow dr;

            try
            {
                while (get_info_Reader.Read())
                {
                    dr = store.NewRow();
                    dr[0] = get_info_Reader["識別碼"].ToString();
                    dr[1] = get_info_Reader["a1"].ToString();
                    dr[2] = get_info_Reader["b1"].ToString();
                    dr[3] = get_info_Reader["mp"].ToString();
                    dr[4] = (Convert.ToDouble(get_info_Reader["b1"].ToString()) * Convert.ToDouble(get_info_Reader["mp"].ToString())).ToString();

                    store.Rows.Add(dr);
                }
            }
            catch (Exception ex)
            {
                Error_Label.Text = Error_Label.Text + ex.ToString();
                conn.Close();
            }
            finally
            {
                get_info_cmd.Dispose();
                get_info_Reader.Close();
                conn.Close();
            }
            storeview = new DataView(store);

            Calculator_GridView.Font.Size = new FontUnit(10);
            Calculator_GridView.DataSourceID = "";
            Calculator_GridView.DataSource = storeview;
            Calculator_GridView.DataBind();
        }

1 个答案:

答案 0 :(得分:2)

将gridview保存在ajax更新面板中。之后ajax控件窗格的内容模板使用定时器控件,该控件将定期自动更新你的gridview