2013-02-26 83 views
3

,我怎么能点击我的按钮后立即刷新我的gridview。 我的按钮有更新码。有代码;刷新Gridview后按钮在我的asp.net项目上点击

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的数据”,但我无法理解的方法。 你能帮我一下吗?

非常感谢。

+0

是的,您可以使用数据绑定,因为它最简单的,我认为 – Pyromancer 2013-02-26 01:57:23

回答

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; 
     } 

那么如果你要刷新你把事件button_Click希望这种帮助loaddata();表,

+0

回报数据表后,我会写我的按钮,点击代码和继续? – 2013-02-26 02:09:18

+0

不,如果你想在更新时自动更新'dataGridView',你只需要在'button3_Click'的末尾加上'loadData();' – Pyromancer 2013-02-26 02:13:13

+0

为什么要使用'select * from yourTable'。这真的很糟糕。 – 2013-02-26 05:39:50

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

+1

这对于SQL更简单:)感谢提示 – Pyromancer 2013-02-26 02:10:15

1

加入购物车Tweet代码您的按键点击

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(); 
0

DataBind控制成功。

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