Click here to Skip to main content
15,895,803 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,
I am working on an existing form which populates a datagridview as follows:
It is using a bindingsource because I see at the bottom of the form in designer the BindingSource control.
C#
private void PopulateGrid()
        {
            System.Data.DataTable dt = TFNGet();
            fBS.DataSource = dt;
            dGV.AutoGenerateColumns = false;
            dGV.DataSource = fBS;
            int i = 0;
            dGV.Columns[i++].DataPropertyName = "BCode";
            dGV.Columns[i++].DataPropertyName = "FDCode";
            dGV.Columns[i++].DataPropertyName = "Currency";
            dGV.Columns[i++].DataPropertyName = "CCcy";
            dGV.Columns[i++].DataPropertyName = "VD";
            dGV.Columns[i++].DataPropertyName = "BS";
            dGV.Columns[i++].DataPropertyName = "A";
            dGV.Columns[i++].DataPropertyName = "PCN";
            dGV.Refresh();
        }


Now I would like to add a checkbox column as the first column to this dGV called "Failed"
So I am doing the following to do this but the error is:
"Index was out of range. Must be non-negative and less than the size of the collection.

C#
private void PopulateGrid()
        {
            System.Data.DataTable dt = TFNGet();

		dt.Columns.Add("Failed", typeof(Boolean));

            fBS.DataSource = dt;
            dGV.AutoGenerateColumns = false;
            dGV.DataSource = fBS;
            int i = 0;
            dGV.Columns[i++].DataPropertyName = "BCode";
            dGV.Columns[i++].DataPropertyName = "FDCode";
            dGV.Columns[i++].DataPropertyName = "Currency";
            dGV.Columns[i++].DataPropertyName = "CCcy";
            dGV.Columns[i++].DataPropertyName = "VD";
            dGV.Columns[i++].DataPropertyName = "BS";
            dGV.Columns[i++].DataPropertyName = "A";
            dGV.Columns[i++].DataPropertyName = "PCN";

	//checkbox column
            dGV.Columns[i++].DataPropertyName = "Failed";

            dGV.Refresh();
        }


Any thoughts please?
Thanks
Posted

1 solution

Where are the other columns being defined?

It is likely you are setting the DataPropertyName on a column that does not exist. Which one is actually causing the exception?
The one of dGV.Columns[i++].DataPropertyNane = "Failed" or is it maybe dGV.Columns[i++].DataPropertyNane = "FDCode"

I suggest the second because I am only seeing one column added,
C#
dt.Columns.Add("Failed", typeof(Boolean));


So it seems you are setting this column to be "BCode" and then trying to set columns that do not exist.
 
Share this answer
 
Comments
arkiboys 18-Oct-11 12:26pm    
Solved as I had to add columns manually to binding source. Thank you
[no name] 18-Oct-11 12:44pm    
You are welcome :)
[no name] 18-Oct-11 17:21pm    
Also, you can click the "Accept Answer" which will mark it as answered.
Thank you

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