Click here to Skip to main content
15,899,003 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted

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

SQL
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


Hope this helps for allowing DataGridView to show more than 1 table.
 
Share this answer
 
v3
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900