1

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

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

That would work however you can allow the user to manually insert rows into the datagridview which would not be reflected into the globale datatable. It is better to get the data from the datagridview itself.
0

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

Comments

0

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;

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.