2010-09-18 32 views
0
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) 
{ 

} 

如上提到,我創建具有三列的DataTable並在使用該DataTable中的datagridview 2列表<>(以上未圖示)顯示的項目。列表僅填充2列,第三列填充用戶輸入。在那個按鈕上單擊事件我想只寫這些行在用戶輸入的文件中,忽略其餘的行。我怎樣才能做到這一點。 許多謝謝出口的datagridview到csv

+0

什麼是你的問題呢?從「OnClick」事件獲取當前行?附加事件到按鈕?或者從給定的'DataRow'創建一個CSV文件? – Abel 2010-09-18 22:31:08

+0

爲datagridview生成行並將其保存在.csv文件中是個問題。 – Ani 2010-09-18 22:33:16

回答

0

使用componentRGOdataGridView.Rows獲取組件RGOdataGridView中的所有row
然後使用DataGridViewRow類的Cells屬性來獲取單元格的值。
之後,您可以使用您最喜愛的csv編寫器製作一個csv文件。

您可以嘗試使用此代碼將單元格的值輸出到輸出窗口。

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()); 
    } 
0

DataGridView控件提供了DataGridView.GetClipboardContent()方法,其格式化所選擇的細胞作爲DataObject可以放置到Windows剪貼板。然後,您可以檢索剪貼板的內容爲TextDataFormat.CommaSeparatedValue。這將剪貼板的內容作爲CSV格式的字符串返回,可以將其寫入文件。通過使用DataGridView提供的方法SelectAll(),您可以選擇整個DataGridView以將其複製到剪貼板中。您甚至可以通過指定DataGridView.ClipboardCopyMode等於枚舉DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText來複制標題行。

證人下面的代碼簡單:

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); 
    } 
} 

請注意,我也很謹慎保存剪貼板中的內容之前,我複製的DataGridView,讓我做後,我能夠恢復它。

我相信這種方法是優越的,因爲您正在使用已經爲您提供的框架,而不是通過循環行和單元格來重新發明輪子,這很容易出錯。

希望這可以解決您的問題...