2015-08-21 60 views
0

我想一個新行添加到組合框的數據表,但收到錯誤添加行添加到組合框的dataTable

"Input array is longer than the number of columns in this table." 

組合框時:字符串,整數 「ALL」,0 「A」,1 「B」,2

下面是我的代碼

SqlDataAdapter _dataAdapter_myReader = new SqlDataAdapter(); 
DataTable _dataTable_myReader = new DataTable(); 

public Form_ListAll() 
{ 
    _dataAdapter_myReader = new SqlDataAdapter("SELECT DISTINCT CASE WHEN type = '1' then 'A' WHEN type = '2' then 'B' END AS Descp, type from table ORDER BY type", myConnection); 

    _dataTable_myReader.Rows.Add(new object[] { "ALL", 0 }); 
    _dataAdapter_myReader.Fill(_dataTable_myReader); 
    _cmbBoxType.DataSource = _dataTable_myReader; 
    _cmbBoxType.DisplayMember = "Descp"; 
    _cmbBoxType.ValueMember = "type"; 
    _cmbBoxType.SelectedIndex = 0; 
    ... 
} 
+0

你有在數據表中兩列..?因爲它表示數據表不包含兩列,並且您試圖添加一個包含兩列的行。 – Arshad

回答

0

,你需要首先創建數據表結構和然後添加行,

_dataAdapter_myReader = new SqlDataAdapter(「SELECT DISTINCT CASE WHEN type ='1'then'A'WHEN type ='2'then'B'END AS Descp,type from table ORDER BY type 「,myConnection);

_dataAdapter_myReader.Fill(_dataTable_myReader);

DataRow dr = callsTable.NewRow();

dr [「Descp」] =「全部」;
dr [「type」] =「0」;

_dataAdapter_myReader.Rows.InsertAt(dr,0);

,然後填充組合框:)

希望這有助於