Click here to Skip to main content
14,392,108 members
Rate this:
Please Sign up or sign in to 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.
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.

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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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,
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.
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100