如何在Windows应用程序表单中使用datareader或dataset将来自两个不同表的数据绑定到单个datagridview中 [英] how to bind datas from two different tables into a single datagridview using datareader or dataset,in windows application form

查看:87
本文介绍了如何在Windows应用程序表单中使用datareader或dataset将来自两个不同表的数据绑定到单个datagridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..

i希望将来自两个不同表的数据绑定到单个datagridview中。我使用批量选择查询来获取数据,并且可以使用datareader或dataadapter完成绑定。但是我我只能从第一张表中获取数据。如何在这里绑定两个表中的数据。附上下面的代码。请帮助我..

提前致谢。

swathi 。



使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System.Windows.Forms;

使用System.Data.SqlClient;



命名空间WindowsFormsApplication1

{

公共部分类Form1:表格

{

public Form1()

{

InitializeComponent();

bindgrid();



} $ / $
private void bindgrid()

{



SqlConnection con = new SqlConnection(Data Source =( local); Initial Catalog = book; User ID = sa; Password = nest123 @!);

con.Open();

SqlCommand cmd = new SqlCommand(@ 选择ID,名称,作者,销售价格;选择invc_id,总价格从账单,con);

SqlDataAdapter sda = new SqlDataAdapter(cmd);

cmd。 ExecuteNonQuery();

SqlCommandBuilder sc = new SqlCommandBuilder(sda);

DataTable dt = new DataTable(sale,bill);



sda.Fill(dt);

dataGridView1.DataSource = dt;



















}











}

}

Hi..
i want to bind datas from two different tables into a single datagridview.Here i am using batch select query to get datas,and binding can be done using datareader or dataadapter.But i am getting datas from first table only.How can i bind datas from two tables here.Am attaching the code below.Help me please..
Thanks in advance.
swathi.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
bindgrid();

}
private void bindgrid()
{

SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=book;User ID=sa;Password=nest123@!");
con.Open();
SqlCommand cmd = new SqlCommand(@"select id,name,author,price from sale;select invc_id,totalprice from bill", con);
SqlDataAdapter sda=new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
SqlCommandBuilder sc = new SqlCommandBuilder(sda);
DataTable dt = new DataTable("sale", "bill");

sda.Fill(dt);
dataGridView1.DataSource = dt;









}





}
}

推荐答案

这里有几件事:

1.第二个参数不是DataTable。

2.你想对数据表做什么,我看到你从2个表中选择6列。您想在数据网格中显示六列吗?如果是这样,您将必须编写一个连接表的查询,将结果集合并到单个数据表并绑定到网格。
Couple of things here:
1. The second parameter is not DataTable.
2. What do you want to with the data tables, I see you are selecting 6 columns from 2 tables. Do you want to show six columns in the data grid? If so you will have to write a single query joining the tables, get the result set into single data table and bind to the grid.


如果您想在同一个查询中使用六列,您可以使用在Sale和Bill表之间加入。看来这两个表格之间没有关系。所以你可以使用以下查询。

选择 sale.id,sale.name,sale.author,sale.price,bill.invc_id,bill.totalprice 来自 sale,bill
If you want six columns in same query you can use Join between Sale and Bill table. It seems there is no relation between these two tabels. So You can Use the following query.
select sale.id,sale.name,sale.author,sale.price,bill.invc_id, bill.totalprice from sale, bill


这篇关于如何在Windows应用程序表单中使用datareader或dataset将来自两个不同表的数据绑定到单个datagridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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