Ali Raza
Ali Raza

Reputation: 1241

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.

enter image description here

Upvotes: 1

Views: 7550

Answers (4)

mihai
mihai

Reputation: 2804

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

Upvotes: 0

Shon
Shon

Reputation: 486

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;

Upvotes: 0

SᴇM
SᴇM

Reputation: 7213

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

Upvotes: 0

Muhammad Saqib
Muhammad Saqib

Reputation: 41

You should declare DataTable as globally because every time on Button click it has instantiated with new key word.

Upvotes: 1

Related Questions