Click here to Skip to main content
15,885,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i get this error,
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

and this is my code,please tell me solution,
thnx in advance.

private void dtgridcompanyList_CellContentClick(object sender, DataGridViewCellEventArgs e)
{


C#
int ColumnIndex = e.ColumnIndex;
            obj_companies.Company_Id = dtgridcompanyList.Rows[e.RowIndex].Cells[2].Value.ToString();
            obj_companies.CompanyName = dtgridcompanyList.Rows[e.RowIndex].Cells[3].Value.ToString();
            obj_companies.CompanyRegno = dtgridcompanyList.Rows[e.RowIndex].Cells[4].Value.ToString();
            obj_companies.CompanyUrl = dtgridcompanyList.Rows[e.RowIndex].Cells[5].Value.ToString();
            obj_companies.CompanyfaxNo = dtgridcompanyList.Rows[e.RowIndex].Cells[6].Value.ToString();
            obj_companies.BranchId = dtgridcompanyList.Rows[e.RowIndex].Cells[7].Value.ToString();
            obj_companies.Branchname = dtgridcompanyList.Rows[e.RowIndex].Cells[8].Value.ToString();
            obj_companies.Address = dtgridcompanyList.Rows[e.RowIndex].Cells[9].Value.ToString();
            obj_companies.country =Convert.ToInt32( dtgridcompanyList.Rows[e.RowIndex].Cells[10].Value.ToString());
            obj_companies.state = Convert.ToInt32(dtgridcompanyList.Rows[e.RowIndex].Cells[11].Value.ToString());
            obj_companies.city = Convert.ToInt32(dtgridcompanyList.Rows[e.RowIndex].Cells[12].Value.ToString());
            obj_companies.Emailid = dtgridcompanyList.Rows[e.RowIndex].Cells[13].Value.ToString();
            obj_companies.pincode = dtgridcompanyList.Rows[e.RowIndex].Cells[14].Value.ToString();
            obj_companies.contactnumber1 = dtgridcompanyList.Rows[e.RowIndex].Cells[15].Value.ToString();
            obj_companies.contactnumber2 = dtgridcompanyList.Rows[e.RowIndex].Cells[16].Value.ToString();

}
Posted
Comments
Orcun Iyigun 20-Feb-13 4:55am    
Have you debugged your code? Have you checked if you are getting the correct data or not? and why are you converting the value to string and then convert it to int32? Just do it directly?

1 solution

dude - simple debugging - are you using any sort of logging ? I would (for example)

1) if there's a rows count property, I'd log the value of (eg) dtgridcompanyList.Rows.Count()
2) log and compare e.RowIndex to dtgridcompanyList.Rows.Count()

Similarly for each e.RowIndex you need to check the Cells.Count() value and for example in the case you've shown, Cells[16] see if that is greater than the number of cells

why dont you set a breakpoint at [int ColumnIndex = e.ColumnIndex;] and single step through the code until it blows up - that line shows you where your indexes are going out of whack - you then have to figure out why ...
 
Share this answer
 
Comments
Ajinkya Ban 20-Feb-13 5:11am    
thanx,
the problem solved

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