DataGridViewComboBoxCell值无效

时间:2015-03-03 07:55:26

标签: c# datagridviewcombobox

我试图在我的datagridview中放入3个绑定的DataGridViewComboBoxCell,

一个用于'分类'表,第二篇文章'

表和第三个

' ArticleNonCon'表。当我选择第二个类别时

DataGridViewComboBoxCell应该给我这个类别的文章和

当我选择一篇文章时,第三个DataGridViewComboBoxCell应该给我

本文的序列号。这是我试过的代码:

单击显示表单的按钮

if (con.State != ConnectionState.Open)
{
    con.Open();
}

SqlCommand catCmd = new SqlCommand("SELECT IdCategorie, LibCategorieFr FROM Categorie", con);
DataTable catDt = new DataTable();
catDt.Load(catCmd.ExecuteReader());
((DataGridViewComboBoxColumn)dataGridView1.Columns["CategorieColumn"]).DataSource = catDt;    
((DataGridViewComboBoxColumn)dataGridView1.Columns["CategorieColumn"]).DisplayMember = "LibCategorieFr";
((DataGridViewComboBoxColumn)dataGridView1.Columns["CategorieColumn"]).ValueMember = "IdCategorie";

并在datagridview的CellEndEdit事件中:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    switch (dataGridView1.Columns[e.ColumnIndex].Name)
    {
        case "CategorieColumn":
            if (con.State != ConnectionState.Open)
            {
                 con.Open();
            }
            SqlCommand artCmd = new SqlCommand("SELECT * FROM Article WHERE IdCategorie = @IdCategorie", con);
            artCmd.Parameters.AddWithValue("@IdCategorie", dataGridView1.CurrentRow.Cells["CategorieColumn"].Value);
            DataTable articleDt = new DataTable();
            articleDt.Load(artCmd.ExecuteReader());
            ((DataGridViewComboBoxColumn)dataGridView1.Columns["Column3"]).DataSource = articleDt;
              ((DataGridViewComboBoxColumn)dataGridView1.Columns["Column3"]).DisplayMember = "LibArticleFr";
            ((DataGridViewComboBoxColumn)dataGridView1.Columns["Column3"]).ValueMember = "CodeArticle";
            con.Close();
            break;

       case "Column3":
           if (con.State != ConnectionState.Open)
           {
               con.Open();
           }

           SqlCommand numSerieCmd = new SqlCommand("SELECT * FROM ArticleNonCon WHERE CodeArticle = @CodeArticle", con);
           numSerieCmd.Parameters.AddWithValue("@CodeArticle", dataGridView1.CurrentRow.Cells["Column3"].Value);
           DataTable numSerieDt = new DataTable();
           numSerieDt.Load(numSerieCmd.ExecuteReader());
           ((DataGridViewComboBoxColumn)dataGridView1.Columns["NumSerieColumn"]).DataSource = numSerieDt;
           ((DataGridViewComboBoxColumn)dataGridView1.Columns["NumSerieColumn"]).DisplayMember = "NumSerieArticle";
           ((DataGridViewComboBoxColumn)dataGridView1.Columns["NumSerieColumn"]).ValueMember = "id";
           con.Close();

           break;
    }
}

此代码适用于第一次运行,但是当我尝试更改文章并从第三个datagridviewComboboxCell中选择本文的序列号时,我得到了此异常:

  

System.ArgumentException:DataGridViewComboBoxCell值无效

当我尝试关闭应用程序时也会发生此异常。

那么如何解决这个错误或者我应该在哪个datagridview事件中添加我的代码?

提前致谢。

1 个答案:

答案 0 :(得分:0)

假设您的DataGridView根本没有现有数据。无论如何你必须创建数据。在你的情况下,它可能是一个只有3个属性的类

public class MyDataSource 
{
    public int CategoryID { get; set; }
    public int ArticleID { get; set; }
    public int ArticleNonCon { get; set; }
}

我们还需要一些代表ComboBox数据源的类:

public class Category
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class Article
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class ArticleNonCon
{
    public int ID { get; set; }
    public string Name { get; set; }
}

这个类(作为它的列表)将附加到您的DataGridView和IS您的DataSource(它将存储您的ComboBoxes的ValueMembers)。将以下代码放入initialize方法:

List<MyDataSource> listMyDataSource = new List<MyDataSource>() // it's an empty list of MyDataSource

dataGridView1.AutoGenerateColumns = false; // prevent DataGridView from adding columns we don't want to have

dataGridView1.DataSource = new BindingList(listMyDataSource); // this is very important as we want to add new rows to our DataGridView. Adding just a list object wouldn't allow this 

现在我们必须为ComboBoxColumns创建数据源:

List<Category> listCategories = new List<Category>
{
     new Category { ID = 1, Name = "Category1"},
     new Category { ID = 2, Name = "Category2"},
     new Category { ID = 3, Name = "Category3"},
};

List<Article> listArticles = new List<Article>
{
    new Article { ID = 1, Name = "Article1"},
    new Article { ID = 2, Name = "Article2"},
    new Article { ID = 3, Name = "Article3"},
};

List<ArticleNonCon> listArticlesNonCon = new List<ArticleNonCon>
{
    new ArticleNonCon{ ID = 1, Name = "ArticleNonCon"},
    new ArticleNonCon{ ID = 2, Name = "ArticleNonCon"},
    new ArticleNonCon{ ID = 3, Name = "ArticleNonCon"},
};

这当然只是样本数据......所以请用数据库中的数据填充它。

现在我们将列添加到DataGridView:

dataGridView.Columns.Add(new DataGridViewComboBoxColumn
{
    Name = "CategoryColumn",
    DataSource = listCategories,
    ValueMember = "ID",  // property of our class Category
    DisplayMember = "Name", // property of our class Category
    DataPropertyName = "CategoryID" // bind it to the property CategoryID from our class MyDataSource
});
...

Article和ArticleNonCon也是如此。

您需要做的一件事:订阅DataGridView的DataError事件(否则在尝试添加包含空数据的新行时会出现异常)。

dataGridView.Columns.Add(new DataGridViewComboBoxColumn
{
    Name = "ArticleColumn",
    DataSource = listArticles,
    ValueMember = "ID",
    DisplayMember = "Name",
    DataPropertyName = "ArticleID"
});

这应该做。 (这就是在Winforms中绑定和显示数据的方式)