菜鸟请教各位高手:
我有一个职员表,存放职员信息,同时还有一个状态表,存放职员的状态。
职员表里面的状态字段存放的是状态的id,现在我想把职员表绑定到一个DataGrid控件,但我想在Grid里面显示状态的名称,而不是状态的id,我该如何做?
我的绑定代码是:
string sql = "select * from employee"
DataSet ds = GetDataSet(sql,"tablename"); //GetDataSet是我自己写的一个函数。
myDataGrid.DataSource = ds;

解决方案 »

  1.   

    能说的稍微详细点吗,我刚学C#不到一个月。楼上的能不能留下一个QQ号,还有很多问题向你请教呢。
      

  2.   

    肯定还有个雇员表employee的,比如:状态表叫status
    所以应该两个表做关联
    select employee.name, *
    from status inner join employee on
    employee.id = status.id
      

  3.   

    这个是sql问题.用个left join就解决了.可以去SQL区看看
      

  4.   

    //form1窗体里
    string str="select employee.name,status.name from employee,status where empioyee.id=status.id"; //这个我没测试过,不知道是否正确,反正意思是通过关联查询得出职员名、职员状态
    string str_tb="table_Name";
    DataSet ds = new DataSet();
    private void from1_Load(object sender, EventArgs e)
    {
       ds = linkdb.queryDB(str, str_tb);
       dataGridView1.DataSource = ds;
       dataGridView1.DataMember = str_tb;
       define_dataGridView();
    }//自定义dataGridView
    private void define_dataGridView()
    {            
         dataGridView1.Columns[0].HeaderText = "职员姓名";
         dataGridView1.Columns[1].HeaderText = "状态";
    }//类
    ......(省略) 
    class LinkDB
    {
         private SqlConnection conn = new SqlConnection("server=xx;uid=sa;pwd=xxx;database=xxx");
          private SqlDataAdapter da;
          DataSet ds = new DataSet();
          public DataSet queryDB1(string strsql,string strtableName)
          {
                strSQL = strsql;
                da = new SqlDataAdapter(strSQL, conn);
                ds.Clear();
                da.Fill(ds, strtableName);
                return ds;
           }
    }
      

  5.   

    纠正个错误,那个类的方法名多敲了个1,应该是 public DataSet queryDB(string strsql,string strtableName),而不是 public DataSet queryDB1(string strsql,string strtableName)
      

  6.   

    又是类似的问题建立两个表的Relation,使用ado.net表达式来解决。个人认为比较合理。
    http://www.microsoft.com/china/MSDN/library/data/dataAccess/ADONETEXP.mspx