各位大侠好!!
我现在有一个前台注册页面,有一个是兴趣爱好项,为CheckBox多项选择,存入数据库时存的是它的Value,如:前台页面选择项为看书,听音乐,存入数据库的则是1;2(1对应看书,2对应听音乐,中间还有个分号 - -!)
我用GridView读取数据也是读的1;2,有没有办法在读取时让它显示为看书,听音乐!!
谢谢了!!急~~

解决方案 »

  1.   

    当然可以,后台类似rowdatabound事件
      

  2.   

    看你是想显示成标签上的"看书,听音乐"
    还是复选框打勾的效果
    前者麻烦点,后者简单
    1,前者需要转换成字符串数组后再读取
    2.后者直接转着数组就可以绑定了
    string str=...;
    foreach(listItem li in chk.items)
    {
         if(str==li.value)
           li.checked=true;   
    }
      

  3.   

    select case 列 when 1 then 看书 when 2 then 听音乐 end as 列
    逻辑判断自己想
      

  4.   

    这个字段值是并列的多项,用SELECT 不好实现吧
    我是想在后台写个方法,在前台GridView绑定时调用这个方法。
      

  5.   

    写个转换方法,输入是1,2,3等字符串数组,输出是看书,听音乐等数组
    public string[] str(string array)
    {
        string tempstring;
        foreach(string s in array)
        {
             tempstring+=....//通过sql语句查询出来的名字,,select name from table where id=s  
             tempstring+=tempstring+";";
        }
        return tempstring
    }
      

  6.   

    xhan2000 说的貌似是对的吧,
    在rowdatabound里,判断,row[i].cell[j].text中的值,若是1 ,则把它重新赋值
    if(row[i].cell[j].text==1)
    {
      row[i].cell[j].text=“tingyinyue
    }
    呵呵,不知道对不对。
      

  7.   

    你把值和文字都放到数据库的表里就简单了
    只放值的话 
    if(row[e.RowIndex].cell[列位置].text==1) 

      row[e.RowIndex].cell[列位置].text=“tingyinyue 

      

  8.   

    方法很多的2楼的方法应该可以
    还可以在aspx页面调用cs方法,就是你吧“1,2”传进这个方法处理后
     return你想要的结果就可以
      

  9.   

    <%# Eval("bjxx_bjlb").ToString()=="1"?"读书":(Eval("bjxx_bjlb").ToString()=="2"?"音乐":"不详")%>
      

  10.   

    <%# Eval("bjxx_bjlb").ToString()=="1"?"读书":(Eval("bjxx_bjlb").ToString()=="2"?"音乐":"不详")%>
      

  11.   


        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType==DataControlRowType.DataRow)
            {
                
                if (e.Row.Cells[4].Text.Trim() == "1")//注意cell[]里面的取值
                {
                    e.Row.Cells[4].Text = "<font color=red>看书</font>";
                }
                else 
                {
                    e.Row.Cells[4].Text = "听音乐";
                }
            }
        }
      

  12.   

    你可以先绑定出来,然后按照:拆分
    string []arr=绑定.split(new char[]{':'})
    arr[0]=看书
    arr[i]=听音乐
      

  13.   

    推荐在数据库加两个表:Interest(id int,name varchar(25)) ,User_Interest(id int,UserId int,InterestID int). 把你注册的结果存到User_Interest中,在GridView绑定时指定
    GridView1.DataKeyNames = new string[] { "UserId" };
    GridView1.DataBind();
    在<asp:TemplateField HeaderText="兴趣">
           <ItemTemplate>
               <asp:Label ID="lblInterest" runat="server"></asp:Label>
           </ItemTemplate>
     </asp:TemplateField>
    然后在
     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int UserId= Convert.ToInt32((GridView1.DataKeys[e.Row.RowIndex]["UserId"]).ToString());
                Label lblInterest= (Label)e.Row.FindControl("lblInterest") as Label;
                UserBL userBL = new UserBL();
                SqlDataReader dr = userBL.SelectInterest(UserId);//多表连接查询Interest,User_Interest
                string s = "";
                while (dr.Read())
                {
                    s +="["+ dr["name"].ToString()+"]"+"";
                }
                lblInterest.Text =s;
            }
        }
    以后如果增加或者修改兴趣,程序代码不用改!