如何从c#.net中的单个datagridview中的多个表中获取数据 [英] how to fetch data from multiple tables in single datagridview in c#.net

查看:249
本文介绍了如何从c#.net中的单个datagridview中的多个表中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private   void  Form1_Load(对象发​​件人,EventArgs e)
{

OleDbConnection con = new OleDbConnection(constr);
OleDbCommand cmd = new OleDbCommand();

cmd.Connection = con;

cmd.CommandText = select property_name,propertytype_id from tb_property;
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
for int i = 0 ; i < ds.Tables [ 0 ]。Rows.Count; i ++)
{

for int j = 0 ; j < 2 ; j ++)
{
dataGridView1.Rows.Add( new DataGridViewRow());
dataGridView1.Rows [i] .Cells [j] .Value = ds.Tables [ 0 ]。行[i] [j] .ToString() ;
}
}
con.Close();
}

这里我在这个代码中从数据库中获取两个值并在datagridview中显示它们...

现在问题是我的第二个字段从我的数据库中的propertytype_id获取...在我的数据库中有另一个名为tb_property_type的表,并且在该表中有一个属性名称和一个对应于每个id的值...现在在这个datagridview中我想显示对应于该数据库的值另一个表中的propertytype_id ...任何人都可以将我甩掉吗?

解决方案

将查询更改为此,您将能够获得该值。并且你可以在循环中修改。



  选择a.property_name,a.propertytype_id,b.propertytype_value来自tb_property内部联接tb_property_type b 
on a.propertytype_id = b.propertytype_id


private void Form1_Load(object sender, EventArgs e)
        {

            OleDbConnection con = new OleDbConnection(constr);
            OleDbCommand cmd = new OleDbCommand();

            cmd.Connection = con;

            cmd.CommandText = "select  property_name,propertytype_id from tb_property  ";
            cmd.CommandType = CommandType.Text;
            OleDbDataAdapter da = new OleDbDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {

                for (int j = 0; j < 2; j++)
                {
                    dataGridView1.Rows.Add(new DataGridViewRow());
                    dataGridView1.Rows[i].Cells[j].Value = ds.Tables[0].Rows[i][j].ToString();
                }
            }
            con.Close();
}

here in this code i am fetching two values from database and displaying them in datagridview...
now the problem is that the second field that i am fetching from database that is propertytype_id ...in my database there is another table named tb_property_type and in that table there is a property name and a value corresponding to every id...now in this datagridview i want to display value corresponding to the the propertytype_id in the other table...can anyone halp me out??

解决方案

Change your query to this, you will be able to get the value. and you can modify in the loop.

"select  a.property_name,a.propertytype_id , b.propertytype_value  from tb_property a  inner join tb_property_type b
 on a.propertytype_id = b.propertytype_id"


这篇关于如何从c#.net中的单个datagridview中的多个表中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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