Click here to Skip to main content
15,891,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have to Bind values from Single column of database to multiple column in Gridview.
I.e. Suppose I have column "Name" in table having value "Firstname.Lastname",now I have to bind "Firstname" in one column and "Lastname" in other column.


Please help me..

Thanks in advance.
Posted
Comments
sjelen 5-Apr-12 9:07am    
Both solutions below will work, but you should consider having two separate columns in database. How would you sort records by last name in your current database?
Member 10486850 27-Dec-13 7:25am    
code to bind same column value in the grid view
Member 10486850 27-Dec-13 7:25am    
code to bind same column value in the grid view...plzz reply urgent

Hi,
You can do it using a code like this:
ASP.NET
<asp:lable name="FirstName" text="<%# GetFirstName(Bind("Name"))%> />
<asp:lable name="LastName" text="<%# GetLastName(Bind("Name"))%> />

In your code behind you need to write these methods:

C#
public string GetFirstName(string name){
  //TODO write code to split your string and return the FirstName
}


public string GetLastName(string name){
  //TODO write code to split your string and return the LastName
}



Ii hope it will help,
Cheers.
 
Share this answer
 
v2
Create a DataTable which will have 2 columns - FirstName and LastName (or the required number of columns). Populate it with the existing data returned from database using some logic to separate the values (say based on white space). You can do this while reading the values as well (if you are using a SqlDataReader). Bind the GridView to this DataTable.

This is one of the way that first came to my mind. There could be different ways to do this.
Hope this helps!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900