在ASp.Net的ListView中绑定值 [英] Binding Values in ListView of ASp.Net

查看:112
本文介绍了在ASp.Net的ListView中绑定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想根据以下场景绑定Listview中的值。



数据库STRUCTIRE:

 AGE SEX DOCTORNAME TABLETNAME 
21 F WILLIAM CALBOL,DOL,XXX
22 M XXXXX LIVOGERN ,DOLOX,XXX



我选择上表中的所有值并将其绑定在listview中,格式如下:

< b>

LISTVIEW


 AGE SEX DOCTORNAME TABLETNAME 
21 F WILLIAM CALBOL
21 F WILLIAM DOL
21 F WILLIAM XXX
22 M XXXXX LIVOGERN
22 M XXXXX DOLOX
22 M XXXXX XXX





从Db我希望用,将TABLETNAME溢出,并将其显示在列表的单行中。在正常列表视图中意味着我可以选择值并根据columname将其绑定。任何人都可以帮我这样做吗?

解决方案

一种方法是你在 MS excel 中导入数据库,然后通过使用data to columns with delimiter拆分列来分隔TABLETNAME值,然后再次创建一个具有单独TABLETNAME的新数据库。



或者,您可以使用 SQL CHARINDEX

 OleDbConnection con =  new  OleDbConnection(DisConnection); 
con.Open();
string selquery = 从tblTablet中选择* ;
OleDbCommand cmd = new OleDbCommand(selquery,con);
OleDbDataReader read = cmd.ExecuteReader();
DataTable dat = new DataTable();
dat.Columns.AddRange( new DataColumn [ 8 ] {
new DataColumn( Sex typeof string )),
new DataColumn( 年龄 typeof string )),
new DataColumn( DoctorName typeof string ))} new DataColumn( Tablet typeof string ))});
while (read.Read())
{

string sex = read.GetValue( 0 )。ToString();
string age = read.GetValue( 1 )。ToString();
string Name = read.GetValue( 2 )。ToString();
string tablet = read.GetValue( 3 )。ToString();
string [] spilled = tablet.spilt(' );
for int i = 0 ; i< spilt.length; i ++)>
{
dat.Rows.Add(sex,age,Name,spilled [ I]的ToString());
}
}
Listview1.DataSource = dat;
Listview1.DataBind();


你已经解决了这个问题,但我想在这里提一点。



表的结构不正确。当你有一个 1:N关系医生平板电脑,所以你应该另一个表来处理这个问题。无需使用逗号专栏。



因此,您的新表格看起来像......

  DOCTORNAME TABLETNAME  
WILLIAM CALBOL
WILLIAM DOL
WILLIAM XXX
XXXXX LIVOGERN
XXXXX DOLOX
XXXXX XXX



如果你只能存储ID,那就更好了,只要你有一个带有ID的单独表格。

  DOCTORID TABLETID  
1 1
1 2
1 3
2 4
2 5
2 3


Hi,
I want to bind the values in Listview depending on the following Scenario.

DATABASE STRUCTIRE:

AGE      SEX    DOCTORNAME    TABLETNAME
21       F        WILLIAM      CALBOL,DOL,XXX
22       M        XXXXX        LIVOGERN,DOLOX,XXX


I am selecting all the values from above table and bind it in the listview in below format:

LISTVIEW

AGE      SEX    DOCTORNAME    TABLETNAME
21       F        WILLIAM      CALBOL
21       F        WILLIAM      DOL
21       F        WILLIAM      XXX
22       M        XXXXX        LIVOGERN
22       M        XXXXX       DOLOX
22       M        XXXXX        XXX



From Db i want to spilt the TABLETNAME by "," and displayed it in single row of list. In normal list view means i can select the values and bind it in depending on columname.Anyone help me to do this?

解决方案

One way to do it is that you import the database in MS excel , then separate the TABLETNAME values by splitting columns using "data to columns with , delimiter" , then again creating a new DB with seperate TABLETNAMEs.

Alternatively, you can select Distinct TABLETNAMEs using SQL CHARINDEX


            OleDbConnection con = new OleDbConnection(DisConnection);
            con.Open();
            string selquery = "Select * from tblTablet";
            OleDbCommand cmd = new OleDbCommand(selquery, con);
            OleDbDataReader read = cmd.ExecuteReader();
            DataTable dat = new DataTable();
            dat.Columns.AddRange(new DataColumn[8] { 
new DataColumn("Sex",typeof(string)),
new DataColumn("Age",typeof(string)), 
new DataColumn("DoctorName",typeof(string))}                                                                      new DataColumn("Tablet",typeof(string))});
            while (read.Read())
            {

                string sex = read.GetValue(0).ToString();
                string age = read.GetValue(1).ToString();
                string Name= read.GetValue(2).ToString();
                string tablet= read.GetValue(3).ToString(); 
                string[] spilt=tablet.spilt(',');
                for(int i=0;i<spilt.length;i++)>
                {
                    dat.Rows.Add(sex,age,Name,spilt[i].ToString());
                }
            } 
   Listview1.DataSource=dat;            
   Listview1.DataBind();


You have solved this, but I would like to mention one point here.

The structure of Table is not correct. As you are having a 1:N Relationship with Doctor and Tablet, so you should another Table to handle this. No need to have a comma spearated column.

So, your new Table would look like...

DOCTORNAME    TABLETNAME
WILLIAM        CALBOL
WILLIAM        DOL
WILLIAM        XXX
XXXXX          LIVOGERN
XXXXX          DOLOX
XXXXX          XXX


It would be better, if you can store only the Ids, provided you have individual tables for them with an ID.

DOCTORID    TABLETID
   1            1
   1            2
   1            3
   2            4
   2            5
   2            3


这篇关于在ASp.Net的ListView中绑定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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