DataTable and it's Gridview - A Column Order Questionhttp://forums.asp.net/t/1612209.aspx/1?DataTable+and+it+s+Gridview+A+Column+Order+QuestionThu, 14 Oct 2010 21:18:55 -040016122094122099http://forums.asp.net/p/1612209/4122099.aspx/1?DataTable+and+it+s+Gridview+A+Column+Order+QuestionDataTable and it's Gridview - A Column Order Question <p>Well, Ive been developing with gridviews for quite some time now.&nbsp; Just recently I came across a situation that has me stumped.&nbsp; Heres how the gridview has been working for me.</p> <p>&nbsp;</p> <p>1.&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;A stored procedure from the database is called (three parameters are passed (<i>reporttype, name, location</i>)</p> <p>2.&nbsp;&nbsp;&nbsp;&nbsp; The data is retrieved using the SqlDataAdapter to fill a Datatable</p> <p>3.&nbsp;&nbsp;&nbsp;&nbsp; The gridview DOES NOT AUTOGENERATE COLUMNS</p> <p>4.&nbsp;&nbsp;&nbsp;&nbsp; Columns are created programmatically as it reads the DataTable results (headertext, datafield, and dataformatstring are set at runtime)</p> <p>5.&nbsp;&nbsp;&nbsp;&nbsp; The Gridview triggers the databind event and voila it works.</p> <p>&nbsp;</p> <p>My dilemma is this:&nbsp; the stored procedure (that I havent developed) returns a various set of columns and column order.&nbsp; I have one report type that does the following:</p> <p>&nbsp;</p> <p>ReportType 1:&nbsp; column orders returned in the following order when running directly in SQL</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>col B, col C, col D, col E, col A</b></p> <p>&nbsp;</p> <p>when I run the report through my gridview, the column order is as follows:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>col A, col B, col C, col D, col E</b></p> <p>&nbsp;</p> <p>I see that the columns are being filled into the datatable (in codebehind) in the following order:&nbsp; <b>col A, col B, col C, col D, col E</b></p> <p>&nbsp;</p> <p><b>&gt;&gt;&nbsp; </b>My question is why would the datatable be filling the columns in that order, when the stored procedure returns the table back with:&nbsp; col B, col C, col D, col E, col A</p> <p>&nbsp;</p> <p>Your thoughts would be appreciated!</p> 2010-10-12T18:16:43-04:004122178http://forums.asp.net/p/1612209/4122178.aspx/1?Re+DataTable+and+it+s+Gridview+A+Column+Order+QuestionRe: DataTable and it's Gridview - A Column Order Question <p>It depends on how you are adding the column names. DataTable&nbsp;Column order will&nbsp;be same as the column&nbsp;order in the query.</p> <pre style="font-family:consolas"><span style="color:#2b91af">SqlConnection</span>&nbsp;conn&nbsp;=&nbsp;<span style="color:blue">new</span>&nbsp;<span style="color:#2b91af">SqlConnection</span>(<span style="color:#2b91af">ConfigurationManager</span>.ConnectionStrings[<span style="color:#a31515">&quot;ConString&quot;</span>].ConnectionString);<br><span style="color:#2b91af">SqlDataAdapter</span>&nbsp;adap&nbsp;=&nbsp;<span style="color:blue">new</span>&nbsp;<span style="color:#2b91af">SqlDataAdapter</span>(<span style="color:#a31515">&quot;Select&nbsp;[Col Z], [Col A]&nbsp;from&nbsp;TestTable&quot;</span>,&nbsp;conn);<br><span style="color:#2b91af">DataSet</span>&nbsp;ds&nbsp;=&nbsp;<span style="color:blue">new</span>&nbsp;<span style="color:#2b91af">DataSet</span>();<br>adap.Fill(ds);<br><span style="color:#2b91af">GridView</span>&nbsp;GridView1&nbsp;=&nbsp;<span style="color:blue">new</span>&nbsp;<span style="color:#2b91af">GridView</span>();<br>GridView1.AutoGenerateColumns&nbsp;=&nbsp;<span style="color:blue">false</span>;<br><span style="color:blue">foreach</span>&nbsp;(<span style="color:#2b91af">DataColumn</span>&nbsp;col&nbsp;<span style="color:blue">in</span>&nbsp;ds.Tables[0].Columns)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af">BoundField</span>&nbsp;bf&nbsp;=&nbsp;<span style="color:blue">new</span>&nbsp;<span style="color:#2b91af">BoundField</span>();<br>&nbsp;&nbsp;&nbsp;&nbsp;bf.DataField&nbsp;=&nbsp;col.ColumnName;<br>&nbsp;&nbsp;&nbsp;&nbsp;bf.HeaderText&nbsp;=&nbsp;col.ColumnName;<br>&nbsp;&nbsp;&nbsp;&nbsp;GridView1.Columns.Add(bf);<br>}<br>GridView1.DataSource&nbsp;=&nbsp;ds.Tables[0];<br>GridView1.DataBind();<br>Page.Form.Controls.Add(GridView1);</pre> 2010-10-12T19:19:18-04:004122218http://forums.asp.net/p/1612209/4122218.aspx/1?Re+DataTable+and+it+s+Gridview+A+Column+Order+QuestionRe: DataTable and it's Gridview - A Column Order Question <p>Right after the datatable is filled, I checked it's column order and it's not the same as what is generated from the sproc.<br> </p> 2010-10-12T20:12:05-04:004122244http://forums.asp.net/p/1612209/4122244.aspx/1?Re+DataTable+and+it+s+Gridview+A+Column+Order+QuestionRe: DataTable and it's Gridview - A Column Order Question <p>Honestly, I have used that many times and never noticed any issues. Command or Stored Procedure, it returned same column order. I am not sure if there is any configuration that you have to check.</p> 2010-10-12T20:29:23-04:004125651http://forums.asp.net/p/1612209/4125651.aspx/1?Re+DataTable+and+it+s+Gridview+A+Column+Order+QuestionRe: DataTable and it's Gridview - A Column Order Question <p>sansan,</p> <p>It was a configuration issue - I was pointing to a different instance of the database.&nbsp; Talk about a rookie mistake!<br> </p> 2010-10-14T21:18:55-04:00