#region 显示多列DataTable到combobox /// <summary> /// 显示多列DataTable到combobox /// </summary> /// <param name="dataTable">含有超过两列的DataTable</param> /// <param name="comboBox">combobox控件名</param> /// <param name="TextColumn">显示的text对应的列</param> /// <param name="ValueColumn">value值对应的列</param> /// <param name="isNeedShowEmpty">是否显示‘请选择’</param> /// <param name="isNeedShowAll">是否显示‘全部’</param> public static void ToDataTableCombobox(DataTable dataTable, ComboBox comboBox, string TextColumn, string ValueColumn, bool isNeedShowEmpty, bool isNeedShowAll) { if (dataTable == null || comboBox == null || dataTable.Columns.Count == 0) { return; } DataTable dtnew = new DataTable(); dtnew.Columns.Add("text"); dtnew.Columns.Add("value"); if (isNeedShowEmpty) { DataRow emptyRow = dtnew.NewRow(); emptyRow[1] = ""; emptyRow[0] = "--请选择--"; dtnew.Rows.InsertAt(emptyRow, 0); } else if (isNeedShowAll) { DataRow emptyRow = dtnew.NewRow(); emptyRow[1] = ""; emptyRow[0] = "全部"; dtnew.Rows.InsertAt(emptyRow, 0); } for (int i = 0; i < dataTable.Rows.Count; i++) { DataRow dr = dtnew.NewRow(); dr["text"] = dataTable.Rows[i][TextColumn]; dr["value"] = dataTable.Rows[i][ValueColumn]; dtnew.Rows.Add(dr); } comboBox.DataSource = dtnew;//GetItemList() comboBox.DisplayMember = dtnew.Columns[0].ColumnName;//"text"; comboBox.ValueMember = dtnew.Columns[1].ColumnName; //"value"; } #endregion
时间: 2024-11-10 10:15:18