如何在没有数据库连接的情况下在datagrid中插入多行? [英] How to insert multiple rows in datagrid without db connection?

查看:61
本文介绍了如何在没有数据库连接的情况下在datagrid中插入多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据插入到datagrid视图中。但想到的是当我试图插入第二行时,它会替换datagrid单元格中的现有行。任何人都可以帮我在没有数据库连接的数据网格视图单元格中显示多行吗?



我尝试过:



Hi, I tried to insert data into datagrid view. But the think is when i tried to insert second row, it replace the existing row in datagrid cell. Can anyone help me to display multiple rows in datagrid view cell without database connection?

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web;

namespace task2
{
    public partial class Form1 : Form
    {
        private DataTable myTable = new DataTable();

        public Form1()
        {
            InitializeComponent();
            this.initialiseTable(this.myTable);
            this.dataGridView1.DataSource = this.myTable;
        }
        private void CLEAR_Click(object sender, EventArgs e)
        {
            Name.Text = "";
            LastName.Text = "";
            lsGender.Text = "";
            phone.Text = "";
            mail.Text = "";
        }
        private void CLOSE_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void initialiseTable(DataTable table)
        {
            table.Columns.Add(new DataColumn("Column1"));
            table.Columns.Add(new DataColumn("Column2"));
            table.Columns.Add(new DataColumn("Column3"));
        }


        private void ADD_Click(object sender, EventArgs e)
        {
            
           
            DataRow rd = this.myTable.NewRow();
            rd[0] = Name.Text;
            rd[1] = LastName.Text;
            rd[2] = lsGender.SelectedItem;
            this.myTable.Rows.Add(rd);
        }          
    }
}

推荐答案

奇怪:我将你的代码复制到一个应用程序中,添加了一个DGV和一个按钮,它工作正常。

我怀疑你正在关闭应用程序并重新打开它,或者使用一些你没有在那里显示的代码。

当然,你展示的代码在这里工作正常。
Strange: I copied your code into an app, added a DGV and a button, and it worked fine.
I'd suspect that you are closing the app and re-opening it, or using some code you don't show there.
Certainly, the code you show works fine here.


试试这个



try this

private void ADD_Click(object sender, EventArgs e)
       {

           DataRow rd = this.myTable.NewRow();
           rd[0] = Name.Text;
           rd[1] = LastName.Text;
           rd[2] = lsGender.SelectedItem;
           this.myTable.Rows.Add(rd);
           dataGridView1.DataSource = this.myTable; // add this line to bind the updated datatable to griview soruce

       }





数据将仅在应用程序的生命周期之前出现(应用程序打开并运行)..当它关闭时,数据表将重置

如果您不想使用数据库并且还要维护值,您应使用 XML



The data will be present only till the life time of the application ( app opened and running ).. when it is closed the datatable will reset.
If you dont want to use DB and also to maintain the values, you shall use XML


这篇关于如何在没有数据库连接的情况下在datagrid中插入多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆