【问题标题】:How to add a new data to a datagrid keeping the previously existed data there too?如何将新数据添加到数据网格中,同时保留以前存在的数据?
【发布时间】:2019-11-09 19:06:24
【问题描述】:

我正在从 SQL 中选择数据到数据网格中,代码工作正常,但它正在从我的数据网格中删除以前的数据。

我的代码(C# WPF SQL 数据网格):

  private void enter(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    SqlConnection com = new SqlConnection("Server = localhost; database = PrimaSOFT ; integrated security = true");

                    SqlCommand cmd = new SqlCommand("produktit", com);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Barkodi", txtBarkodi.Text);
                    //Created a new DataTable

                    SqlDataAdapter da = new SqlDataAdapter(cmd);



                    DataColumn dc = new DataColumn();//Made a new DataColumn to populate above DataTable
                    dc.DataType = System.Type.GetType("System.String");//Defined the DataType inside, this can be [[int]] if you want.
                    dc.ColumnName = "Barkodi";//Gave it a name (important for the custom expression - can only be one word so use underscores if you need multiple words)

                    DataColumn dc2 = new DataColumn();
                    dc2.DataType = System.Type.GetType("System.String");
                    dc2.ColumnName = "Emertimi";

                    DataColumn dc3 = new DataColumn();
                    dc3.DataType = System.Type.GetType("System.Decimal");
                    dc3.ColumnName = "Sasia";

                    DataColumn dc4 = new DataColumn();
                    dc4.DataType = System.Type.GetType("System.Decimal");
                    dc4.ColumnName = "Cmimi";

                    DataColumn dc5 = new DataColumn();
                    dc5.DataType = System.Type.GetType("System.String");
                    dc5.Caption = "sds";

                    dc5.ColumnName = "TVSH";

                    DataColumn dc6 = new DataColumn();
                    dc6.DataType = System.Type.GetType("System.String");
                    dc6.ColumnName = "Total";
                    dc6.Expression = "Cmimi * Sasia";//Multiplying the Price and Quantity DataColumns

                    dataTable.Columns.Add(dc);//Add them to the DataTable
                    dataTable.Columns.Add(dc2);
                    dataTable.Columns.Add(dc3);
                    dataTable.Columns.Add(dc4);
                    dataTable.Columns.Add(dc5);
                    dataTable.Columns.Add(dc6);

                    dtgartikujt.ItemsSource = dataTable.DefaultView;//Set the DataGrid ItemSource to this new generated DataTable

                    com.Open();//Open the SQL connection

                    SqlDataReader reader = cmd.ExecuteReader();//Create a SqlDataReader

                    while (reader.Read())//For each row that the SQL query returns do
                    {
                        DataRow dr = dataTable.NewRow();//Create new DataRow to populate the DataTable (which is currently binded to the DataGrid)
                        dr[0] = reader[0];//Fill DataTable column 0 current row (Product) with reader[0] (Product from sql)
                        dr[1] = reader[1];
                        dr[2] = reader[2];
                        dr[3] = reader[3];
                        dr[4] = reader[4];


                        dataTable.Rows.Add(dr);//Add the new created DataRow to the DataTable

                    }
                }
            }

求和码

                    object sumObject;
                    sumObject = dataTable.Compute("Sum(Totali)", 
                     string.Empty);
                    txttotali.Text = sumObject.ToString();

我希望在不删除以前数据的情况下在数据网格中显示新数据。 我应该如何处理?

已编辑:我还附上了求和代码,对数据网格列求和,并显示到文本框

【问题讨论】:

    标签: c# sql datagrid


    【解决方案1】:

    我想我理解你在做什么:将 dataTable 代码与 Sql(ADO.NET) 代码分开,并且只运行一次 dataTable 部分。因此,例如在form_load 中运行CreateTable

    public void CreateTable()
    {
     DataColumn dc = new DataColumn();//Made a new DataColumn to populate above DataTable
                        dc.DataType = System.Type.GetType("System.String");//Defined the DataType inside, this can be [[int]] if you want.
                        dc.ColumnName = "Barkodi";//Gave it a name (important for the custom expression - can only be one word so use underscores if you need multiple words)
    
                        DataColumn dc2 = new DataColumn();
                        dc2.DataType = System.Type.GetType("System.String");
                        dc2.ColumnName = "Emertimi";
    
                        DataColumn dc3 = new DataColumn();
                        dc3.DataType = System.Type.GetType("System.Decimal");
                        dc3.ColumnName = "Sasia";
    
                        DataColumn dc4 = new DataColumn();
                        dc4.DataType = System.Type.GetType("System.Decimal");
                        dc4.ColumnName = "Cmimi";
    
                        DataColumn dc5 = new DataColumn();
                        dc5.DataType = System.Type.GetType("System.String");
                        dc5.Caption = "sds";
    
                        dc5.ColumnName = "TVSH";
    
                        DataColumn dc6 = new DataColumn();
                        dc6.DataType = System.Type.GetType("System.String");
                        dc6.ColumnName = "Total";
                        dc6.Expression = "Cmimi * Sasia";//Multiplying the Price and Quantity DataColumns
    
                        dataTable.Columns.Add(dc);//Add them to the DataTable
                        dataTable.Columns.Add(dc2);
                        dataTable.Columns.Add(dc3);
                        dataTable.Columns.Add(dc4);
                        dataTable.Columns.Add(dc5);
                        dataTable.Columns.Add(dc6);
    
                        dtgartikujt.ItemsSource = dataTable.DefaultView;//Set the DataGrid ItemSource to this new generated DataTable
    }
    

    以及其余代码:

         long sum=0;
         private void enter(object sender, KeyEventArgs e)
                {
                    try
                    {
                        if (e.Key == Key.Enter)
                        {
                            SqlConnection com = new SqlConnection("Server = localhost; database = PrimaSOFT ; integrated security = true");
    
                            SqlCommand cmd = new SqlCommand("produktit", com);
                            cmd.CommandType = System.Data.CommandType.StoredProcedure;
                            cmd.Parameters.AddWithValue("@Barkodi", txtBarkodi.Text);
                            //Created a new DataTable
    
                            SqlDataAdapter da = new SqlDataAdapter(cmd);
    
                            com.Open();//Open the SQL connection
    
                            SqlDataReader reader = cmd.ExecuteReader();//Create a SqlDataReader
    
                            while (reader.Read())//For each row that the SQL query returns do
                            {
                                DataRow dr = dataTable.NewRow();//Create new DataRow to populate the DataTable (which is currently binded to the DataGrid)
                                dr[0] = reader[0];//Fill DataTable column 0 current row (Product) with reader[0] (Product from sql)
                                dr[1] = reader[1];
                                dr[2] = reader[2];
                                dr[3] = reader[3];
                                dr[4] = reader[4];
                                // sum+= Convert.ToInt64(reader[4]); // use the total index instead of 4 . or use reader["total"];
    
                                dataTable.Rows.Add(dr);//Add the new created DataRow to the DataTable
        // or maybe dtgartikujt.Rows.Add(dr);
                            }
    
    // textbox1.Text=sum.Tostring();
                        }
                    }
    

    【讨论】:

    • 如何对“总计”列求和并显示到文本框
    • 这是另一个问题,但我添加了答案但发表了评论。你的问题太简单了。请多学习!
    • 您@nAvid 还需要学习更多,因为这不是正确的答案(意味着您显示的汇总数据网格的代码错误)。我想出了怎么做,这就是方法(你可以在我的代码中看到,我编辑了它)
    • 下一个问题见!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    相关资源
    最近更新 更多