concatenation in Gridview

Posted by Vijayar under ASP.NET on 5/19/2011 | Points: 10 | Views : 11614 | Status : [Member] | Replies : 15
Hi
I have the fields as first name,last name ,address in a table,I want to concatenate first name and last name into a single field and display it in grid view separated by a horizontal line.Please Help me

vijaya


Responses

Posted by: Nithadeepak on: 5/19/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,
try this...
select FirstName+'-'+LastName Name from Table


Now bind this result set to gridview
Thanks,

Nitha Deepak

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prafullshrivastava on: 5/19/2011 [Member] Starter | Points: 25

Up
0
Down

Try this if it useful for u then reply me.

<Columns>
<asp:TemplateField>
<HeaderTemplate>
Name
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "FirstName").ToString() %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "LastName").ToString() %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>

Pshrivastava

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 5/19/2011 [Member] Starter | Points: 25

Up
0
Down
can u please explian by taking an example

vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 5/19/2011 [Member] Starter | Points: 25

Up
0
Down
can u explain me in detail by taking an example with an attachment if possible

vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nithadeepak on: 5/19/2011 [Member] Bronze | Points: 25

Up
0
Down
check out this....
aspx
<body>

<form id="form1" runat="server">
<div>
<asp:GridView AutoGenerateColumns="false" runat="server" ID="gr1">
<Columns>
<asp:BoundField HeaderText="Name" DataField="Name" />
</Columns></asp:GridView>
</div>
</form>
</body>

aspx.cs
 protected void Page_Load(object sender, EventArgs e)

{
string conString = your connection string;
SqlDataAdapter adapter = new SqlDataAdapter("select firstname+'-'+lastname Name from employee", conString);
DataSet ds = new DataSet();
adapter.Fill(ds, "Employee");
gr1.DataSource = ds;
gr1.DataBind();

}

Thanks,

Nitha Deepak

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prafullshrivastava on: 5/19/2011 [Member] Starter | Points: 25

Up
0
Down
ok try this, i think this is help you.



check out this....
aspx
<body>
<form id="form1" runat="server">
<div>
<asp:GridView AutoGenerateColumns="false" runat="server" ID="gr1">
<Columns>
<asp:TemplateField>
<HeaderTemplate>Name</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "firstname").ToString() %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "lastname ").ToString() %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>

aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
string conString = your connection string;
SqlDataAdapter adapter = new SqlDataAdapter("select firstname,lastname from employee", conString);
DataSet ds = new DataSet();
DataTable dt =new DataTable();
adapter.Fill(ds, "Employee");
dt=ds.Tables[0];
gr1.DataSource = dt;
gr1.DataBind();

}

Pshrivastava

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 5/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
I want the concatenation to be separated by a new line but not as Ram-Lakshman as name
it should be Ram newline Lakshman

vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 5/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
I want the concatenation to be separated by a new line but not as Ram-Lakshman as name
it should be Ram newline Lakshman

vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 5/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi

1.Try to use <br/> in your select query like(Select FirstName+'<br/>'+LastName Name from xyz)
or
2.Create a Template column by using two labels separated by <br/>.
<asp:TemplateField> 

<HeaderTemplate>Name</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
<br/>
<asp:Label runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
* Please Note in the second case your collection should have atleast those two properties

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nithadeepak on: 5/19/2011 [Member] Bronze | Points: 25

Up
0
Down
Refer this....
http://msdn.microsoft.com/en-us/library/Aa258239

Nitha Deepak

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 5/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
i am using the query as Select FName +' <br/>' + LName Name,FName,LName from Example"
and design as
<form id="form1" runat="server">
<div>
<table align="center" width=50%>

<tr>
<td><asp:Label ID="Lblfname" runat="server" Text="FirstName"> </asp:Label></td>
<td> <asp:TextBox ID="txtfirstname" runat="server"></asp:TextBox></td>

</tr>
<tr>
<td><asp:Label ID="Lblmname" runat="server" Text="MiddleName"> </asp:Label></td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<td> <asp:TextBox ID="txtmiddlename" runat="server" ></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="Lbllname" runat="server" Text="LastName"> </asp:Label></td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<td> <asp:TextBox ID="txtlastname" runat="server" ></asp:TextBox></td>
</td>
</tr>

But i am getting the output as Firstname<br/> Lastname
ex: Vijaya<br>Lakshmi
But i need as
Vijaya
Lakshmi
Where vijaya is Firstname and Lkshmi Is Last Name
where is the mistake in my code
Please let me know








<tr>
<asp:GridView ID="GridView1" runat="server">

<Columns>

<asp:TemplateField>

<HeaderTemplate>Name</HeaderTemplate>

<ItemTemplate>

<asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>

<br/>

<asp:Label ID="Label2" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>
</Columns>
</asp:GridView>
</tr>

</table>
</div>
</form>

vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 5/23/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
I have the fields as first name,last name ,address in a table,I want to concatenate first name and last name into a single field and display it in Datalist from database separated by a horizontal line .Please Help me with an example

vijaya


vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 5/23/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
I have the fields as first name,last name ,address in a table,I want to concatenate first name and last name into a single field and display it in Datalist from database separated by a horizontal line .Please Help me with an example

vijaya



vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 6/13/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
Please Help me it's very urgent


private void bindtoddl()
{



SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings[""].ToString();
conn.Open();

SqlDataAdapter da = new SqlDataAdapter("SELECT 'B'+Cast(branchname as varchar)branchname, (FirstName+MiddleName+' '+LastName +' ('+d.DesignationName+')') AS Name ,nb.branchid FROM EmployeePersonalDetails ep,dbo.EmployeeCompanyDetails ec, Designations d,NewBranches nb where ep.EmployeeId=ec.EmployeeId and ec.DesignationId=d.DesignationId and nb.BranchId=ec.BranchId and d.DesignationName like 'COUNSELOR (SENIOR)' union (select 'B'+Cast(branchname as varchar)branchname, branchname ,branchid from newbranches )order by branchid", conn);

//SqlDataAdapter da = new SqlDataAdapter("SELECT (FirstName+MiddleName+' '+LastName +' ('+d.DesignationName+')') AS Name ,nb.branchid,nb.branchname FROM EmployeePersonalDetails ep,dbo.EmployeeCompanyDetails ec, Designations d,NewBranches nb where ep.EmployeeId=ec.EmployeeId and ec.DesignationId=d.DesignationId and nb.BranchId=ec.BranchId and d.DesignationName like 'COUNSELOR (SENIOR)' union (select 'B'+Cast(branchname as varchar)branchname,branchid,branchname from newbranches )order by branchid", conn);

DataSet ds = new DataSet();
da.Fill(ds);

rdemployees.DataSource = ds;
rdemployees.DataTextField = "Name";
rdemployees.DataValueField = "branchname";
rdemployees.DataBind();



}

private void SetDropDownStyle()
{
foreach (var item in rdemployees.Items.Cast<ListItem>().Where(li => li.Value.StartsWith("B", StringComparison.OrdinalIgnoreCase)))
{

item.Attributes.CssStyle.Add("font-weight", "bold");
}
}


I am concatenating branchname ia m using dropdown style,but iam not getting .Please solve the query



vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: BAIJUEP on: 8/21/2011 [Member] Starter | Points: 25

Up
0
Down
The solution given by Vijayar Posted on: 5/19/2011 8:20:44 AM worked thanks a lot

Baiju EP

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response