简体   繁体   English

从一个datagridview向datagridview c#Windows窗体添加多行

[英]Adding multiple row from one datagridview to datagridview c# windows form

i have this piece of code through which i am inserting one grid view data to another 我有这段代码,通过它我可以将一个网格视图数据插入到另一个

private void btnADD_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    DataRow dr;
    dt.Columns.Add("LOB");
    dt.Columns.Add("Quantity");
    dt.Columns.Add("Name");
    dt.Columns.Add("Packing");
    dt.Columns.Add("Price");
    dt.Columns.Add("Code");
    dr = dt.NewRow();
    dr["LOB"] = txtLOB.Text;
    dr["Quantity"] = txtQuantity.Text;
    dr["Name"] = txtName.Text;
    dr["Packing"] = txtPacking.Text;
    dr["Price"] = txtPrice.Text;
    dr["Code"] = txtBachNo.Text;
    dt.Rows.Add(dr);
    gridviewDtaInserted.DataSource = dt; 
}

i am able to insert one row at a time but i want to insert many rows one after another. 我能够一次插入一行,但我想一次又一次插入很多行。

在此处输入图片说明

您应该将DataTable声明为全局数据,因为每次单击Button时,它都会用新的关键字实例化。

Try this: 尝试这个:

DataTable dt = new DataTable();
dt.Columns.Add("LOB");
dt.Columns.Add("Quantity");
dt.Columns.Add("Name");
dt.Columns.Add("Packing");
dt.Columns.Add("Price");
dt.Columns.Add("Code");

private void btnADD_Click(object sender, EventArgs e)
{
    DataRow dr;

    for(int i = 0; i <= RowsCountThatYouWantToIsert; i++)
    {
        dr = dt.NewRow();
        dr["LOB"] = txtLOB.Text;
        dr["Quantity"] = txtQuantity.Text;
        dr["Name"] = txtName.Text;
        dr["Packing"] = txtPacking.Text;
        dr["Price"] = txtPrice.Text;
        dr["Code"] = txtBachNo.Text;
        dt.Rows.Add(dr);
    }

    gridviewDtaInserted.DataSource = dt; 
}

IF you are reading from one view to another you can use a looping structure to go through each iteration. 如果要从一个视图读取到另一个视图,则可以使用循环结构来遍历每个迭代。 I would suggest a for loop so that you can use the current numerical iteration as part of the instruction. 我建议使用for循环,以便您可以将当前的数值迭代用作指令的一部分。 if you want to ammend the first view to the second then you may want to use 如果您想将第一个视图修改为第二个视图,则可以使用

DataTable dt = new DataTable();
DataTable dt2 = new DataTable();
dt=(DataTable)DataGridViewer1.datasource;
dt2=(DataTable)DataGridViewer2.datasource;
dt2.Merge(dt);
DataGridViewer2.datasource=dt2;

The DataSource property, from a DataGridView accepts a collection of objects. 来自DataGridViewDataSource属性接受对象的集合。 So, I would suggest you to add how many rows you want to that public collection of objects, and at the end to update the DataSource gridviewDtaInserted.DataSource = myCollection; 因此,我建议您向该对象的公共集合中添加所需的行数,最后更新DataSource gridviewDtaInserted.DataSource = myCollection;

See here more info: MSDN Also, here is a nice question may help you. 请参阅此处更多信息: MSDN此外,这是一个很好的问题,可能会对您有所帮助。

As a skeleton you can design in this way: 作为骨架,您可以通过以下方式进行设计:

public class TestFunctional {
    public TestFunctional(){
        DataItems = new List<DataItem>();
    }

    public List<DataItem> DataItems { get; set; }

    private void AddOneItem(){
        var newItem = new DataItem {
            LOB = "a",
            Quantity = 1,
            Name = "A",
            Packing = true,
            Code = "a1"
        };

        DataItems.Add(newItem);

        RefreshGrid();
    }

    private void AddMultipleItems(){
        var newItem1 = new DataItem {
            LOB = "a",
            Quantity = 1,
            Name = "A",
            Packing = true,
            Code = "a1"
        };

        var newItem2 = new DataItem {
            LOB = "b",
            Quantity = 2,
            Name = "B",
            Packing = false,
            Code = "b2"
        };

        DataItems.Add(newItem1);
        DataItems.Add(newItem2);

        /*or use DataItems.AddRange( ... ) */

        RefreshGrid();
    }

    private void RefreshGrid()
    {
        gridviewDtaInserted.Rows.Clear();
        gridviewDtaInserted.Refresh();

        gridviewDtaInserted.DataSource = DataItems;
    }
}

public class DataItem{
    public string LOB { get; set; }
    public double Quantity { get; set; }
    public string Name { get; set; }
    public bool Packing { get; set; }
    public decimal Price { get; set; }
    public string Code { get; set; }    
}

I hope it will help you. 希望对您有帮助。 Otherwise, ask :) 否则,请问:)


Edit: 编辑:

Also, try to use the BindingList instead of List, I am not sure, but maybe it will automatically update the DataSource of the grid as soon an item is inserted in the collection. 另外,我不确定使用BindingList而不是List,但是不确定如何,只要将一个项目插入集合中,它就会自动更新网格的DataSource

BindingList<DataItem> DataItems

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

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