EDN Admin
Well-known member
I got 2 datgridviews, trying to copy the header and it values to a excel sheet. The first loop is working fine for the first datagirdview<br/>
<br/>
The first datagird has 4 columns.<br/>
<br/>
<pre class="prettyprint for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[7, i] = dataGridView1.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 8, j + 1] = dataGridView1.Rows.Cells[j].Value.ToString();
}
}[/code]
<br/>
<br/>
This is the second datagridview and Im trying to copy the header and its values.<br/>
<br/>
The first datagridview header values finished at 7nth row cell `D7`and the row values finished at 8th row cell `D8`. <br/>
<br/>
I want to put the second datagridview header from `E7` of 7nth row and the row values from `E8` of 8th row.<br/>
<br/>
The second datagrid had 3 cols. Somebody please help me to solve this. Thank you<br/>
<br/>
<pre class="prettyprint for (int i = 7; i < dataGridView2.Columns.Count + 1; i++)
{
worksheet.Cells[7, i] = dataGridView2.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView2.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView2.Columns.Count; j++)
{
worksheet.Cells[i + 8, j + 1] = dataGridView2.Rows.Cells[j].Value.ToString();
}
}[/code]
<br/>
View the full article
<br/>
The first datagird has 4 columns.<br/>
<br/>
<pre class="prettyprint for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[7, i] = dataGridView1.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 8, j + 1] = dataGridView1.Rows.Cells[j].Value.ToString();
}
}[/code]
<br/>
<br/>
This is the second datagridview and Im trying to copy the header and its values.<br/>
<br/>
The first datagridview header values finished at 7nth row cell `D7`and the row values finished at 8th row cell `D8`. <br/>
<br/>
I want to put the second datagridview header from `E7` of 7nth row and the row values from `E8` of 8th row.<br/>
<br/>
The second datagrid had 3 cols. Somebody please help me to solve this. Thank you<br/>
<br/>
<pre class="prettyprint for (int i = 7; i < dataGridView2.Columns.Count + 1; i++)
{
worksheet.Cells[7, i] = dataGridView2.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView2.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView2.Columns.Count; j++)
{
worksheet.Cells[i + 8, j + 1] = dataGridView2.Rows.Cells[j].Value.ToString();
}
}[/code]
<br/>
View the full article