GridView - Join 2 DataTableshttp://forums.asp.net/t/1795960.aspx/1?GridView+Join+2+DataTablesTue, 01 May 2012 15:28:46 -040017959604946591http://forums.asp.net/p/1795960/4946591.aspx/1?GridView+Join+2+DataTablesGridView - Join 2 DataTables <p>Hi, I have a two grid views from two different SQL statements&nbsp;which I want to try join together.</p> <p>Both have the same 1st column, the persons name, but the second column in both DataTables are differente</p> <p>so DataTable 1 looks like...</p> <p><strong>Name, Value</strong><br> John Smith, 1<br> Jane Doe, 2<br> Tom Jones, 3&nbsp;</p> <p>DataTable 2 looks like...</p> <p><strong>Name, Value</strong><br> John Smith, A<br> Jane Doe, B<br> Tom Jones, C</p> <p>Is there anyway I could make the DataTable like this to make a one GridView of the two...</p> <p><strong>Name, Value</strong><br> John Smith, A, 1<br> Jane Doe, B, 2<br> Tom Jones, C, 3&nbsp;</p> <p></p> <p>Thanks very much</p> <p>&nbsp;</p> 2012-04-23T13:58:49-04:004946605http://forums.asp.net/p/1795960/4946605.aspx/1?Re+GridView+Join+2+DataTablesRe: GridView - Join 2 DataTables <p>ofcours you can just use this query and bind it to yor gridview</p> <p>select a.Name , &nbsp;b.Value ,a.value from Table1 as a inner join Table2 as b on a.Name = b.Name&nbsp;</p> 2012-04-23T14:08:29-04:004946666http://forums.asp.net/p/1795960/4946666.aspx/1?Re+GridView+Join+2+DataTablesRe: GridView - Join 2 DataTables <p>Thanks for your reply,</p> <p>i'm sorry im a bit of a noob at this part of asp. would you be able to help with more explanation or a simple example</p> <p>thank you</p> 2012-04-23T14:34:40-04:004946717http://forums.asp.net/p/1795960/4946717.aspx/1?Re+GridView+Join+2+DataTablesRe: GridView - Join 2 DataTables <p></p> <blockquote><span class="icon-blockquote"></span> <h4>danieldunn10</h4> <p></p> <p>Thanks for your reply,</p> <p>i'm sorry im a bit of a noob at this part of asp. would you be able to help with more explanation or a simple example</p> <p>thank you</p> <p></p> </blockquote> <p></p> <p>Hope this helps:</p> <pre class="prettyprint">var querable = (from ds1 in DataSource1 join ds2 in DataSource2 on ds1.Name equals ds2.Name select new { ds1.Name, ds1.OtherColumn1, ds1.OtherColumn12 }).SingleOrDefault(); GridView1.DataSource = querable; GridView1.DataBind();</pre> <p><br> <br> </p> <p></p> 2012-04-23T14:57:18-04:004946738http://forums.asp.net/p/1795960/4946738.aspx/1?Re+GridView+Join+2+DataTablesRe: GridView - Join 2 DataTables <p>Thanks 2pac, i think that is what I am after, I'm just a little unsure what to use instead of ds1 / ds2 and datasource1 / datasource2. Below my two sql codes I have</p> <pre class="prettyprint">dbcomm = New OdbcCommand(sql, MyConnection) Dim dt As DataTable = New DataTable() dbcomm.Fill(dt)</pre> <p>and</p> <pre class="prettyprint"> dbcomm2 = New OdbcCommand(sql2, MyConnection) Dim dt2 As DataTable = New DataTable() dbcomm2.Fill(dt2)</pre> <p>What should mine look like?</p> <p>My columns are <strong>FeeEarnerName</strong>, <strong>Value1</strong> (in table 1), <strong>Value2</strong> (in table 2)<br> <br> Thanks very much, sorry for the simple questions!</p> <p></p> 2012-04-23T15:08:55-04:004946751http://forums.asp.net/p/1795960/4946751.aspx/1?Re+GridView+Join+2+DataTablesRe: GridView - Join 2 DataTables <p>You could try just one SQL statement instead. Something like:</p> <p>SELECT t1.FeeEarnerName, t1.Value1, t2.Value2<br> FROM table1 t1<br> INNER JOIN table2 t2 ON t1.FeeEarnerName = t2.FeeEarnerName</p> <p>And then just use that one datatable for your single gridview.</p> <p></p> 2012-04-23T15:14:42-04:004946776http://forums.asp.net/p/1795960/4946776.aspx/1?Re+GridView+Join+2+DataTablesRe: GridView - Join 2 DataTables <p>Thanks ksqcoder, can i do a SQL statement on my two datatables?</p> 2012-04-23T15:24:52-04:004946967http://forums.asp.net/p/1795960/4946967.aspx/1?Re+GridView+Join+2+DataTablesRe: GridView - Join 2 DataTables <p>You could use Linq on the two datatables like the example from 2pac. The code I suggested would be for another OdbcCommand. If you just wanted the one GridView instead of the two I would just go that route and use the one OdbcCommand and one GridView. If you still needed the two GridViews along with a new merged GridView then you could use Linq. A Linq query might look like:</p> <p>var results = from t in dt1.AsEnumerable()<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; join s in dt2.AsEnumerable() on t.FeeEarnerName equals s.FeeEarnerName<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; select t.FeeEarnerName, t.Value1, s.Value2;<br> <br> GridViewBoth.DataSource = results;</p> <p>Or just make three database calls, two for your original GridViews and a third returning the joined data from both tables.</p> 2012-04-23T17:26:53-04:004947698http://forums.asp.net/p/1795960/4947698.aspx/1?Re+GridView+Join+2+DataTablesRe: GridView - Join 2 DataTables <p>hey ,, you can have only one gridview with one sql statment just write the sql statment instead the others and do the binding to the gridview</p> <p>so you will got a one gridview with the values you want ,, no need for two</p> 2012-04-24T05:57:54-04:004947826http://forums.asp.net/p/1795960/4947826.aspx/1?Re+GridView+Join+2+DataTablesRe: GridView - Join 2 DataTables <p>Thanks for that, I am using this code below</p> <pre class="prettyprint">dbcomm = New OdbcDataAdapter(sql, MyConnection) Dim dt As DataTable = New DataTable() dbcomm.Fill(dt) dbcomm = New OdbcDataAdapter(sql2, MyConnection) Dim dt2 As DataTable = New DataTable() dbcomm.Fill(dt2) Dim results = From t In dt.AsEnumerable() Join s In dt2.AsEnumerable() On t.FeeEarner Equals s.FeeEarner Select t.FeeEarner, t.CurrentValue, s.CurrentValue2 Employment.DataSource = results Employment.DataBind()</pre> <p>I cant understand why its not working though! &nbsp;In my SQL statement I have a column called FeeEarner.</p> <p>But it underlines t.FeeEarne, s.FeeEarner,&nbsp;t.CurrentValue, s.CurrentValue2&nbsp;and says...</p> <p>'FeeEarner' is not a member of system.data.datarow</p> <p>THanks for any advice</p> 2012-04-24T06:57:27-04:004949928http://forums.asp.net/p/1795960/4949928.aspx/1?Re+GridView+Join+2+DataTablesRe: GridView - Join 2 DataTables <p>I've tried using the merge() method instead which nearly achieves what I want.&nbsp;I've used...</p> <pre class="prettyprint">dbcomm = New OdbcDataAdapter(sql, MyConnection) Dim dt As DataTable = New DataTable() dbcomm.Fill(dt) dbcomm = New OdbcDataAdapter(sql2, MyConnection) Dim dt2 As DataTable = New DataTable() dbcomm.Fill(dt2) dt.Merge(dt2, False, MissingSchemaAction.Add) Employment.DataSource = dt Employment.DataBind()</pre> <p>The results are like this though</p> <p>Parson1, 1,&nbsp;<em>null</em><br> Person2, 2,&nbsp;<em>null</em><br> Person3, 3,&nbsp;<em>null</em><br> Person1,&nbsp;<em>null</em>, A<br> Person2,&nbsp;<em>null</em>, B<br> Person3,&nbsp;<em>null</em>, C&nbsp;</p> <p>Is there anyway to combine them?</p> <p>Thanks!</p> 2012-04-25T06:29:56-04:004950166http://forums.asp.net/p/1795960/4950166.aspx/1?Re+GridView+Join+2+DataTablesRe: GridView - Join 2 DataTables <p>Hi,</p> <p>Why not execute the above inner sql query to return a new datatable with two tables' data and bind to GridView. It is the same to you bind one DataTable to one GridView. Just change a select sql statement.</p> <p>Thanks,</p> 2012-04-25T08:00:38-04:004950208http://forums.asp.net/p/1795960/4950208.aspx/1?Re+GridView+Join+2+DataTablesRe: GridView - Join 2 DataTables <p>why are you doing this you can do it in another way</p> <p>instead of using to sql just use 1 sql statement and dorictly bind it to you gridview</p> <p>as following</p> <p>your sql will be&nbsp;</p> <p>&quot;select t.FeeEarner , t.CurrentValue , s.CurrentValue2 &nbsp;from table1 as t inner join table2 as s on t.FeeEarner = s.FeeEarner&quot;</p> <p>then&nbsp;</p> <p>dbcomm =&nbsp;<span class="typ">New</span><span class="pln"> </span><span class="typ">OdbcDataAdapter</span><span class="pun">(</span><span class="pln">sql</span><span class="pun">,</span><span class="pln"> </span><span class="typ">MyConnection</span><span class="pun">)</span></p> <pre class="prettyprint">Dim dt As DataTable = New DataTable() dbcomm.Fill(dt)</pre> <pre class="prettyprint"><span class="typ">Employment</span><span class="pun">.</span><span class="typ">DataSource</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> dataTable&nbsp;</span></pre> <pre class="prettyprint"><span class="typ">Employment</span><span class="pun">.</span><span class="typ">DataBind</span><span class="pun">()</span></pre> <pre class="prettyprint"><span class="pun"><br /></span></pre> <pre class="prettyprint">that's it ,,, that what was iam telling you in my previous post </pre> <pre class="prettyprint"><span class="pun"><br /></span></pre> <pre class="prettyprint"><span class="pun"><br /></span></pre> <p><span class="pun"><br> </span></p> 2012-04-25T08:21:25-04:004951131http://forums.asp.net/p/1795960/4951131.aspx/1?Re+GridView+Join+2+DataTablesRe: GridView - Join 2 DataTables <p>If it is from 2 databases&nbsp;using 2 SqlConnections and sqlCommands Fill 2 dataTables. Picking field values from these 2 datatables fill a third Datatatable. Set this as the datasource&nbsp; and Databind the GridView</p> 2012-04-25T14:54:34-04:004955969http://forums.asp.net/p/1795960/4955969.aspx/1?Re+GridView+Join+2+DataTablesRe: GridView - Join 2 DataTables <p>Thanks&nbsp;<a title="basheerkal" href="http://forums.asp.net/members/basheerkal.aspx">basheerkal</a>, that sounds like what I am looking for!</p> <p>How would the code look for that?</p> <p>THanks very much</p> 2012-04-28T08:20:15-04:004960165http://forums.asp.net/p/1795960/4960165.aspx/1?Re+GridView+Join+2+DataTablesRe: GridView - Join 2 DataTables <p>Try like this..this is a working example. May be some other methods are there&nbsp;to do this ... this is only one of them</p> <pre class="prettyprint">void GvFill() { string constr = ConfigurationManager.ConnectionStrings[&quot;TestConnectionString&quot;].ConnectionString; string qry1 = &quot;SELECT * FROM table_1&quot;; string qry2 = &quot;SELECT * FROM table_2&quot;; SqlConnection conn = new SqlConnection(constr); conn.Open(); SqlCommand cmd1 = new SqlCommand(qry1, conn); SqlCommand cmd2 = new SqlCommand(qry2, conn); SqlDataAdapter da1 = new SqlDataAdapter(cmd1); SqlDataAdapter da2 = new SqlDataAdapter(cmd2); DataTable dt1 = new DataTable(); DataTable dt2 = new DataTable(); da1.Fill(dt1); da2.Fill(dt2); int Counter = 0; dt2.Columns.Add(&quot;Value1&quot;); foreach (DataRow tr in dt2.Rows) { dt2.Rows[Counter][2] = dt1.Rows[Counter][1].ToString(); Counter=Counter&#43;1; } GridView2.DataSource = dt2; GridView2.DataBind(); conn.Close(); } protected void Button1_Click(object sender, EventArgs e) { GvFill(); }</pre> <p>This the gridView in my form</p> <pre class="prettyprint">&lt;asp:GridView ID="GridView2" runat="server"&gt;&lt;/asp:GridView&gt;</pre> <p>&nbsp;</p> 2012-05-01T15:28:46-04:00