SQL查询字符串填写datagridview [英] SQL query string fill in datagridview

查看:100
本文介绍了SQL查询字符串填写datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用c#,我有两个SQL查询字符串,我想将第一个字符串结果填充到dataGridview列[0]中,将第二个字符串结果填充到dataGridView列[1]中.如何添加此项?

请我帮忙.

问候
weldys

Hi,

I am using c# , I have two SQL Query string and I would like to fill the first string result in to dataGridview column[0] and the secound string result in to dataGridView columns[1]. How can I add this item?

Please I need some help.

Regards
weldys

推荐答案

您好,Weldys,
不幸的是,datagridview一次仅支持绑定到一个表.
如果要在datagridview中查看多个表,您要做的就是创建一个dataTable,其中包含要查看的表.
然后将datagridview的数据源设置为新的dataTable.当然,这些表必须位于同一数据库中.

您可以通过在解决方案资源管理器窗口中双击"DatasetName.xsd"文件来创建新的dataTable.

然后将数据表从工具箱中拖出,并将其配置设置为SQL语句,该语句使用查询生成器或键入的语句从要合并的表中获取数据.

只要确保您正确连接了相关字段,否则您将得到很多重复的数据行.

我发现更容易包含想要的表中的所有字段,然后在属性窗口中的datagridview的"columns.collection"选项中排除不需要的字段.

联接2个表的示例:DataTest =表1,tblLogin =表2

Hi Weldys,
Unforunately the datagridview only supports binding to one table at a time.
If you want more than 1 table viewed in a datagridview all you need to do is create a dataTable that has the tables you want to view.
Then set the datasource of the datagridview to the new dataTable. Of course the tables need to be in the same database.

You can make a new dataTable by double clicking the "DatasetName.xsd" file in your solution explorer window.

Then drag a datatable over from the toolbox and set its configuration to an SQL statement that grabs the data from the tables you want to combine, using either the query builder or a typed out statement.

Just make sure you join the related fields correctly or you''ll end up with many duplicate rows of data.

I find it easier to include all fields from the tables I want then exclude unwanted fields in the "columns.collection" option of the datagridview in the properties window.

Example of Joining 2 tables: DataTest = Table 1, tblLogin = Table 2

SELECT     DataTest.Username, DataTest.Value1, DataTest.Cleared2, DataTest.Value2, DataTest.value3, DataTest.[Entry Date], DataTest.Total,
                      DataTest.GrandTotal, tblLogin.LoginID, tblLogin.UserName AS Expr1, tblLogin.[Password], tblLogin.PassPhrase, tblLogin.PassAnswer
FROM         DataTest, tblLogin
WHERE DataTest.Username = tblLogin.Username



希望这有助于DataGridView显示多个表.



Hope this helps for allowing DataGridView to show more than 1 table.


Hi,
 
thank you for you help, here is my code:
 
string strSQL1;
string strSQL2;  
int volt = 250;
DataTable table = new DataTable();
 while (volt <= 775)
 {
 strSQL1 = " SELECT  COUNT(ID) [column1]  FROM  [tablename]" +
  " WHERE (ID = ''112'') AND (DVolt BETWEEN " + volt + " AND (" + volt + "+ 25)) AND (APower / (RPower) BETWEEN 0.0 AND 0.05)";
 strSQL2 = " SELECT  COUNT(ID)[column2]  FROM  [tablename]" +
  " WHERE (ID = ''112'') AND (DVolt BETWEEN " + volt + " AND (" + volt + "+ 25)) AND (Apower / (RPower) BETWEEN 0.05 AND 0.1)";
  volt = volt + 25;
  using (SqlConnection conn = new SqlConnection(string connection)
  {
  conn.Open();
  
  using (SqlDataAdapter adapter1 = new SqlDataAdapter(strSQL1, conn))
  {
  adapter1 .Fill(table) ;
  
  dataGridView1.DataSource = table ;
  
  }
  using (SqlDataAdapter adapter2  = new SqlDataAdapter(strSQL2, conn))
  {
  adapter2.Fill(table);
  dataGridView1.DataSource = table; 
  }
  conn.close();
 
  }
} 
when I run this code in dataGridview column1 and column2 rows are not proporsional. 
please I need your help on that.
 
Best Regards
weldys


这篇关于SQL查询字符串填写datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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