private void toolStripButtonDelete_Click(object sender, EventArgs e) { DataGridViewRow currentRow = this.dgvHasRowNumDetl.CurrentRow; if (currentRow == null) { DialogBox.ShowError("请选择要删除的行!"); } else { string str = ""; str = currentRow.Cells[0].Value.ToString(); if (DialogBox.ShowQuestion("你真的要删除当前记录!") == DialogResult.Yes) { if (this._iDelete.Delete(Convert.ToInt32(str))) { DialogBox.ShowInfo("删除成功!"); this.dgvHasRowNumDetl.Rows.Remove(currentRow); } else { DialogBox.ShowError("删除失败!"); } } } }
private void ExportToExcel() { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Execl files (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = true; saveFileDialog.Title = "保存为Excel文件"; saveFileDialog.ShowDialog(); if (saveFileDialog.FileName.IndexOf(":") < 0) { return; //被点了"取消" } Stream myStream; myStream = saveFileDialog.OpenFile(); StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0)); string columnTitle = ""; try { //写入列标题 for (int i = 0; i < this.dgvHasRowNumDetl.ColumnCount; i++) { if (i > 0) { columnTitle += "\t"; } columnTitle += this.dgvHasRowNumDetl.Columns[i].HeaderText; } sw.WriteLine(columnTitle); //写入列内容 for (int j = 0; j < this.dgvHasRowNumDetl.Rows.Count; j++) { string columnValue = ""; for (int k = 0; k < this.dgvHasRowNumDetl.Columns.Count; k++) { if (k > 0) { columnValue += "\t"; } if (this.dgvHasRowNumDetl.Rows[j].Cells[k].Value == null) { columnValue += ""; } else { columnValue += this.dgvHasRowNumDetl.Rows[j].Cells[k].Value.ToString().Trim(); } } sw.WriteLine(columnValue); } sw.Close(); myStream.Close(); if (DialogBox.ShowQuestion("导出成功!\n\r是否立即打开此文件") == DialogResult.Yes) { //OPEN; Process proc = Process.Start(saveFileDialog.FileName); } } catch (Exception e) { MessageBox.Show(e.ToString()); } finally { sw.Close(); myStream.Close(); } }