Joshua Van Hoesen
Joshua Van Hoesen

Reputation: 1732

Binding BindingList<string[]> to datagridview

Situation:

I am attempting to bind a BindingList<string[]> constructed from a LINQ to SQL query to a DataGridView.

Problem:

I either cannot make modification to the DataGridView after items are generated -or- I get a bunch of unwanted fields in my DataGridView (it depends on which iteration of my code I use) I have googled as hard as I can and tried implementing most of the solutions I have found online to no avail.

I know that string has no public property for its actual value. I am having a difficult time determining how to retrieve that (I believe is part of the problem).

C#

int item = (from p in CurrentConversion.Companies[lbCompanies.SelectedIndex].Modules 
    where p.ModuleName.Equals(clbModules.SelectedItem) 
    select p.ModuleId)
    .FirstOrDefault();

BindingList<string[]> Data = new BindingList<string[]>((
    from p in CurrentConversion.Companies[lbCompanies.SelectedIndex].QuestionAnswers
    where p[2].Equals(item)
    select new string[] { p[0].ToString(), p[3].ToString() })
    .ToList());

dgvQuestions.DataSource = Data;
dgvQuestions.Refresh();

Unwanted Behavior:

This occurs after binding

datagridview

Question:

  1. Why is this happening?
  2. How do I fix it?

Additional Information:

I am not sure what additional information may be need but I will supply what is requested.

Also if I switch to my other code iteration:

int item = (from p in CurrentConversion.Companies[lbCompanies.SelectedIndex].Modules where p.ModuleName.Equals(clbModules.SelectedItem) select p.ModuleId).FirstOrDefault();
            var Data = new BindingList<object>((from p in CurrentConversion.Companies[lbCompanies.SelectedIndex].QuestionAnswers where p[2].Equals(item) select new {Question = p[0].ToString(), Answer = p[3].ToString() }).Cast<object>().ToList());
            dgvQuestions.DataSource = Data;
            dgvQuestions.Refresh();
            dgvQuestions.Columns[1].ReadOnly = false;

I can see the data properly but I cannot edit the column I would like to.

Upvotes: 0

Views: 1009

Answers (2)

Brian S
Brian S

Reputation: 5785

The reason you're seeing those fields in the Grid is that you're binding each row to a string[]. So it is automatically displaying the properties of string[] as the columns. There is no built-in logic for the grid to parse an array and use the contents of the array as columns.

In order to get the DataGrid to display your data correctly, you should bind it to a custom type, and it will use the public properties of the type as columns.

Upvotes: 0

Magnus
Magnus

Reputation: 46909

You are binding to a list of string arrays, and you are getting the properties form the array. Most likely you want something like the following:

var Data = new BindingList<object>((
    from p in CurrentConversion.Companies[lbCompanies.SelectedIndex].QuestionAnswers 
    where p[2].Equals(item) 
    select new {
                Val1 = p[0].ToString(), 
                Val2 = p[3].ToString() 
               }).ToList());

Upvotes: 1

Related Questions