Click here to Skip to main content
14,975,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am making program in which the data in textbox should be shown in data grid view.when a user click ok button buyitem is incremented and the data is passed to datagridview but instead of showing result it gives error.

SQL
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

C#
 public Form1()
        {
            InitializeComponent();
        }
        int buyItem = 0;
        connection con = new connection();

  private void btnOk_Click(object sender, EventArgs e)
        {


            dataGridView1.Rows[buyItem].Cells[0].Value = buyItem + 1;
            dataGridView1.Rows[buyItem].Cells[1].Value = txtItemno.Text;
            dataGridView1.Rows[buyItem].Cells[2].Value = cbxItem.Text;
            dataGridView1.Rows[buyItem].Cells[3].Value = cbxBrand.Text;
            dataGridView1.Rows[buyItem].Cells[4].Value = txtQty.Text;
            dataGridView1.Rows[buyItem].Cells[5].Value = txtPrice.Text;
            dataGridView1.Rows[buyItem].Cells[6].Value = txtPoint.Text;
            dataGridView1.Rows[buyItem].Cells[7].Value =txtLineTotal.Text;

            buyItem++;
}
Posted
Comments
BillWoodruff 26-Oct-14 5:31am
   
put a break-point(s) in your code, single-step through it (F11), and show us where the error occurs.

We can't solve that for you - it depends on a load of information we don;t have any access to: your DataGridView, your data, etc.

But you can: run your code in the debugger and look closely at the line it stops on.
Look at the variables: what is the value of buyItem? How many Rows does the DGV have? It it has sufficient rows, how many Cells does each Row have?

Most likely, the DVG doesn't have any rows, in which case you need to create a new DateGridViewRow and add it to the DGV...but only you can tell, we can't access your running code...
   
First of all you should insert the information in your database.After it you must read all of your information from DB(by Store Procedure and get help from List).Finally fill your gridview with selected items.(you should use For to fill all rows of gridview)
   
i have debugged it and it give the above mention error on
this line.
dataGridView1.Rows[buyItem].Cells[0].Value = buyItem + 1;
columns numbers are same.
   
Comments
[no name] 26-Oct-14 9:10am
   
I suggest to delete this answer and write this as a comment to avoid to become downvoted more. Regards, Bruno

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900