3

在我的 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

4 回答 4

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# 初学者的东西Youtube

于 2013-02-26T02:08:23.980 回答
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();
于 2013-02-26T04:26:40.060 回答
0

DataBind 你控制成功。

if (i > 0)
      {
          yourGridView.DataSource=YourDataSource;
          yourGridView.DataBind();
          Response.Write(" SUCCESS ");
      }
于 2013-02-26T05:38:36.930 回答
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希望这有帮助,

于 2013-02-26T02:00:24.773 回答