无法更新datagridview中的信息

时间:2017-06-13 15:29:37

标签: c# sql .net ms-access datagridview

Screenshot

这是数据网格视图,它连接到访问数据库。 有一个名为" Status"的列。 (你可以在红色方块看到它)。 我想通过按下" Check in按钮"更新其中的文本(到达)到文本(内部)。选定行 。我怎样才能做到这一点? 提前谢谢。

 private void searchbd_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab == tabControl1.TabPages["tabPage1"])
            {
                try
                {

                    string dtparrival = this.dateTimePicker4.Text.ToString();
                    string statusarrival = "Arrival";

                    string strSql = "SELECT * FROM guestreg WHERE g_status = '" + statusarrival + "' AND g_ad = '" + dtparrival + "'";


                    OleDbCommand cmd = new OleDbCommand(strSql, connection);
                    connection.Open();
                    cmd.CommandType = CommandType.Text;
                    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                    DataTable scores = new DataTable();
                    da.Fill(scores);
                    arrivaldgv.DataSource = scores;
                    /**************************************************************************/
                    this.arrivaldgv.Columns[0].Visible = false;
                    this.arrivaldgv.Columns["g_totalrate"].Visible = false;
                    this.arrivaldgv.Columns["u_added"].Visible = false;
                    this.arrivaldgv.Columns["u_timeadded"].Visible = false;
                    /**************************************************************************/
                    this.arrivaldgv.Columns["g_name"].HeaderText = "Name";  //1
                    this.arrivaldgv.Columns["g_surname"].HeaderText = "Surname"; //2
                    this.arrivaldgv.Columns["g_company"].HeaderText = "Company"; //3
                    this.arrivaldgv.Columns["g_ad"].HeaderText = "Arrival Day"; //4
                    this.arrivaldgv.Columns["g_dd"].HeaderText = "Departure Day"; //5
                    this.arrivaldgv.Columns["g_amountofdays"].HeaderText = "Amount of Days"; //6
                    this.arrivaldgv.Columns["g_na"].HeaderText = "Number of Adults"; //7
                    this.arrivaldgv.Columns["g_amountofdays"].HeaderText = "Amount of Days"; //8
                    this.arrivaldgv.Columns["g_room"].HeaderText = "Room Number"; //9
                    this.arrivaldgv.Columns["g_rate"].HeaderText = "Rate"; //10
                    this.arrivaldgv.Columns["g_totalrate"].HeaderText = "Total Rate"; //11
                    this.arrivaldgv.Columns["g_info"].HeaderText = "Information"; //12
                    this.arrivaldgv.Columns["u_added"].HeaderText = "User"; //13
                    this.arrivaldgv.Columns["u_timeadded"].HeaderText = "Time"; //14
                    this.arrivaldgv.Columns["g_status"].HeaderText = "Status"; //15
                    /**************************************************************************/
                    connection.Close();
                }
                catch
                {
                    MessageBox.Show("Please, try again.");
                }

1 个答案:

答案 0 :(得分:0)

我想,你只想更新dataGridView单元格中的值,而不是数据库表中的值。

最简单的方法是:

    private void CheckInButton_Click(object sender, EventArgs e)
    {
        int row = dataGridView1.CurrentCell.RowIndex;
        int col = dataGridView1.CurrentCell.ColumnIndex;

        dataGridView1[col, row].Value = "In House";
    }

您当然可以添加一些验证。例如:检查是否选择了任何单元格。