English 中文(简体)
如何在数据表的 com子中展示多种价值
原标题:How to display multiple values in a combo box from a datatable
  • 时间:2011-10-11 03:09:31
  •  标签:
  • c#
  • winforms

我有一个数据表,有3个领域,其中我需要把第2和第3个领域的价值观结合起来,并在 com盒中显示。 我的做法与目前的做法一样。

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");

但我正在将产出作为<编码>System.Data.DataRowView。

即便如此,我也不能从所储存的程序水平改变。 然而,我可以采用实体办法处理某些财产,但目前情况会发生巨大变化。 是否有任何机制可以将数据表作为来源并完成这项工作。

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";
问题回答

引证。

  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 combo Box to DataView und DataTable. 页: 1 它为我工作。

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

你们也可以简单地利用“公平观点”并撰写你在其中的合并领域。

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

和法典:

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

利用Combo盒式活动,如:

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;
  }
}




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签