List<string> lst = new List<string>();
lst.Add("aaa");
lst.Add("aa");
lst.Add("a");
lst.Add("abc");
lst.Add("ab");
lst.Add("bc");
dataGridView1.DataSource = lst;代码如上,定义一个list然后用datagridview绑定
问题:绑定之后datagridview中数据显示是字符串的长度...如下
3
2
1
3
2
2
请问如何使其显示字符
aaa
aa
a
abc
ab
bc
DataTable dt = new DataTable();
dt.Columns.Add("name");
dt.Rows.Add("aaa");
dt.Rows.Add("aa");
dt.Rows.Add("a");
dt.Rows.Add("abc");
dt.Rows.Add("ab");
dt.Rows.Add("bc");
dataGridView1.DataSource = dt;
Item
aaa
aa
a
abc
ab
bc
List<string> lst = new List<string>();
lst.Add("aaa");
lst.Add("aa");
lst.Add("a");
lst.Add("abc");
lst.Add("ab");
lst.Add("bc");
GridView1.DataSource = lst;
GridView1.DataBind();控件
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>我测试没问题
GridView1.DataBind();
...我是说winform中的datagridview控件
lst.Add("aaa");
lst.Add("aa");
lst.Add("a");
lst.Add("abc");
lst.Add("ab");
lst.Add("bc");
dataGridView1.DataSource = lst;这样DataGridView会把String类的Length属性当作一列.String中只公开了两个属性Chars[] 和Length.你的那种方法得不到你想要的结果(高人也许可以).变通办法:using System;
using System.Collections.Generic;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
List<Something> lst1 = new List<Something>(); Something st = new Something("asdfas");
lst1.Add(st); Something st1 = new Something("发送的发送到");
lst1.Add(st1); dataGridView1.DataSource = lst1;
}
} public class Something
{
public string m_str; public string Str
{
get { return m_str; }
set { m_str = value; }
} public Something(string str)
{
m_str = str;
}
}
}这样DataGridView会有一列,列名称Str
要想使DataGridView显示字符串集合,可以使用匿名类型将字符串进行包装:
var test = new List<string> { "", "", "" };
dataGridView1.DataSource = (from s in test select new { s }).ToList();
using System.Linq;
不懂Linq
错误 2 无法为源类型“System.Collections.Generic.List<string>”找到查询模式的实现。找不到“Select”。是否缺少对“System.Core.dll”的引用或未使用“System.Linq”的指令?