DataGridView: Copy complete to clipboard - c#

I have a DataGridView in a .Net application (V4 C# VS2010) & want to copy all the data to the clipboard on the click of a button. No problem -
private void copyToClipboard()
{
dataGridView1.SelectAll();
DataObject dataObj = dataGridView1.GetClipboardContent();
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
}
Problem is that the user might already have some cells, rows etc selected on the DataGrid & I don't really want to change that selection. The above obviously selects everything. I could dataGridView1.ClearSelection(); at the end which is marginally better but still doesn't achieve what's required.
I can save the selected cells:
var mySelectedCells = dataGridView1.SelectedCells;
but how do I get those selected cells reselected on the DataGrid after the copy? Is there an easy way to get the selected cells collection back into the DataGrid? Perhaps there is a better way to get the whole grid copied to the clipboard in the first place without affecting presently selected cells?

I suppose if you just wanted to represent the contents of the cells as text and copy them to the clipboard, tab-delimited, you could do something like:
var newline = System.Environment.NewLine;
var tab = "\t";
var clipboard_string = "";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int i=0; i < row.Cells.Count; i++)
{
if(i == (row.Cells.Count - 1))
clipboard_string += row.Cells[i].Value + newline;
else
clipboard_string += row.Cells[i].Value + tab;
}
}
Clipboard.SetText(clipboard_string);
The output seems pretty similar to that of the GetClipboardContent(), but be careful for any DataGridViewImageColumns or any type that isn't implicitly a string.
Edit: Anthony is correct, use StringBuilder to avoid allocating a new string for every concatenation. The new code:
var newline = System.Environment.NewLine;
var tab = "\t";
var clipboard_string = new StringBuilder();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
if (i == (row.Cells.Count - 1))
clipboard_string.Append(row.Cells[i].Value + newline);
else
clipboard_string.Append(row.Cells[i].Value + tab);
}
}
Clipboard.SetText(clipboard_string.ToString());

Here is a version of the VB code in C# with options to copy headers and to only copy selected rows.
private void CopyDataGridViewToClipboard(DataGridView dgv, bool includeHeaders = true, bool allRows = false)
{
// copies the contents of selected/all rows in a data grid view control to clipboard with optional headers
try
{
string s = "";
DataGridViewColumn oCurrentCol = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
if (includeHeaders)
{
do
{
s = s + oCurrentCol.HeaderText + "\t";
oCurrentCol = dgv.Columns.GetNextColumn(oCurrentCol, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
}
while (oCurrentCol != null);
s = s.Substring(0, s.Length - 1);
s = s + Environment.NewLine; //Get rows
}
foreach (DataGridViewRow row in dgv.Rows)
{
oCurrentCol = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
if (row.Selected || allRows)
{
do
{
if (row.Cells[oCurrentCol.Index].Value != null) s = s + row.Cells[oCurrentCol.Index].Value.ToString();
s = s + "\t";
oCurrentCol = dgv.Columns.GetNextColumn(oCurrentCol, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
}
while (oCurrentCol != null);
s = s.Substring(0, s.Length - 1);
s = s + Environment.NewLine;
}
}
Clipboard.SetText(s);
}
catch (Exception ex)
{
toolStripStatusLabel2.Text = #"Error: " + ex.Message;
}
}

You might want to include the column headers:
private void copyAllToClipboard()
{
var newline = System.Environment.NewLine;
var tab = "\t";
var clipboard_string = new StringBuilder();
int i;
for (i = 0; i < this.Columns.Count - 1; i++)
{
clipboard_string.Append(this.Columns[i].Name);
clipboard_string.Append(tab);
}
clipboard_string.Append(this.Columns[i].Name);
clipboard_string.Append(newline);
foreach (DataGridViewRow row in this.Rows)
{
for ( i = 0; i < row.Cells.Count - 1; i++)
{
clipboard_string.Append(row.Cells[i].Value);
clipboard_string.Append(tab);
}
clipboard_string.Append(row.Cells[i].Value);
clipboard_string.Append(newline);
}
Clipboard.SetText(clipboard_string.ToString());
}

I think below method will exactly do what you want. Just call this method with the DataGridView name at the button click event.
Private Sub CopyDataGridViewToClipboard(ByRef dgv As DataGridView)
Try
Dim s As String = ""
Dim oCurrentCol As DataGridViewColumn 'Get header
oCurrentCol = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible)
Do
s &= oCurrentCol.HeaderText & Chr(Keys.Tab)
oCurrentCol = dgv.Columns.GetNextColumn(oCurrentCol, _
DataGridViewElementStates.Visible, DataGridViewElementStates.None)
Loop Until oCurrentCol Is Nothing
s = s.Substring(0, s.Length - 1)
s &= Environment.NewLine 'Get rows
For Each row As DataGridViewRow In dgv.Rows
oCurrentCol = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible)
Do
If row.Cells(oCurrentCol.Index).Value IsNot Nothing Then
s &= row.Cells(oCurrentCol.Index).Value.ToString
End If
s &= Chr(Keys.Tab)
oCurrentCol = dgv.Columns.GetNextColumn(oCurrentCol, _
DataGridViewElementStates.Visible, DataGridViewElementStates.None)
Loop Until oCurrentCol Is Nothing
s = s.Substring(0, s.Length - 1)
s &= Environment.NewLine
Next 'Put to clipboard
Dim o As New DataObject
o.SetText(s)
Clipboard.SetDataObject(o, True)
Catch ex As Exception
ShowError(ex, Me)
End Try
End Sub

DataGridView columns can be visible/invisible and also can be displayed in different order then the order they were created. This code takes care of both:
public static void CopyGridViewToClipboard(DataGridView gvCopy)
{
if (gvCopy == null) return;
StringBuilder s = new StringBuilder();
int offset = gvCopy.ColumnHeadersVisible ? 1 : 0;
int visibleColumnsCount = 0;
//count visible columns and build mapping between each column and it's display position
Dictionary<int, int> indexMapping = new Dictionary<int, int>();
int currIndex = 0;
int lastFoundMinDisplayIndex = -1;
for (int j = 0; j < gvCopy.ColumnCount; j++)
{
//find min DisplayIndex >= currIndex where column is visible
int minDisplayIndex = 100000;
int minDisplayIndexColumn = 100000;
for (int k = 0; k < gvCopy.ColumnCount; k++)
{
if ((gvCopy.Columns[k].Visible) && (gvCopy.Columns[k].DisplayIndex >= currIndex) && (gvCopy.Columns[k].DisplayIndex > lastFoundMinDisplayIndex))
{
if (gvCopy.Columns[k].DisplayIndex < minDisplayIndex)
{
minDisplayIndex = gvCopy.Columns[k].DisplayIndex;
minDisplayIndexColumn = k;
}
}
}
if (minDisplayIndex == 100000) break;
indexMapping.Add(minDisplayIndexColumn, currIndex);
lastFoundMinDisplayIndex = minDisplayIndex;
currIndex++;
}
visibleColumnsCount = currIndex;
//put data in temp array -- required to position columns in display order
string[,] data = new string[gvCopy.RowCount + offset, visibleColumnsCount];
if (gvCopy.ColumnHeadersVisible)
{
for (int j = 0; j < gvCopy.ColumnCount; j++)
{
if (gvCopy.Columns[j].Visible)
{
data[0, indexMapping[j]] = gvCopy.Columns[j].HeaderText;
}
}
}
for (int i = 0; i < gvCopy.RowCount; i++)
{
for (int j = 0; j < gvCopy.ColumnCount; j++)
{
if (gvCopy.Columns[j].Visible)
{
data[i + offset, indexMapping[j]] = gvCopy[j, i].FormattedValue.ToString();
}
}
}
//copy data
for (int i = 0; i < gvCopy.RowCount + offset; i++)
{
for (int j = 0; j < visibleColumnsCount; j++)
{
s.Append(data[i, j]);
s.Append("\t");
}
s.Append("\r\n");
}
Clipboard.SetDataObject(s.ToString());
}

You should change multiselect property of DataGridView. Here is the code:
private void copyToClipboard()
{
dataGridView1.MultiSelect = True;
dataGridView1.SelectAll();
DataObject dataObj = dataGridView1.GetClipboardContent();
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
}

Related

C# EPPlus not evaluating formula SUM(A3:B3)

I have just started working with EPPlus. i have data table which has some numeric data and formula. when i load data table by EPPlus and save to excel then formula not evaluated i found when i open the same excel file. formula lies in excel cell as string like SUM(A3:B3)
To evaluate formulate i have tried many options of EPPLUS and those are listed here
pack.Workbook.Worksheets["Test"].Calculate();
pack.Workbook.Worksheets["Test"].Cells["A3"].Calculate();
pack.Workbook.Worksheets["Test"].Cells["B3"].Calculate();
ws.Calculate();
Here i am referring my full sample code where formula not working. please have a look and tell me what i need to add in my code to evaluate formula.
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = GetDataTable();
string path = #"d:\EPPLUS_DT_Excel.xlsx";
Stream stream = File.Create(path);
using (ExcelPackage pack = new ExcelPackage())
{
ExcelWorksheet ws = pack.Workbook.Worksheets.Add("Test");
ws.Cells["A1"].LoadFromDataTable(dt, false);
//pack.Workbook.Worksheets["Test"].Calculate();
//pack.Workbook.Worksheets["Test"].Cells["A3"].Calculate();
//pack.Workbook.Worksheets["Test"].Cells["B3"].Calculate();
ws.Calculate();
pack.SaveAs(stream);
stream.Close();
MessageBox.Show("Done");
}
}
public DataTable GetDataTable()
{
string strSum = "", strColName, strImmediateOneUp = "", strImmediateTwoUp = "";
int startsum = 0;
int currow = 0;
bool firstTimeSum = true;
int NumRows = 3;
int NumColumns = 2;
DataTable dt = new DataTable();
for (int col = 0; col < NumColumns; col++)
{
strColName = GenerateColumnText(col);
DataColumn datacol = new DataColumn(strColName, typeof(object));
dt.Columns.Add(datacol);
}
for (int row = 0; row < NumRows; row++)
{
dt.Rows.Add();
for (int col = 0; col < NumColumns; col++)
{
if (row < 2)
{
dt.Rows[row][col] = Convert.ToInt32(new Random().Next(1, NumRows));
}
else
{
if (firstTimeSum)
{
if (row - currow == 2)
{
currow = row;
startsum = 0;
firstTimeSum = false;
}
else
{
startsum = 1;
}
}
else
{
if (row - currow == 3)
{
currow = row;
startsum = 0;
}
}
if (startsum == 0)
{
strColName = GenerateColumnText(col);
strImmediateOneUp = strColName + ((row + 1) - 1).ToString();
strImmediateTwoUp = strColName + ((row + 1) - 2).ToString();
strSum = string.Format("+SUM({0}:{1})", strImmediateTwoUp, strImmediateOneUp);
dt.Rows[row][col] = strSum;
}
else
{
dt.Rows[row][col] = Convert.ToInt32(new Random().Next(1, NumRows));
}
}
}
startsum = 1;
}
return dt;
}
private string GenerateColumnText(int num)
{
string str = "";
char achar;
int mod;
while (true)
{
mod = (num % 26) + 65;
num = (int)(num / 26);
achar = (char)mod;
str = achar + str;
if (num > 0) num--;
else if (num == 0) break;
}
return str;
}
When adding a formula to a cell you use the Formula property. When you load a range from a DataTable using LoadFromDataTable it has no way of knowing that some of the values are meant to be interpreted as formulas.
You can use LoadDataTable to populate the cells on which the formulas will operate, but for the formulas you'll need to set the Formula property.

C# Exporting DataGridView to CSV file

As you can see from the pictures i have attached below i am having some trouble with converting this to a valid csv file. I am nearly there but cant quite figure out the last bit i need. I need to bring the column headers into the textbox and i also need to remove the ',' at the end of each line. BTW i am converting the top DGV.
here is my code -
private void btnSave_Click(object sender, EventArgs e)
{
int countRow = dgvCsv1.RowCount;
int cellCount = dgvCsv1.Rows[0].Cells.Count;
for (int rowIndex = 0; rowIndex <= countRow -1; rowIndex++)
{
for (int cellIndex = 0; cellIndex <= cellCount - 1; cellIndex++)
{
textBoxExport.Text = textBoxExport.Text + dgvCsv1.Rows[rowIndex].Cells[cellIndex].Value.ToString() + ",";
}
textBoxExport.Text = textBoxExport.Text + "\r\n";
}
System.IO.File.WriteAllText(lblFilePath.Text, textBoxExport.Text);
}
Any help greatly appreciated.
This has already been solved here. The LINQ solution does exactly what you want.
var sb = new StringBuilder();
var headers = dgvCsv1.Columns.Cast<DataGridViewColumn>();
sb.AppendLine(string.Join(",", headers.Select(column => "\"" + column.HeaderText + "\"").ToArray()));
foreach (DataGridViewRow row in dgvCsv1.Rows)
{
var cells = row.Cells.Cast<DataGridViewCell>();
sb.AppendLine(string.Join(",", cells.Select(cell => "\"" + cell.Value + "\"").ToArray()));
}
So the final code would look like:
private void btnSave_Click(object sender, EventArgs e)
{
var sb = new StringBuilder();
var headers = dgvCsv1.Columns.Cast<DataGridViewColumn>();
sb.AppendLine(string.Join(",", headers.Select(column => "\"" + column.HeaderText + "\"").ToArray()));
foreach (DataGridViewRow row in dgvCsv1.Rows)
{
var cells = row.Cells.Cast<DataGridViewCell>();
sb.AppendLine(string.Join(",", cells.Select(cell => "\"" + cell.Value + "\"").ToArray()));
}
textBoxExport.Text = sb.ToString();
System.IO.File.WriteAllText(lblFilePath.Text, textBoxExport.Text);
}
Remove last delimiter (comma)
You already have the number of columns ColumnCount, therefore, while looping through the columns, a simple check is needed to see if the currentColumn is “less than” the ColumnCount -1. If the currentColumn IS less than ColumnCount -1, then you need to add the “comma” ,. If currentColumn is NOT “less than” ColumnCount, then this can mean only one thing… this is the last column and instead of adding the comma you want to add a new line.
if (currentCol < ColumnCount - 1) {
textBoxExport.Text += ",";
}
else {
textBoxExport.Text += Environment.NewLine;
}
Column Headers in CSV file
This will need to be separate from looping through the rows. Before looping through the rows, loop through the columns of the grid and get each columns Name property. This would be the first line in the CSV file. You can use the same strategy as above to avoid the last comma.
for (int currentCol = 0; currentCol < ColumnCount; currentCol++) {
textBoxExport.Text += dgvCsv1.Columns[currentCol].Name;
if (currentCol < ColumnCount - 1) {
textBoxExport.Text += ",";
}
else {
textBoxExport.Text += Environment.NewLine;
}
}
Lastly, it is unclear why you are using a TextBox to store the CSV string. I recommend using a StringBuilder. Below is an example of what is described above.
private void btnSave_Click(object sender, EventArgs e) {
StringBuilder sb = new StringBuilder();
int RowCount = dgvCsv1.RowCount;
int ColumnCount = dgvCsv1.ColumnCount;
// get column headers
for (int currentCol = 0; currentCol < ColumnCount; currentCol++) {
sb.Append(dgvCsv1.Columns[currentCol].Name);
if (currentCol < ColumnCount - 1) {
sb.Append(",");
}
else {
sb.AppendLine();
}
}
// get the rows data
for (int currentRow = 0; currentRow < RowCount; currentRow++) {
if (!dgvCsv1.Rows[currentRow].IsNewRow) {
for (int currentCol = 0; currentCol < ColumnCount; currentCol++) {
if (dgvCsv1.Rows[currentRow].Cells[currentCol].Value != null) {
sb.Append(dgvCsv1.Rows[currentRow].Cells[currentCol].Value.ToString());
}
if (currentCol < ColumnCount - 1) {
sb.Append(",");
}
else {
sb.AppendLine();
}
}
}
}
textBoxExport.Text = sb.ToString();
System.IO.File.WriteAllText(#"D:\Test\DGV_CSV_EXPORT.csv", sb.ToString());
}

CSV Export from DataGrid - Wrong Rows

How can I export my datagrid to CSV correctly? Why are the Rows in different columns?
ExportToCsv function return:
Column Headers:
"SR #;8D Report Requested;Status (ASSIST);In R&D;BTQ NUMBER;Priority;Target Date;Implementation Date;Status (BTQ)"
Rows: WRONG!
"1-3271406718;yes;yes;BTQ00153254;6 - Enhancement;22.02.2014;09.09.2014 ;COMPLETED;Eng. wait"
How it should be:
Column Headers:
"SR #;8D Report Requested;Status (ASSIST);In R&D;BTQ NUMBER;Priority;Target Date;Implementation Date;Status (BTQ)"
Rows:
"1-3271406718;yes;Eng. wait;yes;BTQ00153254;6 - Enhancement;22.02.2014;09.09.2014 ;COMPLETED"
here my code:
string CsvFpath = saveDLG.FileName;
StreamWriter csvFileWriter = new StreamWriter(CsvFpath, false);
string columnHeaderText = "";
int countColumn = dgvView.Columns.Count - 1;
if (countColumn >= 0)
{
columnHeaderText = (dgvView.Columns[0].Header).ToString();
}
//Writing column headers
for (int i = 1; i <= countColumn; i++)
{
columnHeaderText = columnHeaderText + ';' + (dgvView.Columns[i].Header).ToString();
}
csvFileWriter.WriteLine(columnHeaderText);
// Writing values row by row
for (int i = 0; i <= dgvView.Items.Count - 2; i++)
{
string dataFromGrid = "";
for (int j = 0; j <= dgvView.Columns.Count - 1; j++)
{
if (j == 0)
{
dataFromGrid = ((DataRowView)dgvView.Items[i]).Row.ItemArray[j].ToString();
}
else
{
dataFromGrid = dataFromGrid + ';' + ((DataRowView)dgvView.Items[i]).Row.ItemArray[j].ToString();
}
}
csvFileWriter.WriteLine(dataFromGrid);
}
csvFileWriter.Flush();
csvFileWriter.Close();
Try it with Linq. It's simpler and easy to read:
public string DataGridToCSV(string delimiter = ";")
{
var sb = new StringBuilder();
var headers = myDataGridView.Columns.Cast<DataGridViewColumn>();
sb.AppendLine(string.Join(delimiter, headers.Select(column => "\"" + column.HeaderText + "\"").ToArray()));
foreach (DataGridViewRow row in myDataGridView.Rows)
{
var cells = row.Cells.Cast<DataGridViewCell>();
sb.AppendLine(string.Join(delimiter, cells.Select(cell => "\"" + cell.Value + "\"").ToArray()));
}
return sb.ToString();
}
Just save the String as *.csv File and you're done.

Is it possible to remove the first element of a datatable cell, which is a string value?

I have a datatable filled with strings.
On a column of these I want to remove the first 2 elements.
""Edit: By element i mean the first 2 characters of the string of a datacell. End of Edit""
Such thing is easy in strings, using the remove command. But is the same possible in a datacell?
PS: Some might answer "Why not define the string first, and then place it on the cell" but I'd rather avoid it, since my cells are defined by a series of concatenations. Here's how I'm doing it.
//create a list of the columns name
List<string> listadecolunas = new List<string>();
foreach (DataColumn coluna in DataAccess.Instance.tabelafinal5.Columns)
{
listadecolunas.Add(coluna.ColumnName);
}
//create a concatenated string that encolsures all the following columns
for (int i = 0; i < DataAccess.Instance.tabelafinal5.Rows.Count; i++)
{
for (int y = 2; y < listadecolunas.Count; y++)
{
string elemento0 = DataAccess.Instance.tabelafinal5.Rows[i][y].ToString();
if (elemento0 != "")
{
DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] = DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] + " + " + elemento0;
}
}
}
I didn't want to fill this with detail, but since I've posted the code, in the end I will get a datacell that begins with + and I want to remove it, it's just that.
Edit2:
My cell looks like this
I wanna remove the first + :)
You can do this with a little helper like that:
for (int i = 0; i < DataAccess.Instance.tabelafinal5.Rows.Count; i++)
{
bool isFirst = true;
for (int y = 2; y < listadecolunas.Count; y++)
{
string elemento0 = DataAccess.Instance.tabelafinal5.Rows[i][y].ToString();
if (elemento0 != "")
{
if(isFirst)
{
isFirst = false;
DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] = DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] + elemento0;
}
else
{
DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] = DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] + " + " + elemento0;
}
}
}
}
A little more efficient with a StringBuilder but same concept:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < DataAccess.Instance.tabelafinal5.Rows.Count; i++)
{
bool isFirst = true;
sb.Clear();
for (int y = 2; y < listadecolunas.Count; y++)
{
string elemento0 = DataAccess.Instance.tabelafinal5.Rows[i][y].ToString();
if (elemento0 != "")
{
if(isFirst)
{
isFirst = false;
}
else
{
sb.Append(" + ")
}
sb.Append(elemento0);
}
}
if(sb.Length > 0)
{
DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] = DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] + sb.ToString();
}
}
Now, if you even construct the Cell across different methods and you do not know what has happened before, you would set isFirst based on if the current value of the cell is empty or not instead of assigning true to it by default.

Writing to text file doesn't match

i have a 10x10 text box ( 100 of them )
I write this code to write into text file :
foreach (Control control in Panel1.Controls)
{
var textBox = control as TextBox;
if (textBox != null)
{
if (string.IsNullOrEmpty(textBox.Text)) // ignore this
{
textBox.Style["visibility"] = "hidden";
}
textBox.Enabled = false;
if (numberofCommas > 8)
{
stringWriter.Write(textBox.Text);
numberofCommas = 0;
}
else
{
stringWriter.Write("," + textBox.Text );
numberofCommas++;
recordsWritten++;
}
if (recordsWritten == 10)
{
stringWriter.WriteLine();
recordsWritten = 0;
}
else
{
}
From the above i want to have 10 rows of 9 commas in the text file but instead i have 9 rows of 10 commas in the text file , is my code logic wrong? because i have been looking it for hours , i still couldn't solve it . sorry if my logic is bad , i am new to programming.
I think that you should increment recordsWritten in the last step:
if (numberofCommas > 8)
{
stringWriter.Write(textBox.Text);
numberofCommas = 0;
recordsWritten++;
}
Here is a better way to do it using Linq:
var textBoxes = Panel1.Controls.OfType<TextBox>().Select((t, i) => new { TextBox = t, Index = i }).ToList();
foreach (var tb in textBoxes)
{
if (string.IsNullOrEmpty(tb.TextBox.Text))
tb.TextBox.Style["visibility"] = "hidden";
tb.TextBox.Enabled = false;
}
foreach (var line in textBoxes.GroupBy(e => e.Index / 10)
.Select(e =>
string.Join(", ",
e.Select(a => a.TextBox.Text).ToArray())))
stringWriter.WriteLine(line);
I wouldn't recommend you to use 100 TextBox objects, you can use a DataGridView binded to a DataTable with 10 rows and 10 columns. You can still edit your data and save it to a file.
Try the below code
StringWriter stringWriter1 = new StringWriter();
DataTable dataTable1 = new DataTable();
private void Form1_Shown(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
int i;
for (i = 0; i < 10; i++)
{
dataTable1.Columns.Add("Column" + (i + 1), typeof(string));
}
for (i = 0; i < 10; i++)
{
DataRow dataRow1 = dataTable1.NewRow();
dataTable1.Rows.Add(dataRow1);
}
dataGridView1.DataSource = dataTable1;
}
private void button1_Click(object sender, EventArgs e)
{
string rowString = "";
int i,j;
for (i = 0; i < 10; i++)
{
rowString = "";
for (j = 0; j < 10; j++)
{
if (dataTable1.Rows[i][j].ToString().Contains(",") == true)
{
//Enclosing the field data inside quotes so that it can
//be identified as a single entity.
rowString += "\"" + dataTable1.Rows[i][j] + "\"" + ",";
}
else
{
rowString += dataTable1.Rows[i][j] + ",";
}
}
rowString = rowString.Substring(0, rowString.Length - 1);
stringWriter1.WriteLine(rowString);
}
}
you just need to add a DataGridView onto your Form.

Categories

Resources