0

I am trying add a new row into datagridview with the button click for learning purposes. When I click the button, it deletes the old data and add the new row. But I want to append it to the grid

here is my form_load function which gets the data from the database and fill the grid.

private void Form1_Load(object sender, EventArgs e)
    {
        SqlConnection connection = new SqlConnection("Data Source=ARIF-BILGISAYAR;Initial Catalog=RotanetLocal;Persist Security Info=True;User ID=sa;Password=12345");
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM TestTable", connection);
        DataSet sourceDataSet = new DataSet();
        adapter.Fill(sourceDataSet);
        gridControl1.DataSource = sourceDataSet.Tables[0];

    }

and here is my button event which is supposed to append a row into the grid, but it deletes the old data and inserts the new one

private void btn_update_Click(object sender, EventArgs e)
    {
        DataTable dtOperation = new DataTable();

        var myOriginalDataSource = (DataTable)gridControl1.DataSource;
        var dr = myOriginalDataSource.NewRow();
        dr["id"] = 1;
        dr["name"] = "Gelen Havale";
        dr["lname"] = "Gelen Havale";
        dtOperation.Rows.Add(dr);

        gridControl1.Refresh();
    }

my btn_update_click function is now trying to create a row and append it to the old data. but it now crashes. It says taht this row belongs to another table. how can I fix that?

Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189

3 Answers3

1

It looks like you are completely replacing the DataSource property which will tell the control to completely re-render.

Add the new row to the existing DataSource and then call the Refresh() method.

On the other-hand if you are using WPF move away from using DataTable and start using an ObservableCollection<T> instead.

    private void btn_update_Click(object sender, EventArgs e)
    {
        var myOriginalDataSource = gridControl1.DataSource;
        var dr = myOriginalDataSource.NewRow();

        dr["id"] = 1;
        dr["name"] = "Gelen Havale";
        dr["lname"] = "Gelen Havale";

        gridControl1.Refresh();
    }

The Refresh() is psudocode as I don't know the DevExpress WinForms controls very well.

Jammer
  • 9,969
  • 11
  • 68
  • 115
  • how do you append into existing datasource? – Arif YILMAZ May 03 '13 at 08:05
  • Create a new `DataRow` object and populate it by calling the `NewRow()` method on the **ORIGINAL** DataSource object `sourceDataSet`. Then call the `Refresh()` method on the `gridControl1` instance. – Jammer May 03 '13 at 08:14
  • Are you looking on the right object? It's a method on `System.Data.DataTable` class. You can either cache a reference to the original DataSource in a private field at the class level or grab it from your control as per the example I provided `gridControl1.DataSource` – Jammer May 03 '13 at 08:32
  • It's still VERY wrong. You are resolving the myOriginalDataSource yet still adding the new row to dtOperation. I'll add another answer. – Jammer May 03 '13 at 08:50
  • There is no need for a gridcontrol.Refresh() or a gridControl1.RefreshDataSource(), the gridcontrol will detect changes from most sources. And Jammers code will work.... – Stig May 07 '13 at 06:37
1

You need to change your code to this:

private void btn_update_Click(object sender, EventArgs e)
{
    var myOriginalDataSource = (DataTable)gridControl1.DataSource;
    var dr = myOriginalDataSource.NewRow();

    dr["id"] = 1;
    dr["name"] = "Gelen Havale";
    dr["lname"] = "Gelen Havale";

    gridControl1.Refresh();
}

I think it would be worth spending some time reading up on Object Orientation Principles

You were still creating a new DataTable needlessly and adding the row to that instance rather than the DataTable already data-bound to your grid control instance.

Jammer
  • 9,969
  • 11
  • 68
  • 115
  • OK, does the DevExpress control have a `BindData()` or similar method? I'm really not familiar with the controls you are using so you may need to get support from DevExpress directly on that. If you debug this method and but a breakpoint on the `Refresh()` line have a look at the contents of `myOriginalDataSource` by clicking the magnifying glass icon, this will open a `DataTable` viewer and you can confirm that the new row has been added. If it has been added correctly start looking at the `DataBind()` method on the grid control. – Jammer May 03 '13 at 09:10
0

you have created a new DataTable and bind with it thats why..

mastermind
  • 1,057
  • 1
  • 8
  • 15