如果为空/(null),则将datagridview单元格更改为combobox c#

如果为空/(null),则将datagridview单元格更改为combobox c#,c#,winforms,datagridview,combobox,C#,Winforms,Datagridview,Combobox,我正在使用C#和sql server 2014开发一个windows窗体。正如标题所述,如果特定列中的DataGridView单元格为空,我希望将其更改为combobox字段 我的表单加载方法如下所示: private void AdminPanel_Load(object sender, EventArgs e) { DBConn.ConnectToDatabase(); dataGridViewAP.DataSource = DBConn.getAd

我正在使用C#和sql server 2014开发一个windows窗体。正如标题所述,如果特定列中的DataGridView单元格为空,我希望将其更改为combobox字段

我的表单加载方法如下所示:

private void AdminPanel_Load(object sender, EventArgs e)
    {
        DBConn.ConnectToDatabase();

        dataGridViewAP.DataSource = DBConn.getAdminInfo();

    }
我有一个方法getAdminInfo()如下

    public DataTable getAdminInfo()
    {
        try
        {
            string strCommand = "SELECT Customer.AccountNo AS [Account Number], Customer.Name AS [Customer Name], Customer.Adrs1 AS [Address 1], Customer.Adrs2 AS [Address 2], Customer.City AS [City], Customer.Province AS [Province], Customer.PostalCode AS [Postal Code], Customer.Email1 AS [Email], Manifest.Class AS [Class], Manifest.Rname AS [Recipient Name], Manifest.Adrs1 AS [Dest. Address 1],  Manifest.Adrs2 AS [Dest. Address 2],  Manifest.City AS [City],  Manifest.State AS [State], Manifest.PostalCode AS [Postal Code], Manifest.Country AS [Country], Manifest.Item AS [Description], Manifest.Weight AS [Weight (lb)], Manifest.Value AS [Value (USD)], Manifest.DateTime AS [Date/Time], Manifest.CheckedBy AS [Checked By], Customer.AccStatus AS [Account Status] FROM Customer INNER JOIN Manifest ON Customer.AccountNo = Manifest.FKAccountNo WHERE CONVERT(DATE,DateTime)=CONVERT(Date,GETDATE())";
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(strCommand, conn);
            da.Fill(dt);
            return dt;
        }
        catch (SqlException e)
        {
            MessageBox.Show(e.Source + "/n" + e.Message + "/n" + e.StackTrace);
            return null;
        }
    }
我想将combobox单元格添加到Account status列单元格中,以便在其为空时使用所选值对其进行更新


希望我的问题很清楚,有什么想法吗?

在将DataTable绑定到DataGridView之前,创建与特定列对应的DataGridViewComboxColum,并设置该列的DataPropertyName属性。通过绑定指定组合框项

要在加载函数中插入的代码:

DataGridViewComboBoxColumn cbcol = new DataGridViewComboBoxColumn ();
cbcol.Name             = "Account_Status" ;
cbcol.DataPropertyName = "Account Status" ; // DataTable column name
cbcol.DataSource       =  ...             ; // ComboBox DataSource
dataGridViewAP.Columns.Add(cbcol);
dataGridViewAP.DataSource = DBConn.getAdminInfo();

很抱歉我反应太晚了。首先感谢您的回复。它起作用了。有没有办法将单元格恢复到以前的状态,即空/空?因为一旦单击组合框,某些值将保持选中状态。我已经使用列表作为cbcol的数据源,我在列表中添加了另一个空字段(“”),以选择一个空字段。我不确定这样做是否正确。一种可能性(未测试)是向DataGridViewComboBoxColumn数据源添加一个新的空行。