Multiple values ​​in a DataValueField C #. network

Just wandering is the ability to put multiple values ​​in a C # DataValueField. net?

Example:

I have the following code:

   <asp:DropDownList ID="ixTeam" CssClass="ixTeam" runat="server" DataSourceID="dsixTeam"
        DataValueField="TeamID" DataTextField="TeamName" AppendDataBoundItems="true">

Is it possible for me to enter the value 2 (TeamID + "|" + TeamTypeID) in the DataValueField?

+5
source share
3 answers

You cannot combine two fields in DataValueField, but you can try several things.

  • You can build your SQL query with concatenation (if you get data from a database)

SQL query:

Select (TeamID +"|"+ TeamTypeID) as CombinedTeam, .....`

and then install

DataFieldValue = "CombinedTeam"

or

  • You can use the LINQ query to create an anonymous type based on the concatenation of two fields.

eg:

var query = from t in yourDataSource
            select new 
            {
                 CombinedTeam = t.TeamID + "|" + t.TeamTypeID,
            } 

Then specify the data source for the LINQ dropdown and specify DataFieldValue

yourDropDownList.DataSource = query;
yourDropDownList.DataFieldValue = "CombinedTeam";
+12
source

Short answer: you cannot.

, TeamID + '|' + TeamTypeID ( , , , , ), .

+4

Yes, it can be done. Format your data when retrieving from the SQL server as follows:

Select TeamID + ' | ' + TeamTypeID CombinedColumn

Now install DataValueField=CombinedColumn

+3
source

All Articles