【问题标题】:how to append new row to the datagridview and keep existing rows?如何将新行附加到 datagridview 并保留现有行?
【发布时间】:2018-02-04 16:45:21
【问题描述】:

我有实验室请求窗口,我可以稍后创建新请求 我需要更新此订单 我创建了更新窗口,我从 datagrid 视图中的数据库中读取了订单详细信息,我需要添加新项目datagridview,但是当我添加新行时出现错误,现有行被删除并添加新行我的代码有什么错误,我正在添加新行。 如何添加新行并保留现有行,还要检查附件 datagridview 图像,然后按 enter 键和 datagridview1 图像,然后按输入其添加新行并删除从数据库中获取的现有行。

private void textAmount_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && textQty.Text != string.Empty && textAmount.Text != string.Empty)
            {
                if (chkcash1.Checked == true)
                {
                    textDiscount.Focus();
                }
                if (chkcash1.Checked == false)
                {
                    for (int i = 0; i < TestsDataGrid.Rows.Count - 1; i++)
                    {
                        if (TestsDataGrid.Rows[i].Cells[0].Value.ToString() == textTestId.Text)
                        {
                            MessageBox.Show("This test added already ", "Duplicate Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }
                    }
                    DataRow r = dt.NewRow();
                    r[0] = textTestId.Text;
                    r[1] = textName.Text;
                    r[2] = textPrice.Text;
                    r[3] = textQty.Text;
                    r[4] = textAmount.Text;
                    r[5] = textTotal.Text;


                    dt.Rows.Add(r);
                    TestsDataGrid.DataSource = dt;
                    textTestId.Clear();
                    textName.Clear();
                    textPrice.Clear();
                    textAmount.Clear();
                    textTotal.Clear();
                    btnSearch.Focus();

                    textOrderTotal.Text = (from DataGridViewRow row in TestsDataGrid.Rows
                                           where row.Cells[4].FormattedValue.ToString() != string.Empty
                                           select Convert.ToDouble(row.Cells[4].FormattedValue)).Sum().ToString();
                }[datagridviewimage][1]

【问题讨论】:

  • 标点是你的朋友。
  • 是的,我没有超过第三行
  • 我什至用文字来编辑它,但我仍然感到困惑。请远离咖啡!
  • 在添加新行之前 dt 有多少行?并且 dt 是您用作数据网格的数据源的源?
  • @Saruman 对不起我的英语不好我英语不流利我会尝试订购问题

标签: c# datagridview


【解决方案1】:

这可能会有所帮助,这里有一个简单的例子:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.Data.OleDb;

namespace Practice
{
    public partial class Form1 : Form
    {
        private DataTable retdt;
        SqlCeDataAdapter da;
        SqlCeCommandBuilder cbuild;
        SqlCeConnection con;
        public Form1()
        {
            InitializeComponent();
            fetchData();
        }
        private void fetchData()
        {
            retdt = new DataTable();
            SqlCeConnection con = null;
            try
            {
                con = new SqlCeConnection(@"Data Source=c:\users\mswami\documents\visual studio 2010\Projects\Practice\Practice\practice.sdf");
                da = new SqlCeDataAdapter();
                da.SelectCommand = new SqlCeCommand("select uid,amount from TestTable", con);
                cbuild = new SqlCeCommandBuilder(da);
                con.Open();
                da.Fill(retdt);
                DGPractice.DataSource = retdt.DefaultView;
                DGPractice.BindingContext = this.BindingContext;
            }
            catch (Exception exce)
            {
                MessageBox.Show(exce.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (maskedTextBox1.Text.Length > 0)
            {
                if (retdt.Select("amount=" + maskedTextBox1.Text).Length == 0)
                {
                    DataRow drow = retdt.NewRow();
                    // drow[0] left blank as it is primary key column with auto increment value
                    drow[1] = Math.Round(Double.Parse(maskedTextBox1.Text),2);
                    retdt.Rows.Add(drow);

                    //Update changes into database table
                    // First process deletes.  
                    da.Update(retdt.Select(null, null, DataViewRowState.Deleted));

                    // Next process updates.  
                    da.Update(retdt.Select(null, null,
                      DataViewRowState.ModifiedCurrent));

                    // Finally, process inserts.  
                    da.Update(retdt.Select(null, null, DataViewRowState.Added));

                    DGPractice.DataSource = null;
                    DGPractice.DataSource = retdt.DefaultView;
                    DGPractice.BindingContext = this.BindingContext;
                }
                else
                {
                    MessageBox.Show("Duplicate amount");
                }
            }
        }
    }

}

designer.cs 代码(用于 DataGrid 列定义和绑定属性)

 // DGPractice
// 
this.DGPractice.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.DGPractice.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.UID,
this.amtField});
this.DGPractice.Location = new System.Drawing.Point(2, 63);
this.DGPractice.Name = "DGPractice";
this.DGPractice.RowHeadersVisible = false;
this.DGPractice.Size = new System.Drawing.Size(373, 196);
this.DGPractice.TabIndex = 0;
// 
// UID
// 
this.UID.DataPropertyName = "uid";
this.UID.HeaderText = "UID";
this.UID.Name = "UID";
// 
// amtField
// 
this.amtField.DataPropertyName = "amount";
this.amtField.HeaderText = "Amount";
this.amtField.Name = "amtField";

DataPropertyName 应与选择查询中的字段名称(表字段名称)相同。

【讨论】:

    【解决方案2】:

    谢谢你,Maddy 它解决了我的错误,它没有将数据保留在数据网格视图中,因为我从存储过程中读取并且在获取到数据表后没有保存数据

    da.Fill(retdt);
    DGPractice.DataSource = retdt.DefaultView;
    DGPractice.BindingContext = this.BindingContext;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 2014-11-20
      相关资源
      最近更新 更多