向combobox dataTable添加行

时间:2015-08-21 06:31:42

标签: c# .net winforms datatable

我想在组合框中向数据表添加新行,但在添加

时会收到错误
"Input array is longer than the number of columns in this table."

combobox:string,int " 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;
    ...
}

1 个答案:

答案 0 :(得分:0)

首先需要创建DataTable结构,然后然后添加带有添加的行,

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

_dataAdapter_myReader.Fill(_dataTable_myReader);

DataRow dr = callsTable.NewRow();

dr [“Descp”] =“全部”;
  dr [“type”] =“0”;

_dataAdapter_myReader.Rows.InsertAt(dr,0);

然后填充组合框:)

希望这有帮助