文本框自定义源作为数据表

时间:2015-08-12 09:15:49

标签: c# winforms

我正在尝试为我的文本框设置自定义来源以获取建议选项。 到目前为止,我已经做到了这一点。

 private void Input_Box_Load(object sender, EventArgs e)
 {
     sc.Open();
     SqlCommand cmd = new SqlCommand("select MoM_ID from dbo.MoM_Form ORDER BY MoM_ID ASC", sc);
     SqlDataReader R_1;

     R_1 = cmd.ExecuteReader();
     DataTable dt_1 = new DataTable();
     dt_1.Columns.Add("MoM_ID", typeof(string));
     dt_1.Load(R_1);

     TextBox_FormID.AutoCompleteMode = AutoCompleteMode.Suggest;
     TextBox_FormID.AutoCompleteSource = dt_1 ;

     sc.Close();
 }

有没有办法将dt_1转换为自动完成集合的类型? 或者我应该将值迭代到一个新列表,然后将该列表添加为源?

由于

1 个答案:

答案 0 :(得分:1)

您可以参考此thread

string[] postSource = dt_1
                    .AsEnumerable()
                    .Select<System.Data.DataRow, String>(x => x.Field<String>("MoM_ID"))
                    .ToArray();

var source = new AutoCompleteStringCollection();
source.AddRange(postSource);
  TextBox_FormID.AutoCompleteCustomSource = source;
  TextBox_FormID.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
  TextBox_FormID.AutoCompleteSource = AutoCompleteSource.CustomSource;