export datagridview to csv
DataTable myTable = new DataTable("components");
DataColumn mydatcolumn;
mydatcolumn = new DataColumn();
mydatcolumn.DataType = System.Type.GetType("System.String");
mydatcolumn.ColumnName = "Component";
myTable.Columns.Add(mydatcolumn);
mydatcolumn = new DataColumn();
mydatcolumn.DataType = System.Type.GetType("System.String");
mydatcolumn.ColumnName = "Service Category";
myTable.Columns.Add(mydatcolumn);
mydatcolumn = new DataColumn();
mydatcolumn.DataType = System.Type.GetType("System.String");
mydatcolumn.ColumnName = "Component Owner";
myTable.Columns.Add(mydatcolumn);
for (int i = 0; i < serviceList.Count; i++)
{
DataRow mydatarow;
//DataRow mydatarow2;
mydatarow = myTable.NewRow();
mydatarow["component"] = ComponentList[i];
//mydatarow2 = myTable.NewRow();
mydatarow["Service Category"] = sc[i];
myTable.Rows.Add(mydatarow);
开发者_如何转开发}
componentRGOdataGridView.DataSource = myTable;
private void addCoButton_Click(object sender, EventArgs e)
{
}
As mention above, I am creating a DataTable with three columns and displaying the items in the 2 List<> (not shown above) on the datagridview using this dataTable. List populate rows for only 2 columns and the third column is for user input. On that button click event I want to write only those row in a file where user is giving input and ignore rest of the rows. How can I achieve this. Many Thanks
Use componentRGOdataGridView.Rows to get all the row in the componentRGOdataGridView.
Then use Cells property from DataGridViewRow class to get the cell's value.
After this you can use your favourite csv writer to make a csv file.
You can try this code to print out cell's value to output window.
foreach (DataGridViewRow item in componentRGOdataGridView.Rows)
{
Debug.Write(item.Cells[0].Value.ToString());
Debug.Write(item.Cells[1].Value.ToString());
Debug.WriteLine(item.Cells[2].Value.ToString());
}
The DataGridView control provides the DataGridView.GetClipboardContent()
method which formats the selected cells as a DataObject
that can be placed into the Windows Clipboard. Then you can retrieve the contents of the Clipboard as a TextDataFormat.CommaSeparatedValue
. This returns the contents of the Clipboard as a CSV formatted string, which can be written to a file. By using the DataGridView's provided methods SelectAll()
you can select the entire DataGridView to copy into the Clipboard. You can even copy the header row too by specifying DataGridView.ClipboardCopyMode
to be equal to the enum DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText
.
Witness the simplicity of following code:
void SaveDataGridViewToCSV(string filename)
{
// Save the current state of the clipboard so we can restore it after we are done
IDataObject objectSave = Clipboard.GetDataObject();
// Choose whether to write header. Use EnableWithoutHeaderText instead to omit header.
dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
// Select all the cells
dataGridView1.SelectAll();
// Copy (set clipboard)
Clipboard.SetDataObject(dataGridView1.GetClipboardContent());
// Paste (get the clipboard and serialize it to a file)
File.WriteAllText(filename,Clipboard.GetText(TextDataFormat.CommaSeparatedValue));
// Restore the current state of the clipboard so the effect is seamless
if(objectSave != null) // If we try to set the Clipboard to an object that is null, it will throw...
{
Clipboard.SetDataObject(objectSave);
}
}
Note that I also took the precaution of saving the contents of the clipboard before I copied the DataGridView, so that I could restore it after I was done.
I believe this method is superior because you are using the framework already provided for you, and not re-inventing the wheel by looping through rows and cells, which is prone to errors.
Hope this solves your problems...
精彩评论