简体   繁体   中英

How to bind a column in DataGrid to more than one column in Data source?

I have:

  • DataTable with three columns: Id , FName , LName .
  • Data Grid with two columns: Id , Full Name . Where Full Name column is combination of FName and LName .

How can the Full Name column of DataGrid can be bound to two Data Source columns ( FName and LName ) without any string manipulation?

You could add a new column to the datatable

eg:

        private void DGTest_gridBind()
    {
        DataTable table = new DataTable();
        table.Columns.Add("FName", typeof(string));
        table.Columns.Add("LName", typeof(string));

        table.Columns.Add("FullName", typeof(string), "FName + ' ' + LName");

        table.Rows.Add("Jack", "Miller");
        table.Rows.Add("Sam", "Tucker");

        DGTest.DataSource = table;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM