3

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

「グリッドビューのデータをバインドする必要があります」と言うかもしれませんが、その方法を理解できませんでした。それについて私を助けてくれませんか?

どうもありがとうございます。

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 に答える