簡體   English   中英

如何將ComboBox添加到綁定到datatable的WINFORM datagridview

[英]How to add ComboBox to WINFORM datagridview bound to datatable

我是在VS C#和Winforms中技能水平較低的SQL DBA。 我一直在努力將組合框添加到DataGridView列中已有幾​​天,並且已經放棄了。 我有一個數據表dt1和datagridview dg1。 dg1.Datasource = dt1; dt1是數據集ds1的成員。 我正在提供數組中的組合項目。

我嘗試過自動生成是非。

如果autogeneration = true,我將得到兩個具有1個組合框的同名列,並且列位置錯誤,並且從dt1獲得了正確的數據

如果為false並且我以編程方式為dg1定義了列,則不會從dt1獲得任何數據。

我的代碼應該是什么樣子以及可能缺少的綁定或屬性,因此我在第4列的位置添加了一個用於“ GRADE”的組合框,而dg1從dt1填充,而comb從數組填充。

在閱讀了數十篇博客並嘗試了幾天后,完全感到沮喪。 請幫忙。

    private DataGridViewComboBoxColumn CreateComboBox()
    {
        DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
        {
            combo.Name = "comboColumn";
            combo.HeaderText = "Grade";
            ArrayList drl = new ArrayList();
            drl.Add("GS1");
            drl.Add("GS2");
            drl.Add("WG1");
            drl.Add("WG2");
            combo.Items.AddRange(drl.ToArray());
            combo.DataSource = drl;
            //combo.ValueMember = "EmployeeID";
            //combo.DisplayMember = "Grade";
            //combo.DataPropertyName = "Grade";
        }
        return combo;
    }

    public Employee()
    {
        InitializeComponent();
        WindowState = FormWindowState.Maximized;
        Ds1 = new DataSet("ds1");

        Dt1 = new DataTable("dt1");

        ds1.Tables.Add(dt1);

        dt1.Columns.Add("EmployeeID");
        dt1.Columns.Add("FirstName");
        dt1.Columns.Add("LastName");
        dt1.Columns.Add("Grade");
        dt1.Columns.Add("DOB");

        //initialize datagridview
        Dg1.AutoGenerateColumns = true;

        //dg1.Columns.Add("column4", " EmployeeID ");
        // dg1.Columns.Add("column4", " FirstName ");
        // dg1.Columns.Add("column4", " LastName ");
     Dg1.Columns.Add(CreateComboBox());
        // dg1.Columns.Add("column5", " DOB ");

        Dg1.DataSource = dt1;

    }

解決:經過幾天的探索和嘗試,我嘗試了一個我認為不可行的解決方案,因為它提到了未綁定的列,並且似乎需要SQL或其他連接使其成為綁定列。 原來沒有必要綁定列。 這就是你要做的。

1.Open your Form designer an place Focus on your DataGridView and select properties.

2.Scroll down to the Columns collection (mine was at the bottom under Misc.) and expand the collection.
3.Add Column name and if binding to DataTable set the DataPropertyName to the dt column.  In my case I set both the same. 
Also there is a drop down to choose the ColumnType
4.Add your ComboBox column.  This has a few more settings.  You should look through all of them but I was interested in Items &
HeaderText only.  I set HeaderText the same as ColumnName &
DataPropertyName.  I then opened the Items and added my list.  
There is also a DataSource. I presume that is for populating your
ComboBox from a database, service, or sharepoint but I'm not doing
that.
5.That's it.

在您的.cs代碼文件中,您需要插入兩行代碼。 我在表格的頂部放置了我的表格,在該表格中聲明了所有方法都需要的數據表。

<yourdatagridview>.AutoGenerateColumns = false;
<yourdatagridview>.DataSource = <yourdatatable>;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM