为什么我不能看到的DataGridViewRow添加到一个DataGridView?我不、能看到、DataGridView、DataGridViewRow

由网友(余生相念)分享简介:我想显示行一个DataGridView。这里的code:的foreach(客户卡斯特在CUSTLIST){字符串[] rowValues​​ = {cust.Name,cust.PhoneNo};的DataGridViewRow行=新的DataGridViewRow();布尔行集= row.SetValues​​(ro...

我想显示行一个DataGridView。

这里的code:

 的foreach(客户卡斯特在CUSTLIST)
            {
                字符串[] rowValues​​ = {cust.Name,cust.PhoneNo};
                的DataGridViewRow行=新的DataGridViewRow();
                布尔行集= row.SetValues​​(rowValues​​);
                row.Tag = cust.CustomerId;
                dataGridView1.Rows.Add(行);
            }
 

在窗体加载,我已经初始化dataGridView1的是:

  dataGridView1.ColumnCount = 2;
dataGridView1.Columns [0] .Name点=名称;
dataGridView1.Columns [1],请将.Name =电话;
 

在执行该code,四值得注意的事情发生了:

我可以看到dataGridView1的创建一个新的行。 有没有在它的文字。 行集是假执行row.SetValues​​方法之后。 在该行标记值是否设置正确。

为什么没有在DataGridView显示的数据?

解决方案

 名单,其中,客户> CUSTLIST = GetAllCustomers();
            dataGridView1.Rows.Clear();

            的foreach(在CUSTLIST客户CUST)
            {
                //首先增加了该行,因为那是它是如何工作!不知道为什么!
                的DataGridViewRow R = dataGridView1.Rows [dataGridView1.Rows.Add()];
                //然后编辑
                R.Cells [名称]值= cust.Name。
                。R.Cells [地址]值= cust.Address;
                。R.Cells [电话]值= cust.PhoneNo;
                //客户ID是无形的,但仍然可用,比如,
                //当双单击显示的全部细节
                R.Tag = cust.IntCustomerId;
            }
 
C winform中的Datagridview添加列是提示不是有效标识符

http://aspdiary.blogspot.com/ 2011/04 /添加新行到datagridview.html

I am trying to show rows in a DataGridView.

Here's the code:

foreach (Customers cust in custList)
            {
                string[] rowValues = { cust.Name, cust.PhoneNo };
                DataGridViewRow row = new DataGridViewRow();
                bool rowset = row.SetValues(rowValues);
                row.Tag = cust.CustomerId;
                dataGridView1.Rows.Add(row);
            }

On form load, I have initialized dataGridView1 as:

dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "Name";
dataGridView1.Columns[1].Name = "Phone";

After this code is executed, four notable things happen:

I can see a new row created in the dataGridView1. There's no text in it. rowset is false after row.SetValues method is executed. The row tag value is set correctly.

Why doesn't the DataGridView show data?

解决方案

List<customer> custList = GetAllCustomers();
            dataGridView1.Rows.Clear();

            foreach (Customer cust in custList)
            {
                //First add the row, cos this is how it works! Dont know why!
                DataGridViewRow R = dataGridView1.Rows[dataGridView1.Rows.Add()];
                //Then edit it
                R.Cells["Name"].Value = cust.Name;
                R.Cells["Address"].Value = cust.Address;
                R.Cells["Phone"].Value = cust.PhoneNo;
                //Customer Id is invisible but still usable, like,
                //when double clicked to show full details
                R.Tag = cust.IntCustomerId;
            }

http://aspdiary.blogspot.com/2011/04/adding-new-row-to-datagridview.html

阅读全文

相关推荐

最新文章