57 lines
2.0 KiB
C#
57 lines
2.0 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Data;
|
||
|
|
using System.Data.OleDb;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Text;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
using System.Windows.Forms;
|
||
|
|
|
||
|
|
namespace Database
|
||
|
|
{
|
||
|
|
abstract class OleDBDataGridViewHandler
|
||
|
|
{
|
||
|
|
protected string nameTable;
|
||
|
|
private int idIndex;
|
||
|
|
public OleDbDataAdapter select;
|
||
|
|
private DataGridView dataGridView;
|
||
|
|
protected OleDBDataGridViewHandler(OleDbCommand selectCommand, string nameTable, DataGridView dataGridView, int idIndex = 0)
|
||
|
|
{
|
||
|
|
this.nameTable = nameTable;
|
||
|
|
this.idIndex = idIndex;
|
||
|
|
select = new OleDbDataAdapter(selectCommand);
|
||
|
|
this.dataGridView = dataGridView;
|
||
|
|
this.dataGridView.DataSource = new DataTable();
|
||
|
|
RefreshView();
|
||
|
|
var lastColIndex = dataGridView.Columns.Count - 1;
|
||
|
|
var lastColumn = dataGridView.Columns[lastColIndex];
|
||
|
|
lastColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
|
||
|
|
}
|
||
|
|
protected OleDBDataGridViewHandler(string nameTable, DataGridView dataGridView, string fields = "*", int idIndex = 0)
|
||
|
|
: this(OleDBHandler.SelectCommand(nameTable, fields), nameTable, dataGridView, idIndex)
|
||
|
|
{ }
|
||
|
|
protected void RefreshView()
|
||
|
|
{
|
||
|
|
var dataTable = this.dataGridView.DataSource as DataTable;
|
||
|
|
dataTable.Clear();
|
||
|
|
select.Fill(dataTable);
|
||
|
|
}
|
||
|
|
protected int CurrentId()
|
||
|
|
{
|
||
|
|
if (dataGridView.SelectedCells.Count < 1)
|
||
|
|
return -1;
|
||
|
|
var selectedRowIndex = dataGridView.SelectedCells[0].RowIndex;
|
||
|
|
var value = dataGridView.Rows[selectedRowIndex].Cells[idIndex].Value;
|
||
|
|
return Convert.ToInt32(value);
|
||
|
|
}
|
||
|
|
public void Delete()
|
||
|
|
{
|
||
|
|
int id = CurrentId();
|
||
|
|
if (id < 0)
|
||
|
|
return;
|
||
|
|
OleDBHandler.Delete(nameTable, id);
|
||
|
|
RefreshView();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|