绑定到DataGridView的字符串列表给出字符串属性 [英] Binding List of strings to a DataGridView gives strings properties

查看:101
本文介绍了绑定到DataGridView的字符串列表给出字符串属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在StackOverflow中发现了一个类似的问题,但没有答案。我试图将一个 IList< string> 绑定到一个 DataGridView 作为它的 DataSource ,而不是输出字符串列表,例如在 ListView 中,它会输出列表中元素的属性,在这种情况下,长度



我的代码:

  public void FindMatches()
{
const string regex = @\ {([a-zA-Z _] [a-zA-Z0-9 _] *) \}
IList< string> names =(从匹配匹配在Regex.Matches(ObterConteudoArquivo(),regex)select match.Value).ToList();
_view.Lista =名称;
}

现在我有一个列表存储在 List ,其中包含所有匹配项,例如 {{CP},{DP},{EP}} 绑定到我的 DataGridView:

  public IList< string> Lista 
{
set
{
ltvCampos.DataSource = value;
}
}

这只绑定长度每个字符串。
我也做过:

  public IList< string> Lista 
{
set
{
foreach(字符串名称)
{
DataGridTextBox row = new DataGridTextBox();
row.Text = name;
ltvCampos.Rows.Add(row);
}
}
}

词法分析器说: p>

 调用带有params的方法。你打算调用更具体的方法'int Add(object)'? 


解决方案

您需要 wrap 你的字符串在一个类中,它们将公开属性与setter和getter一起公开:

  class aString {public string theString {get;组; } 
public aString(string s){theString = s; }}

现在创建一个字符串列表..

 列表< aString> theStrings = new List< aString>(); 

..并填写您的比赛:

  theStrings =(来自Regex.Matches(text,regex)的匹配匹配
选择新的aString(match.Value))ToList();

现在您可以将列表绑定到DGV:

  ltvCampos.DataSource = theStrings; 

对于添加的功能,您可能需要使用<$ c插入一层或两层以上的绑定$ c> BindingList (其中会引发更改事件):

  var blist = new BindingList< ; aString>(theStrings); 
ltvCampos.DataSource = theStrings;

或者一个 BindingList BindingSource ,这将为您提供更广泛的选项和方法:

  var blist = new BindingList< aString>(theStrings); 
var source = new BindingSource(blist,null);
ltvCampos.DataSource = source;


I found one similar question in StackOverflow, but it has no answers. I'm trying to bind a IList<string> to a DataGridView as its DataSource, but instead of it output the list of strings, like in a ListView, it outputs me the properties of the elements in the list, in this case, Length.

My code:

public void FindMatches()
{
    const string regex = @"\{([a-zA-Z_][a-zA-Z0-9_]*)\}";
    IList<string> names = (from Match match in Regex.Matches(ObterConteudoArquivo(), regex) select match.Value).ToList();
    _view.Lista = names;
}

Now that I have a list stored in List that contains all my matches, example given { "{CP}", "{DP}", "{EP"} }, I want to bind them to my DataGridView:

public IList<string> Lista
{
    set
    {
        ltvCampos.DataSource = value;
    }
}

This binds only the Length of each string. I also did:

public IList<string> Lista
{
    set
    {
        foreach (string name in value)
        {
            DataGridTextBox row = new DataGridTextBox();
            row.Text = name;
            ltvCampos.Rows.Add(row);
        }
    }
}

The lexer says:

Method with 'params' is invoked. Have you intended to call more specific method 'int Add(object)'?

解决方案

You need to wrap your strings in a class, which exposes them as public properties with both a setter and a getter:

class aString { public string theString { get; set; } 
                public aString(string s) { theString = s; }  }

Now create a list of strings..

List<aString> theStrings = new List<aString>();

..and fill it with your Matches:

theStrings  = (from Match match in Regex.Matches(text, regex) 
               select new aString(match.Value)).ToList();

Now you can bind your list to the DGV:

ltvCampos.DataSource = theStrings;

For added functionality you may want to insert one or two more layers of binding by using a BindingList (which among others will raise change events):

var blist = new BindingList<aString>(theStrings);
ltvCampos.DataSource = theStrings;

or both a BindingList and a BindingSource, which will present you with a wider range of options and methods:

var blist = new BindingList<aString>(theStrings);
var source = new BindingSource(blist, null);
ltvCampos.DataSource = source ;

这篇关于绑定到DataGridView的字符串列表给出字符串属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆