[英]Change datagridview cell to combobox if empty/(null) c#
我正在使用C#和sql server 2014在Windows窗體上工作。正如標題所述,如果它們為空,我想將特定列中的DataGridView單元更改為組合框字段。
我的表單加載方法是這樣的:
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;
}
}
我想將組合框單元格添加到“帳戶狀態列”單元格,以將其更新為所選值(如果為空)。
希望我的問題清楚,有什么想法嗎?
要將DataTable綁定到DataGridView之前,請創建與您的特定列相對應的DataGridViewComboBoxColum並設置該列的DataPropertyName屬性。 通過綁定指定ComboBox項。
要在加載函數中插入的代碼:
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.