将行添加到组合框数据表
Add rows to combobox 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;
...
}
您需要先创建 DataTable 结构,然后然后使用 add,
添加行
_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();
博士["Descp"] = "All";
博士["type"] = "0";
_dataAdapter_myReader.Rows.InsertAt(博士, 0);
然后填充组合框:)
希望对您有所帮助
我想向组合框中的数据表添加新行,但在添加时收到错误
"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;
...
}
您需要先创建 DataTable 结构,然后然后使用 add,
添加行_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();
博士["Descp"] = "All";
博士["type"] = "0";
_dataAdapter_myReader.Rows.InsertAt(博士, 0);
然后填充组合框:)
希望对您有所帮助