Hi all,

i want to add data manully to a dataGridView.but i get this exception.

ArgumentOutOfRangeException.

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

PLZ help me to solve this.
this is my code.

int clicks=0;
private void button4_Click(object sender, EventArgs e)
        {
            
            dataGridView1.Rows[clicks].Cells["Author_Number"].Value = "Author "+(clicks+1).ToString();
            dataGridView1.Rows[clicks].Cells["First_name"].Value = "fname";
            dataGridView1.Rows[clicks].Cells["Last_name"].Value = "lname";
            clicks++;
        }

You have to add rows as well (every loop):

int clicks = 0;
        private void button4_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add();
            dataGridView1.Rows[clicks].Cells["Author_Number"].Value = "Author " + (clicks + 1).ToString();
            dataGridView1.Rows[clicks].Cells["First_name"].Value = "fname";
            dataGridView1.Rows[clicks].Cells["Last_name"].Value = "lname";
            clicks++;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.