public static void SQLSendData(string ConnectionString, string CommandString)
{
using (MySqlConnection connection = new MySqlConnection(ConnectionString))
{
try
{
MySqlCommand command = new MySqlCommand(CommandString, connection);
command.Connection = connection;
connection.Open();
rowsAffected = command.ExecuteNonQuery();
}
catch (SqlException e)
{
MessageBox.Show(e.Message);
}
finally
{
connection.Close();
}
}
}
SQLSendData("ConnectionString", "UPDATE blablalbalba");
using ClosedXML.Excel;
...
private void export_excel()
{
string FileName;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "xls files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
saveFileDialog1.Title = "Экспорт в Excel";
saveFileDialog1.FileName = this.Text + " (" + DateTime.Now.ToString("yyyy-MM-dd") + ")";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
FileName = saveFileDialog1.FileName;
var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add(this.Text);
for (int i = 0; i < DG.Columns.Count; i++)
{
worksheet.Cell(1, i + 1).Value = DG.Columns[i].Name;
}
for (int i = 0; i < DG.Rows.Count; i++)
{
for (int j = 0; j < DG.Columns.Count; j++)
{
worksheet.Cell(i + 2, j + 1).Value = DG.Rows[i].Cells[j].Value.ToString();
}
}
worksheet.Columns().AdjustToContents();
workbook.SaveAs(FileName);
MessageBox.Show("Экспорт завершён");
}
}
private void Form_Load(object sender, EventArgs e)
{
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "ID";
dataGridView1.Columns[1].Name = "Наименование";
dataGridView1.Columns[2].Name = "Категория";
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
string ConnStr = SQLCommands.ConnectionString();
string Command = "SELECT * FROM [dbo].[Изделия]";
DataTable Table1 = new DataTable();
Table1 = SQLGetData(ConnStr, Command).Tables[0];
foreach (DataRow row in Table1.Rows)
{
DataGridViewRow[] dgwrow = new DataGridViewRow[1];
dgwrow[0] = GetComboBox();
// Устанавливаем значения из таблицы изделия
try
{
dgwrow[0].Cells[0].Value = row[0].ToString();
dgwrow[0].Cells[1].Value = row[1].ToString();
dgwrow[0].Cells[2].Value = row[2].ToString();
}
catch { }
dataGridView1.Rows.Add(dgwrow[0]);
}
}
private DataGridViewRow GetComboBox()
{
string ConnStr = "ConnectionString";
string Command = "SELECT [Name] FROM [dbo].[Категория]";
DataTable DT = new DataTable();
DT = SQLGetData(ConnStr, Command).Tables[0];
DataGridViewRow roww = new DataGridViewRow();
// создаём комбобокс
DataGridViewComboBoxCell cell_CB = new DataGridViewComboBoxCell();
roww.CreateCells(dataGridView1);
// наполняем из таблицы "категория"
foreach (DataRow row in DT.Rows)
{
Name = row["Name"].ToString();
cell_CB.Items.AddRange(Name);
}
// третья колонка будет комбобокс
roww.Cells[2] = cell_CB;
return roww;
}
public static DataSet SQLGetData(string ConnectionString, string commandString)
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
DataSet DS = new DataSet();
DataTable DT = new DataTable("Table1");
try
{
connection.Open();
SqlCommand command = new SqlCommand(commandString, connection);
SqlDataReader read = command.ExecuteReader();
DS.Tables.Add(DT);
DS.Load(read, LoadOption.PreserveChanges, DS.Tables[0]);
}
catch (SqlException e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
}
finally
{
connection.Close();
}
return DS;
}
}