表结构是
ID   Name   FatherID
1    a公司   0
2    b公司   1
DataGrid上要显示的结果是:
ID   Name   FatherName
1    a公司   无
2    b公司   a公司表结构可能有问题,
如果不改表表结构怎么实现啊?

解决方案 »

  1.   

    select ID,Name,FatherName=case when FatherID=id then Name else '无' end from 表
      

  2.   

    --TrySELECT  [ID], [Name], ISNULL((SELECT [Name] FROM tb WHERE [ID] = a.FatherID), '无') AS FatherName FROM tb a
      

  3.   

    在DataBand()事件中添加if(e.Item.DataSetIndex != -1)
    {
    if(e.Item.Cells[2].Text == "0")
    {
    e.Item.Cells[2].Text == "无";
    }
    else
    {
    e.Item.Cells[2].Text = GetName(e.Item.Cells[2].Text)
    }
    }再加上这段
    private string GetName(string id)
    {
    string Name = "无";
    for(int i;i<DataGrid1.Rows.Count;i++)
    {
    if(DataGrid1.Rows[i].Cells[0].Text == id)
    {
    Name = DataGrid1.Rows[i].Cells[1].Text;
    break;
    }
    }
    return Name;
    }试一下吧,没测,随手写的
      

  4.   

    sql = "select a.id,a.Name,(select b.Name from table b where b.FatherID = a.FatherID) FatherID from table a";
    如上,直接绑定就可以了
      

  5.   

    #"+Date+"#,access中时间日期类型要加#的
      

  6.   

    select ID,Name,FatherName=case when FatherID=0 then N'无' else Name end from 表你把这个绑定就OK了
      

  7.   

    select id,name,fathername=case when fatherid=id then name else "无" from table
    解释一下
    简单 CASE 函数:CASE input_expression
        WHEN when_expression THEN result_expression
            [ ...n ]
        [ 
            ELSE else_result_expression
        ENDCASE 搜索函数:CASE
        WHEN Boolean_expression THEN result_expression
            [ ...n ]
        [ 
            ELSE else_result_expression
        END参数
    input_expression是使用简单 CASE 格式时所计算的表达式。Input_expression 是任何有效的 Microsoft&reg; SQL Server&#8482; 表达式。 WHEN when_expression使用简单 CASE 格式时 input_expression 所比较的简单表达式。When_expression 是任意有效的 SQL Server 表达式。Input_expression 和每个 when_expression 的数据类型必须相同,或者是隐性转换。 n占位符,表明可以使用多个 WHEN when_expression THEN result_expression 子句或 WHEN Boolean_expression THEN result_expression 子句。THEN result_expression当 input_expression = when_expression 取值为 TRUE,或者 Boolean_expression 取值为 TRUE 时返回的表达式。result expression 是任意有效的 SQL Server 表达式。 ELSE else_result_expression当比较运算取值不为 TRUE 时返回的表达式。如果省略此参数并且比较运算取值不为 TRUE,CASE 将返回 NULL 值。Else_result_expression 是任意有效的 SQL Server 表达式。Else_result_expression 和所有 result_expression 的数据类型必须相同,或者必须是隐性转换。WHEN Boolean_expression使用 CASE 搜索格式时所计算的布尔表达式。Boolean_expression 是任意有效的布尔表达式。