按钮单击后刷新Gridview

时间:2013-02-26 01:54:59

标签: c# asp.net sql

在我的asp.net项目中,如何在单击按钮后立即刷新gridview。 我的按钮有更新代码。这是代码;

protected void Button3_Click(object sender, EventArgs e)
    {
        string strSQL = "UPDATE [bilgiler3] SET [HAM_FM] = ISNULL(MON,0)+ISNULL(TUE,0)+ISNULL(WED,0)+ISNULL(THU,0)+ISNULL(FRI,0)+ISNULL(SAT,0)+ISNULL(SUN,0) WHERE [DATE] BETWEEN @DATE1 AND @DATE2 AND WORK_TYPE='OUT'";
        string connStr = WebConfigurationManager.ConnectionStrings["asgdb01ConnectionString"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            using (SqlCommand comm = new SqlCommand())
            {
                comm.Connection = conn;
                comm.CommandText = strSQL;
                comm.CommandType = CommandType.Text;

                comm.Parameters.AddWithValue("@DATE1", Convert.ToDateTime(TextBox1.Text));
                comm.Parameters.AddWithValue("@DATE2", Convert.ToDateTime(TextBox2.Text));
                try
                {
                    conn.Open();
                    int i = comm.ExecuteNonQuery();
                    conn.Close();
                    if (i > 0)
                    {
                        Response.Write(" SUCCESS ");
                    }
                    else
                    {
                        Response.Write(" ERROR ! ");
                    }
                }
                catch (SqlException ex)
                {
                    Response.Write(ex.ToString());
                }
            }
        }
    }

你可能会说“你需要绑定gridview的数据”但我无法理解这种方法。 你能帮我解决一下吗?

非常感谢你。

4 个答案:

答案 0 :(得分:2)

我会做以下事情:

DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter();
 SqlConnection sc = new SqlConnection("you connection string here Security=True");


private void loadData()
        {
            try
            {
                ds.Clear();
                SqlCommand sCmd= new SqlCommand("Load your database", sc);
                sda.SelectCommand = sCmd;
                sda.Fill(ds, "sCmd");

                datagrid.DataSource = ds.Tables["sCmd"];
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.ExitThread();
            }

        }

c#beginners Youtube

的内容

答案 1 :(得分:1)

点击按钮

添加此代码
SqlConnection con = new SqlConnection("Connection string from web config");
    DataSet ds = new DataSet();
    SqlDataAdapter sda = new SqlDataAdapter("select * from EmployeeTable", con);
    con.Open();
    sda.Fill(ds ,"Data");
gridview1.datasource=ds.tables[0];
gridview1.DataBind();

答案 2 :(得分:0)

 private SqlConnection con;
 private SqlConnectionStringBuilder str;

private void Form8_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            str = new SqlConnectionStringBuilder();
            str.Provider = "";
            str.DataSource = @"source.accdb";
            con = new SqlConnection(str.ConnectionString);
            dataGridView1.DataSource = fillTable("Select* from yourTable");
        }

        private DataTable fillTable(string sql)
        {
            DataTable datatable = new DataTable();
            using (SqlDataAdapter da = new SqlDataAdapter(sql, con))
            {
                da.Fill(datatable);
            }

            return datatable;
        }

然后如果您想刷新表格,请将loaddata();放入事件button_Click希望此帮助,

答案 3 :(得分:0)

DataBind您对成功的控制。

if (i > 0)
      {
          yourGridView.DataSource=YourDataSource;
          yourGridView.DataBind();
          Response.Write(" SUCCESS ");
      }