Why would DataGrid preserves the original order/format?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span style="font-family:Times New Roman,serif I’m using this code to pull data from an Excel file into a DataGrid:
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
<span style="color:blue private <span style="color:blue void button1_Click(<span style="color:blue object sender,
<span style="color:#2B91AF EventArgs e)
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
{
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
<span style="color:#2B91AF DataTable table = <span style="color:blue
new <span style="color:#2B91AF DataTable();
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
<span style="color:blue string strConn = <span style="color:blue
string.Format(<span style="color:#A31515 "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text"",
<span style="color:#A31515 "C:\Users\Excel\Desktop\Coding\DOT.NET\Samples C#\Export DataGridView to SQL Server Table\Import_List.xls");
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
<span style="color:blue using (<span style="color:#2B91AF OleDbConnection dbConnection =
<span style="color:blue new <span style="color:#2B91AF OleDbConnection(strConn))
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
<span> {
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
<span style="color:blue using (<span style="color:#2B91AF OleDbDataAdapter dbAdapter =
<span style="color:blue new <span style="color:#2B91AF OleDbDataAdapter(<span style="color:#A31515 "SELECT * FROM [Sheet1$]", dbConnection))
<span style="color:green //rename sheet if required!
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
dbAdapter.Fill(table);
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
dataGridView1.DataSource = table;
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
<span> <span style="color:blue int rows = table.Rows.Count;
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
}
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif <span>
}
<span style="font-family:Times New Roman,serif
Excel View:
<img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/187999
DataGrid View:<span style="font-family:Times New Roman,serif
<span style="font-family:Times New Roman,serif <img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/188000
<span style="font-family:Times New Roman,serif The code definitely imports the data, as I would expect it to, but the order of the column order is so weird!!<span>
At first, I had Fname, Lname, and Age.<span> Then I added Key (in ColumnA of the Excel Sheet) and “Nothing” (in ColumnD of the Excel Sheet); I put Age in ColumnE of the Excel Sheet.<span>
It seems like the DataGrid preserves the original order of the Excel Sheet, and doesn’t really import the data from the Excel Sheet in the actual order of the data in the Excel Sheet.<span>
I even deleted dataGridView1, and put in a new dataGridView1 object.<span>
It did the same thing!!<span> It’s like the DataGrid preserves the original order/format.<span>
Is this the way it is supposed to work?<span> Is this the desired functionality?
<span> Why would a developer actually want this?<span> This seems so weird!!<span>

<span style="font-family:Times New Roman,serif
<
Ryan Shuell
<br/>

View the full article
 
Back
Top