装满空行DataGridView的 [英] Fill the DatagridView with empty rows

查看:124
本文介绍了装满空行DataGridView的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的窗户形成一个datagridview的。这是填补根据组合框中选择的值从数据库中的数据。对于某些所选项目中有数据库记录。对于某些没有记载。当时我需要保持一个固定大小的datagridview没有记录填充它。有没有办法做到这一点。如果是,请帮我...

I have one datagridview in my windows form. That is filling data from database based on selected value in the combo-box. For some selected item there is record in the database. for some there is no records. At that time i need to maintain a fixed size datagridview with no records filled in it. Is there any way to do this. If yes please help me...

推荐答案

如果您的数据源看起来类似于这样:

If your datasource looks similar to this:

    public class MyDataSource : IEnumerable<SomeClass>
    {}



- 你可以简单地做到这一点:

-you may simply do this:

    public IEnumerator<SomeClass> GetEnumerator()
    {
        if (sourceItems == null)
        {
            yield return SomeClass.Empty;
            //as many times as you want "empty" lines in datagrid
        }
        else
        {
            foreach (var item in sourceItems)
               yield return item;
        }
    }



sourceItems是的项的内部列表键入SomeClass的。
有很多方法可以做到这一点,这只是其中之一。

"sourceItems" is the internal list of items of the type "SomeClass". There are many ways to do this and this is just one of them.

今天你正在做的是从数据库加载数据,并将结果直接写到datagridview的。我的建议是,你从数据库加载数据(如你现在做的)和替代数据写入数据网格,可以创建一个类来保存项目(数据源类)。那么这个类是给类似这样的数据网格:

What you are doing today is to load data from database and write the result directly to datagridview. What I suggest is that you load data from database (as you do now) and instead of writing data to datagrid, you create a class to hold the items (a datasource class). That class is then given to the datagrid similar to this:

MyDataSource ds = new MyDataSource();
GetDataFromDatabase().Select(p=>ds.Add(p));
BindingSource bs = new BindingSource(ds, ");
theDataGrid.DataSource = bs;

然后使用BindingSource的控制研究的内容(只是为了显示一些你可能做的事情):

Then use the BindingSource to controll the contents (just to show some of the things you may do):

 bs.AllowNew = false;
 bs.Position = bs.Count - 1;
 bs.ListChanged += new ListChangedEventHandler(bindingSource_ListChanged);
 bs.CurrentItemChanged += new EventHandler(bindingSource_CurrentItemChanged);

这篇关于装满空行DataGridView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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