将值从数据库的单列绑定到GridView中的多列 [英] Binding Values from Single column of databes to Multiple column in GridView

查看:79
本文介绍了将值从数据库的单列绑定到GridView中的多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我必须将值从数据库的单列绑定到Gridview中的多列.
IE.假设我表中的列" Name "的值为"Firstname.Lastname" ,现在我必须将"Firstname" 绑定到一个列中,其他列中的"姓氏" .


请帮帮我..

预先感谢.

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.

推荐答案


您可以使用如下代码进行操作:
Hi,
You can do it using a code like this:
<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:

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.


创建一个DataTable,它将具有2列-FirstName和LastName(或所需的列数).使用一些逻辑来分隔值(例如基于空白),并使用从数据库返回的现有数据填充它.您也可以在读取值的同时执行此操作(如果使用的是SqlDataReader).将GridView绑定到此DataTable.

这是我首先想到的一种方式.可能有不同的方法来做到这一点.
希望这会有所帮助!
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!


这篇关于将值从数据库的单列绑定到GridView中的多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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