在DataGridView中的一个问题:datagridview的似乎只读用户(的WinForms) [英] A Problem in DataGridView : datagridview seems readonly to user (WinForms)

查看:134
本文介绍了在DataGridView中的一个问题:datagridview的似乎只读用户(的WinForms)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的形式一个DataGridView。它填补了与国家,我的城市选择国家已设置属性(AllowUsersToAddRow = TRUE)
,但是当我跑我的项目用户不能添加或编辑或删除任何row.I检查it.It不是只读(只读= FALSE),它是允许(= TRUE)

I have a datagridview in my form. It fills by selecting country with cities of country.I have set the property (AllowUsersToAddRow = True) but when i run my project user can't add or edit or delete any row.I checked it.It is not readonly(readonly = false) and It is enable (Enabled = true)

这是什么问题?

准则填写datagridview的:

Code of fill datagridview:

private void cmbCountryValues_SelectedIndexChanged(object sender, EventArgs e)
{
    dgvCityValues.Enabled = cmbCountryValues.SelectedIndex>=0;
    if (!dgvCityValues.Enabled)
    {
        dgvCityValues.DataSource = null;
        return;
    }

    int CountryId = int.Parse(cmbCountryValues.SelectedValue.ToString());

    dgvValues.DataSource = from record in Program.dal.Cities(CountryId) select new { record.City};
 }

如果你觉得这个问题非常有用不忘记投票吧。

If you find this question useful don't forgot to vote it.

推荐答案

要给出一个简单的例子,如果我做您的查询的等价物,如:

To give a simplified example, if I do the equivalent of your query, such as:

var cities = new City[] { new City("New York","NY"), new City("Sydney","SY"), new City("London","LN") };
dataGridView.DataSource = cities;



我得到同样的结果,你 - 没有选择添加新行,但如果我改变的BindingList< T> 这个设置为 AllowNew 这所有的作品:

I get the same result as you - no option to add new rows, but if I change to BindingList<T> and set this to AllowNew it all works:

var cities = new City[] { new City("New York","NY"), new City("Sydney","SY"), new City("London","LN") };
var citiesBinding = new BindingList<City>(cities);
citiesBinding.AllowNew = true;

dataGridView.DataSource = citiesBinding;



编辑 - 与您的特定例如一个解决方案:

EDIT - with a solution for your particular example:

private class City
{
    public string Name { get; set; }
}

private void cmbCountryValues_SelectedIndexChanged(object sender, EventArgs e)
{
    dgvCityValues.Enabled = cmbCountryValues.SelectedIndex >= 0;
    if (!dgvCityValues.Enabled)
    {
        dgvCityValues.DataSource = null;
        return;
    }

    int CountryId = int.Parse(cmbCountryValues.SelectedValue.ToString());

    var queryResults = from record in Program.dal.Cities(CountryId) select new City { Name = record.City };
    var queryBinding = new BindingList<City>(queryResults.ToList());
    queryBinding.AllowNew = true;

    dgvValues.DataSource = queryBinding;
}



请注意)我不得不改变匿名类型的查询选择到具体类型城市,改变它的的IEnumerable< T> 通过Linq查询返回到 IList的< T> 兼容类型来创建的BindingList< T> 。这应该工作,但是:)

Note that a) I had to change the anonymous type in the query select into a concrete type City and also change the IEnumerable<T> returned by Linq query to an IList<T> compatible type to create the BindingList<T>. This should work, however :)

这篇关于在DataGridView中的一个问题:datagridview的似乎只读用户(的WinForms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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