3

I'm trying to insert new records into the source table from the C# interface grid view.... But when I retrieve the records with the buttonclick code shown below... Im getting records in the gridview but no option for inserting new records(screen shot attached).. where as i can update the reocrds from the grid view.

Is there any option or property for enabling the insertion option in the gridview ?

Buttonclickcode :

private void RetrieveRules_button_Click(object sender, EventArgs e)
{
    this.dataGridView.DataSource = null;
    this.dataGridView.Rows.Clear();


    SqlCommand cmd1 = con.CreateCommand();
    cmd1.CommandType = CommandType.Text;
    cmd1.CommandText = @" Select TOP 1 * FROM " + schemaName + "[ERSBusinessLogic] ORDER BY ERSBusinessLogic_ID     DESC";
    con.Open();
    cmd1.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter DA = new SqlDataAdapter(cmd1);
    DA.Fill(dt);
    dataGridView.DataSource = dt;
    con.Close();

}

Screenshot of the gridview where there is no option for insertion Thanks

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Gowtham Ramamoorthy
  • 896
  • 4
  • 15
  • 36
  • I think, there's an option in the datagridview to allow insert/update/delete. Click the datagridview and you will see a triangle like option in the upper right of the control, click that and you will see an option to enable to the insert/update and delete. But you have to do some coding to implement that. Happy Coding. – Chris Mar 29 '16 at 02:36
  • You should set `AllowUserToAddRows = true` to let the user add rows. Also for delete `AllowUserToDeleteRows = true` and for edit `ReadOnly = false`. Also to be able to save changes, you should have valid insert, update and delete commands in your data adapter. – Reza Aghaei Mar 29 '16 at 03:28

2 Answers2

5

CRUD Operations using DataGridView, DataTable and DataAdapter

To let the user add, delete or edit rows with DataGridView:

  1. Set AllowUserToAddRows property to true or in DataGridView Tasks, check Enable Adding
  2. Set AllowUserToDeleteRows property to true or in DataGridView Tasks, check Enable Deleting
  3. Set ReadOnly property to false or in DataGridView Tasks, check Enable Editing

To let the user save changes using a SqlDataAdapter:

  1. Create a SqlDataAdapter using a select statement and a connection string.
  2. You should have valid InsertCommand, DeleteCommand and UpdateCommand for your data adapter. Create valid commands using a SqlCommandBuilder.
  3. Load data to a DataTable using data adapter.
  4. Set data table as DataSource of DataGridView
  5. Save changes when you need using SqlDataAdapter.Update by passing data table to the method.

Code

DataTable table;
SqlDataAdapter adapter;
private void Form1_Load(object sender, EventArgs e)
{
    //Create adapter
    var connection = @"your connection string";
    var command = "SELECT * FROM Table1";
    adapter = new SqlDataAdapter(command, connection);

    //Create Insert, Update and Delete commands
    var builder = new SqlCommandBuilder(adapter);

    //Load data
    table = new DataTable();
    adapter.Fill(table);

    //Bind the grid to data
    this.dataGridView1.DataSource = table;

    //Enable add, delete and edit
    this.dataGridView1.AllowUserToAddRows = true;
    this.dataGridView1.AllowUserToDeleteRows = true;
    this.dataGridView1.ReadOnly = false;
}

private void saveButton_Click(object sender, EventArgs e)
{
    //Save Data
    adapter.Update(table);
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    adapter.Dispose();
}

Note

  • You don't need that ExecuteNonQuery. You only need a connection string and a command text. Then you can create a data adapter. Then you even don't need to manage opening and closing the connection, data adapter manages it.
  • When you load data using SELECT TOP 1 *, if you add data and save, you can't see updates next time you load the data because you load only a single record.
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
3

Code is using ExecuteNonQuery, it returns no of rows affected not the results your query returns.

Use ExecuteReader instead.

var reader = cmd1.ExecuteReader();
DataTable dt = new DataTable();
dtLoad(reader);

Is there any option or property for enabling the insertion option in the gridview

dataGridView1.ReadOnly = false;
dataGridView1.EditMode = DataGridViewEditMode.EditOnF2; //if you want to edit on F2.
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • The problem is not related to `ExecuteNonQuery`. The OP is loading data using a `SqlDataAdapter`, that execution is not needed also has not any impact on the result. The problem is he didn't enabled adding, editing and deleting in grid. also to save data, he doesn't have valid insert, update, delete commands. See my [answer](http://stackoverflow.com/a/36274706/3110834). – Reza Aghaei Mar 29 '16 at 03:14