无法编辑dataGridView中的列值

时间:2017-01-21 15:50:54

标签: c# sql visual-studio datagridview

我无法更新dataGridView中列的值。请帮忙

          private void button1_Click(object sender, EventArgs e)
         {

           {
            SqlDataAdapter adapter = new SqlDataAdapter();
            itemRecordBindingSource.EndEdit();
            DataTable dt = new DataTable();
            adapter.Update(dt);    
            MessageBox.Show("Record Saved.");
        }

        catch(Exception c)
        {
           MessageBox.Show("Sorry, could not update data." + c);
        }

    }

它没有显示任何错误,也没有更新表。

1 个答案:

答案 0 :(得分:0)

为了使您的数据网格可编辑,并且在编辑网格中的列后,您希望将其保存到数据库,请使用此网格视图事件。

 I am assuming you are able to fill GridView with data if not first 
 get the data from sql query and set the datasource of grifview to that table . once you have data then you can use this event .


        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {

            try
            {
            -- here you can get the column index and column value 
            now based on the index and value you can witre you sql query / or a function which will update the database . 
            string value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }