简体   繁体   English

绑定列表 <string[]> 到datagridview

[英]Binding BindingList<string[]> to datagridview

Situation: 情况:

I am attempting to bind a BindingList<string[]> constructed from a LINQ to SQL query to a DataGridView . 我试图将LINQ构造的BindingList<string[]>绑定到SQL查询到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. 我既不能进行修改到DataGridView后产生的项目-或者我在弄了一堆不需要的字段的DataGridView (这取决于我使用的我的代码迭代)我用Google搜索硬如我可以和试图实施的最我在网上找到的解决方案无济于事。

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# 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 绑定后发生

数据网格视图

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. 我可以正确看到数据,但无法编辑想要的列。

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());

The reason you're seeing those fields in the Grid is that you're binding each row to a string[] . 之所以在Grid中看到这些字段,是因为您将每一行都绑定到一个string[] So it is automatically displaying the properties of string[] as the columns. 因此它会自动将string[]的属性显示为列。 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. 为了使DataGrid正确显示数据,您应该将其绑定到自定义类型,并且它将使用该类型的公共属性作为列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM