简体   繁体   中英

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. 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. 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;

See here more info: MSDN Also, here is a nice question may help you.

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<DataItem> DataItems

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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