将2列合并为带标点符号的列 [英] Combine 2 Columns to one with punctuation

查看:120
本文介绍了将2列合并为带标点符号的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我需要用逗号来编译两列(LastName,Firstname)。

我唯一的问题是Firstname可能是空白和那个

的情况我不想要,附加到姓氏。


此数据来自Sql Server数据库。我目前使用DataReader

并将其绑定到数据网格以显示

信息。


我意识到我可以使用数据集/数据表而不是阅读器和

添加一个新列,读取数据表并构建新列。

然后删除或隐藏两个原始列列。但这是最好(最有效)的方式吗?


谢谢


dbl

解决方案

您需要一个模板才能执行此操作。我假设您正在使用DataGrid?


< asp:DataGrid Runat =服务器ID = _grid>

< Columns>

< asp:TemplateColumn>

< ItemTemplate>

<%#GetFirstLastName(Container.DataItem)%>

< / ItemTemplate>

< / asp:TemplateColumn>

< / Columns>

< / asp:DataGrid>


然后是这样的代码:


< script runat =" server">

string GetFirstLastName(对象行)

{

string fname = DataBinder.Eval(row," FirstName")as string;

string lname = DataBinder.Eval(row," LastName")as string;

if(fname == null || fname.Trim()。Length == 0)

{

返回lname;

}

返回String.Format(" {0},{1}",lname,fname);

}

< / script>


填写你自己的逻辑(并进行测试! - 我没有运行此代码)。不过你可以得到这个想法。


-Brock

DevelopMentor
http://staff.develop.com/ballen

你好,

我需要用逗号来编译两个列(LastName,Firstname)。
我唯一的问题是Firstname可能是空白的,在那个
的情况下我不是想要,附加到姓氏。

此数据来自Sql Server数据库。我目前正在使用DataReader并简单地将其绑定到数据网格以显示
信息。

我意识到我可以使用数据集/数据表而不是阅读器和
添加一个新列,读取数据表并构建新列。
然后删除或隐藏两个原始列。但这是最好(最有效)的方法吗?

dbl




只是我的


0.02 ......我认为Brock的建议是最好的,除非这是

你会经常做的。你提到了一个数据集,显然

利用缓存它们的能力可能是有益的(将列一次合并到一个新列中并完成它)。 Brock的方式

显然每次都有效....因为你没有提供任何更广泛的关于你的使用范围的b $ b范围,所以这是不可能的猜猜哪种方法可能最好...... b $ b最好......但至少要指出差异(尽管它们有点显而易见)。


卡尔


-

我的ASP.Net教程
http://www.openmymind.net/ - 新增和改进(是的,弹出窗口是

烦人)
http://www.openmymind.net/faq.aspx - 非正式的新闻组常见问题解答(更多内容

来了!)

" Brock Allen" < BA **** @ NOSPAMdevelop.com>在消息中写道

news:32 ********************** @ msnews.microsoft.com ...

您需要一个模板才能执行此操作。我假设您正在使用DataGrid?

< asp:DataGrid Runat =服务器ID = _grid>
<列>
< asp:TemplateColumn>
< ItemTemplate>
<%#GetFirstLastName(Container.DataItem)%>
< / ItemTemplate>
< / asp:TemplateColumn>
< /专栏>
< / asp:DataGrid>

然后是一些类似的代码:

< script runat =" server">
字符串GetFirstLastName(对象行)
{/ f>字符串fname = DataBinder.Eval(row," FirstName")as string;
string lname = DataBinder.Eval(row," LastName" )as string;
if(fname == null || fname.Trim()。length == 0)
{
返回lname;
}
返回String .Format(" {0},{1}",lname,fname);
}
< / script>

填写您自己的逻辑(和测试也! - 我没有运行此代码)。但是你得到了这个想法。

-Brock
DevelopMentor
http://staff.develop.com/ballen

你好,

我需要compine两个带有逗号的列(LastName,Firstname)。
我唯一的问题是Firstname可能是空白的,并且在那个
情况下我不想要,附加到姓氏。

此数据来自Sql Server数据库。我目前正在使用DataReader并简单地将其绑定到数据网格以显示
信息。

我意识到我可以使用数据集/数据表而不是阅读器和
添加一个新列,读取数据表并构建新列。
然后删除或隐藏两个原始列。但这是最好(最有效)的方法吗?

dbl




Howdy,

I need to compine two columns (LastName, Firstname) with the comma.
The only problem I have is the Firstname could be blank and in that
case I don''t want the "," appended to the last name.

This data is coming out of a Sql Server data base. I am currently
using a DataReader and simply binding it to a data grid to display the
information.

I realize that I could use a dataset/datatable instead of a reader and
add a new column, read thorugh the datatable and build the new column.
Then delete or hide the two original columns. But is that the
best(most effecient) way to do it?

Thanks

dbl

解决方案

You will need a template to do this. I assume you''re using a DataGrid?

<asp:DataGrid Runat=server ID=_grid>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<%# GetFirstLastName(Container.DataItem) %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

And then some code like this:

<script runat="server">
string GetFirstLastName(object row)
{
string fname = DataBinder.Eval(row, "FirstName") as string;
string lname = DataBinder.Eval(row, "LastName") as string;
if (fname == null || fname.Trim().Length == 0)
{
return lname;
}
return String.Format("{0}, {1}", lname, fname);
}
</script>

Fill in your own logic (and testing too! -- I didn''t run this code). You
get the idea, though.

-Brock
DevelopMentor
http://staff.develop.com/ballen

Howdy,

I need to compine two columns (LastName, Firstname) with the comma.
The only problem I have is the Firstname could be blank and in that
case I don''t want the "," appended to the last name.

This data is coming out of a Sql Server data base. I am currently
using a DataReader and simply binding it to a data grid to display the
information.

I realize that I could use a dataset/datatable instead of a reader and
add a new column, read thorugh the datatable and build the new column.
Then delete or hide the two original columns. But is that the
best(most effecient) way to do it?

Thanks

dbl




Just my


0.02...I think Brock''s suggestion is the best, unless this is
something that you''ll be doing often. You mentioned a dataset and obviously
taking advantage of the capability to cache them might be beneficial (merge
the columns once into a new column and be done with it). Brock''s way
obviously does this work each time....since you didn''t provide any broader
scope about your usage, it''s impossible to guess at which method might be
best...but atleast wanted to point out the differences (even though they are
somewhat obvious).

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:32**********************@msnews.microsoft.com ...

You will need a template to do this. I assume you''re using a DataGrid?

<asp:DataGrid Runat=server ID=_grid>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<%# GetFirstLastName(Container.DataItem) %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

And then some code like this:

<script runat="server">
string GetFirstLastName(object row)
{
string fname = DataBinder.Eval(row, "FirstName") as string;
string lname = DataBinder.Eval(row, "LastName") as string;
if (fname == null || fname.Trim().Length == 0)
{
return lname;
}
return String.Format("{0}, {1}", lname, fname);
}
</script>

Fill in your own logic (and testing too! -- I didn''t run this code). You
get the idea, though.

-Brock
DevelopMentor
http://staff.develop.com/ballen

Howdy,

I need to compine two columns (LastName, Firstname) with the comma.
The only problem I have is the Firstname could be blank and in that
case I don''t want the "," appended to the last name.

This data is coming out of a Sql Server data base. I am currently
using a DataReader and simply binding it to a data grid to display the
information.

I realize that I could use a dataset/datatable instead of a reader and
add a new column, read thorugh the datatable and build the new column.
Then delete or hide the two original columns. But is that the
best(most effecient) way to do it?

Thanks

dbl




这篇关于将2列合并为带标点符号的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆