Note that there are some explanatory texts on larger screens.

plurals
  1. PONot retrieving data from Gridview to Excel
    text
    copied!<p>The only display in the excel file is the column name and not the data came from the Gridview.</p> <p>Here are my Functions</p> <pre><code> private void ToCsV(DataGridView dGV, string filename) { string stOutput = ""; // Export titles: string sHeaders = ""; for (int j = 0; j &lt; dGV.Columns.Count; j++) sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t"; stOutput += sHeaders + "\r\n"; // Export data. for (int i = 0; i &lt; dGV.RowCount - 1; i++) { string stLine = ""; for (int j = 0; j &lt; dGV.Rows[i].Cells.Count; j++) stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t"; stOutput += stLine + "\r\n"; } Encoding utf16 = Encoding.GetEncoding(1254); byte[] output = utf16.GetBytes(stOutput); FileStream fs = new FileStream(filename, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); bw.Write(output, 0, output.Length); //write the encoded file bw.Flush(); bw.Close(); fs.Close(); } </code></pre> <p>Function that called the data from SQL</p> <pre><code> private void Details() { DataTable dt = new DataTable(); SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM Customer WHERE CustomerID like " + txtCustomerID.Text, cn); adp.Fill(dt); gvHistory.DataSource = dt; } </code></pre> <p>This is my code in button to save as excel file the data from GridView.</p> <pre><code>private void button2_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Excel Documents (*.xls)|*.xls"; sfd.FileName = ""; if (sfd.ShowDialog() == DialogResult.OK) { //ToCsV(dataGridView1, @"c:\export.xls"); ToCsV(gvHistory, sfd.FileName); // Here dataGridview1 is your grid view name } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload