开发者

How to display multiple values in a combo box from a datatable

开发者 https://www.devze.com 2023-04-12 16:08 出处:网络
I have a datatable having 3 fields out of which I need to combine the values of the 2nd and 3rd field and display in the combo box. My approach is as under

I have a datatable having 3 fields out of which I need to combine the values of the 2nd and 3rd field and display in the combo box. My approach is as under

DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Col1", typeof(string));
dt.Columns.Add("Col2", typeof(string));            

Enumerable.Range(1, 10).ToList().ForEach(i => dt.Rows.Add(i, string.Concat("Col1", i), string.Concat("Col2", i)));

comboBox1.DataSource = dt;
comboBox1.DisplayMember = string.Format("{0} : {1}","Col1","Col2");

But I am ge开发者_StackOverflow社区tting the output as System.Data.DataRowView...

Even I cannot change it from stored procedure level. I can however do this using entity approach by exposing some property but that will be a huge change at present. Is there any mechanism by which i can use the datatable as source and accomplish the work.

Thanks


I have done it as under

DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Col1", typeof(string));
            dt.Columns.Add("Col2", typeof(string));
            dt.Columns.Add("ConcatenatedField", typeof(string), "Col1 + ' : ' +Col2"); 

            Enumerable.Range(1, 10).ToList().ForEach(i => dt.Rows.Add(i, string.Concat("Col1", i), string.Concat("Col2", i)));

            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "ConcatenatedField";


Try this..

  DataTable dt = new DataTable();
  foreach (DataRow dr in dt.Rows)
  {
      comboBox1.Items.Add(dr["Col1"].ToString() + dr["Col2"].ToString());
  }


        DataTable dt = ds.Tables[0];
        dt.Columns.Add("NewColumn", typeof(string));            

        foreach (DataRow dr in dt.Rows)
        {
            dr["NewColmun"] = dr["Column1"].ToString() + " " + dr["Co"].ToString();
        }
        Combo.DataSource = dt;
        Combo.DisplayMember = "NewColumn";
        Combo.ValueMember = "ValueField";


Bind combobox to DataView instead of DataTable. You'll get the value instead of System.Data.DataRowView. It works for me.

DataTable dt = new DataTable()
....
DataView dv = new DataView();
dv = new DataView(dt,,,DataViewRowState.CurrentRows);
comboBox1.ItemsSource = dv;
comboBox1.DisplayMemberPath = "YourColumnName";


Also you can simply use a SQL View and write your combined fields in it.

Create View yourView as    
Select Id, "Col1 + ' : ' +Col2" as bindField from yourTable;

and in code:

var yourList = DB.yourView;
if (yourList.Count() > 0)
   {
    comboBox1.DataSource = yourList ;
    comboBox1.DisplayMember = "bindField ";
    comboBox1.ValueMember = "id";
    comboBox1.SelectedItem = "";
   }


Use the Combobox Format event like :

private void cmbDirectoryType_Format(object sender, System.Windows.Forms.ListControlConvertEventArgs e)
{
  if (e.DesiredType == typeof(string))
  {
    string str1 = ((Datarow)e.ListItem)("Col1").ToString();
    string str2 = ((Datarow)e.ListItem)("Col2").ToString();
    e.Value = str1  + " " + str2;
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消