//export header
for (i = 1; i <= this.datagridview1.Columns.Count; i++)
{
ExcelSheet.Cells[3, i] = this.datagridview1.Columns[i - 1].HeaderText;
}
//export data
for (i = 1; i <= this.datagridview1.RowCount; i++)
{
for (j = 1; j <= datagridview1.Columns.Count; j++)
{
ExcelSheet.Cells[i + 3, j] = datagridview1.Rows[i - 1].Cells[j - 1].Value;
}
}
This is exporting all data from datagridview but I don't want to export "Date " column and "checkbox" column
Showing like this
You can use an additional counter for columns to get the proper output (not just empty columns). Also, you can move the logics of "skipping" columns to a function.
int columnsCount = 0;
//export header
for (i = 1; i <= this.datagridview1.Columns.Count; i++)
{
if (SkipColumn(this.datagridview1.Columns[i - 1]))
continue;
columnsCount++;
ExcelSheet.Cells[3, columnsCount] = this.datagridview1.Columns[columnsCount - 1].HeaderText;
}
//export data
for (i = 1; i <= this.datagridview1.RowCount; i++)
{
columnsCount = 0;
for (j = 1; j <= datagridview1.Columns.Count; j++)
{
if (SkipColumn(this.datagridview1.Columns[i - 1]))
continue;
columnsCount++;
ExcelSheet.Cells[i + 3, columnsCount] = datagridview1.Rows[i - 1].Cells[columnsCount - 1].Value;
}
}
// ...
private bool SkipColumn(DataGridViewColumn column)
{
return column.GetType() == typeof(DataGridViewCheckBoxColumn)
|| column.Name == "Date";
}
You can add any logics to SkipColumn. Add any validations, conditions there using DataGridViewColumn properties.
Read about DataGridViewColumn properties at MSDN.
Related
working on a personal project to self develop with c#
i'm currently working on a console app using c# for space invaders - i made the decision to use a 2d array in order to store my game.
my current class for invaders is below, i can "summon" the invaders, move them from left to right, then drop a row and move right to left but they don't iterate back through in the other direction.
public void SummonMovingInvaders(int row, int col, Map[,] spaceInvaders)
{
var checkGameOver = new ConsoleInterface();
while (checkGameOver.GameOverCheck(spaceInvaders) == false)
{
row = 1;
while (spaceInvaders[row, col].Filled == false)
{
//Set up original invaders
for (int i = 1; i < 3; i++)
{
spaceInvaders[1, i].Type = CellType.Invader;
}
for (row = 1; row < 10; row++)
{
for (col = 0; col < 4; col++)
{
for (int j = 3; j > 0; j--)
{
if (j == 0)
{
spaceInvaders[row, col + j].Type = CellType.Empty;
}
else
{
spaceInvaders[row, col + j].Type = spaceInvaders[row, col + j - 1].Type;
}
}
Thread.Sleep(500);
}
//Clear out the row
for (int i = 1; i < spaceInvaders.GetLength(1); i++)
{
spaceInvaders[row, i].Type = CellType.Empty;
}
row++;
for (col = 6; col >= 0; col--)
{
for (int j = 0; j < 2; j++)
{
if (col > 0)
spaceInvaders[row, col + j - 1].Type = CellType.Invader;
else
spaceInvaders[row, col + j].Type = CellType.Invader;
}
Thread.Sleep(500);
for (int j = 1; j <= 2; j++)
{
if (col > 1)
{
spaceInvaders[row, col].Type = CellType.Empty;
spaceInvaders[row, col - 2].Type = spaceInvaders[row, col - 1].Type;
}
else
{
break;
}
}
}
col = 0;
}
}
}
}
I have a console application that retrieves data from a web service. It will write the response on an excel file. My logic is fine with one data row, but if it is more than one, it's need writing on the excel file properly.
int i = 0;
int j = 0;
int k = 0;
foreach (var response in responseList)
{
dt = response;
for (i = 0; i <= dt.Rows.Count - 1; i++)
{
for (j = 0; j <= dt.Columns.Count - 1; j++)
{
xlWorkSheet.Cells[i + 1, j + 1] = dt.Rows[i].ItemArray[j].ToString();
}
}
}
My problem with this code is that the excel rows that are being written are always the same.
Example: first response has 20 rows, second response has 10 rows. But after the loop, my excel only generated 20 rows. Seems like not all the data are not being written. Any ideas why?
EDIT
I tried the code below but still no luck.
int jOffset = 0;
int iOffset = 0;
foreach (var response in responseList)
{
dt = response;
for (i = 0; i <= dt.Rows.Count - 1; i++)
{
for (j = 0; j <= dt.Columns.Count - 1; j++)
{
xlWorkSheet.Cells[i + iOffset + 1, j + jOffset + 1] = dt.Rows[i].ItemArray[j].ToString();
}
}
jOffset++;
iOffset++;
}
try this...
class YourClass
{
//rowOffset as field
int rowOffset = 0;
void YourMethod()
{
//... your other codes here
int i = 0;
int j = 0;
int k = 0;
dt = response;
for (i = 0; i <= dt.Rows.Count - 1; i++)
{
for (j = 0; j <= dt.Columns.Count - 1; j++)
{
xlWorkSheet.Cells[i + iOffset + 1, j + 1] = dt.Rows[i].ItemArray[j].ToString();
}
}
rowOffset += i; //the last 'i' (row) recorded will now be added to your row offset
//...
}
}
Basically, you only need a rowOffset and you should store it as a field to retain its value. Then, every after you finish one loop through the response, you add the last i (row #) to the rowOffset which will be the offset for succeeding loops.
EDIT: correction on rowOffset+=i
The following code is used to save data from an SQL table to excel. The problem is, it does not save table headers if the table is empty.
worksheet4 = workbook.Sheets[4];
worksheet4.Name = "Adjs ";
SQL.DataTable dtAG = new SQL.DataTable();
using (SqlConnection cn1 = new SqlConnection(conStr))
{
using (SqlDataAdapter da4 = new SqlDataAdapter(query4.ToString(), cn1))
{
da4.Fill(dtAG);
}
}
DataColumnCollection dcCollection4 = dtAG.Columns;
for (int i = 1; i < dtAG.Rows.Count + 1; i++)
{
for (int j = 1; j < dtAG.Columns.Count + 1; j++)
{
if (i == 1)
worksheet4.Cells[i, j] = dcCollection4[j - 1].ToString();
else
worksheet4.Cells[i, j] = dtAG.Rows[i - 1][j - 1].ToString();
}
}
Any help would be appreciated.
You could add the columns before you it start filling out the data:
int a = 1;
foreach(DataColumn dc in dtAG.Columns)
{
worksheet4.Cells[1, a] = dc.ColumnName;
a++;
}
and then just start from the next row:
for (int i = 2; i < dtAG.Rows.Count + 1; i++)
{
for (int j = 1; j < dtAG.Columns.Count + 1; j++)
{
worksheet4.Cells[i, j] = dtAG.Rows[i - 1][j - 1].ToString();
}
}
You can set the header values like this: ws.Cells[1, 1].Value = "example";
[i, j] has to be [i+1,j] if not, you over write your headings.
dgvEmployeeTimeSheet.DataSource = OCommonFunctions.SelectData("sp_GP_EmployeeTimeSheetDetailSelectForXLS", "#fromdate", dtpFromDate.Value.ToString("MM/dd/yyyy"), "#todate", dtpTODate.Value.ToString("MM/dd/yyyy"), "#companyid", cmbCompany.SelectedValue.ToString());
data from the above procedure is to be taken in to
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[i + 1, j + 1] = data;
}
}
how do I do it?
I am trying this but nothing get Column name or header in excel.
for (i = 1; i < dataGridView2.Columns.Count + 1; i++)
{
xlWorkSheet.Cells[1, i] = dataGridView2.Columns[i - 1].Name;
}
for (i = 0; i <= dataGridView2.RowCount - 1; i++)
{
for (j = 0; j <= dataGridView2.ColumnCount - 1; j++)
{
DataGridViewCell cell = dataGridView2[j, i];
xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
}
}
Use the HeaderText instead of the ColumnName. This should give you the text which is displayed on the DataGridView.
xlWorkSheet.Cells[1, i] = dataGridView2.Columns[i - 1].HeaderText;