首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

IList<>类型数据绑定到DataGridView以后显示内容不对或无内容显示,该怎么解决

2012-05-06 
IList类型数据绑定到DataGridView以后显示内容不对或无内容显示在以前做过的项目里,我都是用 DataGridVi

IList<>类型数据绑定到DataGridView以后显示内容不对或无内容显示
在以前做过的项目里,我都是用 DataGridView.DataSource 直接赋值为 IList<> 类型的实例,完全没有问题。可是今天做的一个例子突然不能这样用了,比如我下面的代码和运行结果截图:
我把 IList<int> 类型的集合绑定到 dgv_1,就没有任何显示;
我把 IList<string> 类型的集合绑定到dgv_3,显示的居然是字符串的长度;
其他 dgv_4,dgv_5 绑定后都没有任何显示
只有把 dataTable 作为数据源绑定显示正常。

说明一下:
1)我可以保证,每个绑定到DataGridView的集合,其内部元素的值都是正确的,这个我在运行时全部用断点看过,就在对.dataSource赋值以后就出问题了;
2)我以前从没见过这种情况,对比以前的代码,我的用法没有任何改变。

这是运行结果截图



这是代码

C# code
public partial class Form1 : Form{    public Form1()    {        InitializeComponent();        dgv_1.RowHeadersVisible = false;        dgv_2.RowHeadersVisible = false;        dgv_3.RowHeadersVisible = false;        dgv_4.RowHeadersVisible = false;    }    private void btn_BindIList_Click(object sender , EventArgs e)    {        //将IList<int>绑定到dgv_1        IList<int> intList = new List<int>();        intList.Add(1);        intList.Add(99);        dgv_1.DataSource = intList;        //将Table绑定到dgv_2        DataTable dt = new DataTable();        dt.Columns.Add();        dt.Rows.Add(1);        dt.Rows.Add(99);        dgv_2.DataSource = dt;        //将IList<String>绑定到dgv_3        IList<String> strList = new List<String>();        strList.Add("1");        strList.Add("99");        dgv_3.DataSource = strList;        //将IList<MyString>绑定到dgv_4,MyString是一个包含一个string成员变量的类        IList<MyString> myStrList = new List<MyString>();        myStrList.Add(new MyString("1"));        myStrList.Add(new MyString("99"));        dgv_4.DataSource = myStrList;        //将IList<MyStringInt>绑定到dgv_5,MyStringInt是一个包含一个string和一个int成员变量的类        IList<MyStringInt> myStrIntList = new List<MyStringInt>();        myStrIntList.Add(new MyStringInt("1" , 1));        myStrIntList.Add(new MyStringInt("99" , 99));        dgv_5.DataSource = myStrIntList;        dgv_2.Columns[0].Width = dgv_2.Width - 3;        dgv_3.Columns[0].Width = dgv_3.Width - 3;        //dgv_4.Columns[0].Width = dgv_4.Width - 3;        //dgv_3.Columns[0].Width = dgv_3.Width - 3;    }}public class MyString{    public string str;    public MyString(string str)    {        this.str = str;    }}public class MyStringInt{    public string str;    public int i;    public MyStringInt(string str , int i)    {        this.str = str;        this.i = i;    }}



[解决办法]
http://www.tinybbs.cn/csharp/post/id~42198
参考这个,和你问题相似
[解决办法]
首先没有intList.DataBind()

[解决办法]
C# code
 //将IList <int>绑定到dgv_1 
    IList <int> intList = new List <int>();
    intList.Add(1);
    intList.Add(99);
    dgv_1.DataSource = intList;
    dgv_1.DataBind(); //here


[解决办法]
是不是要设置DataGridView的什么属性呀
[解决办法]
探讨
首先没有intList.DataBind()

[解决办法]
探讨

引用:
首先没有intList.DataBind()

WinForm中不用DataBind()

[解决办法]
弱弱的问下楼主,截图是怎么插入的啊????????

[解决办法]
Peter200694013
贴出的链接 学习了
[解决办法]

探讨
在以前做过的项目里,我都是用 DataGridView.DataSource 直接赋值为 IList<> 类型的实例,完全没有问题。可是今天做的一个例子突然不能这样用了,比如我下面的代码和运行结果截图:
我把 IList<int> 类型的集合绑定到 dgv_1,就没有任何显示;
我把 IList<string> 类型的集合绑定到dgv_3,显示的居然是字符串的长度;
其他 dgv_4,dgv……

热点排行