2

How can I show multiple value in a single column of a GridView? For example when I search "Ivan" in the TextBox, the output will return multiple rows of Ivan like this:

Name Task
Ivan Task1
Ivan Task2
Ivan Task3

I want something like this

Name Task
Ivan Task1, Task2, Task3

My Table is like this

Employee (id,name)
Task(id,name)
EmployeeTask(employee.id,task.id)

Here is my sql code

SELECT  e.name,  t.name
FROM EmployeeTask et
INNER JOIN employee e ON e.id = et.employee_id
INNER JOIN task t ON t.id = et.task_id
WHERE e.name = @Name

And this is my GridView mark up

<Columns>
    <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name"/>
    <asp:BoundField DataField="task" HeaderText="Task" SortExpression="task"/>        
</Columns>
10
  • How are you adding information to your table? Commented Jun 20, 2012 at 19:49
  • Check this. This might help you stackoverflow.com/a/11126887/610194 Commented Jun 20, 2012 at 19:50
  • Please can you copy a bit of your GridView code so I can get a idea how are you currently binding the columns Commented Jun 20, 2012 at 19:53
  • How are you accessing your data (Entity Framework, linq-to-sql, etc.)? There are multiple ways this can be done, so it would help to know. Commented Jun 20, 2012 at 20:40
  • While getting data from database you can concatenation. Commented Jun 20, 2012 at 20:57

6 Answers 6

1

In pure SQL, here's a way to get your desired result which you can then feed into the GridView:

SELECT     e.name, REPLACE(REPLACE(REPLACE
                          ((SELECT     t.name
                              FROM         EmployeeTask AS et INNER JOIN
                                                    task AS t ON t.id = et.task_id
                              WHERE     (et.employee_id = e.id)
                              FOR XML RAW(''), ELEMENTS)
                   , '</name><name>', ', '), '<name>', ''), '</name>', '') AS EmployeeTasks
FROM         employee AS e
WHERE     (e.name = @Name)

Essentially, it's a subquery that flattens the tasks into XML output and then replaces the tags with commas.

Sign up to request clarification or add additional context in comments.

2 Comments

if i want to add a new line break after each task where should i use? i tried CHAR(10); but it gave me a syntax error
Modify the line as follows , '</name><name>', CHAR(13) + CHAR(10)), '<name>', ''), '</name>', '') AS EmployeeTasks
1

You have to change your SQL query.

  • If you are using MySQL you can use group_concat.
  • If you are using MSSQL you can find your answer here

Comments

0

You need to concatenate the Task name's using SQL something like this, mind you I have not tested the script

    DECLARE @CTasks VARCHAR(500)
    SET @CTasks = ''

    SELECT @CTasks = @CTasks  + t.Task + ', ' FROM Employee e
    INNER JOIN Tasks t ON t.EmployeeId = e.Id
    WHERE e.Name = @Name
    ORDER BY e.Name ASC

    IF LEN(@CTasks) > 0
    SELECT @Name As Name, @CTasks As Tasks

You might end up getting a ',' at the end that needs taking care from Tasks column

Hope this helps...

Comments

0

If you Are using mssql server 2008 then there is an introduction to the pivot functions which does the same thing you mentioned in the question

How to Use Pivot

Hope you find helpful

Comments

0

In Oracle:

select name,wm_concat(task) as task from table name group by name

I dont know in sql Server.But i think it may help u.

Similar Type Question, See it

In Sql:

SELECT Field1, Substring(Field2, 2, LEN(Field2)) AS Field2 FROM
(
    SELECT
        [InnerData].Field1,
        (SELECT  ',' + Field2 FROM @Fields WHERE Field1=[InnerData].Field1 FOR XML PATH('')) AS Field2
        FROM
        (
            SELECT DISTINCT Field1 FROM @Fields
        ) AS [InnerData]
) AS OuterData

I got this Query from Below link

Refer this link

Comments

0

Create a new class:

Class EmployeeWithTask{ string employeeName{get;set} list<string>employeeTasks {get,set}}

Create an EmployeeWithTask list.after you retrive the list of tasks, for every distinct employee in the result loop through the results and create one instance of EmployeewithTask . Finally bind the EmployeeWithTask list to your gridview

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.