简体   繁体   English

将新增加到gridcontrol devexpress

[英]append new to gridcontrol devexpress

I am trying add a new row into datagridview with the button click for learning purposes. 我正在尝试将新行添加到datagridview中,单击按钮进行学习。 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. 这是我的form_load函数,它从数据库中获取数据并填充网格。

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. 我的btn_update_click函数现在正在尝试创建一行并将其附加到旧数据。 but it now crashes. 但它现在崩溃了。 It says taht this row belongs to another table. 它说这行属于另一个表。 how can I fix that? 我该如何解决这个问题?

It looks like you are completely replacing the DataSource property which will tell the control to completely re-render. 看起来您正在完全替换DataSource属性,该属性将告诉控件完全重新呈现。

Add the new row to the existing DataSource and then call the Refresh() method. 将新行添加到现有DataSource,然后调用Refresh()方法。

On the other-hand if you are using WPF move away from using DataTable and start using an ObservableCollection<T> instead. 另一方面,如果您正在使用WPF,请不要使用DataTable并开始使用ObservableCollection<T>

    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. Refresh()是psudocode,因为我不太了解DevExpress WinForms控件。

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. 您仍在不必要地创建一个新的DataTable并将该行添加到该实例,而不是已经数据绑定到您的网格控件实例的DataTable

你已经创建了一个新的DataTable并与它绑定,这就是为什么..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM