using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
namespace StockCommander
{
class databaseHandle
{
String connectionString = @"Data Source=(localdb)\Projects;Initial Catalog=test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;";
SqlDataAdapter dataAdapter;
DataTable table = new DataTable();
public void GetData(string selectCommand, ref DataGridView dataGrid)
{
try
{
dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
dataGrid.DataSource = table;
dataGrid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}
catch (SqlException)
{
MessageBox.Show("SQL Exception occured...");
}
}
public void GetData(string selectCommand, ref DataGridView dataGrid, int identityColumn)
{
try
{
dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
dataGrid.DataSource = table;
table.Columns[identityColumn].AutoIncrement = true;
table.Columns[identityColumn].AutoIncrementSeed = returnLastId();
dataGrid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}
catch (SqlException)
{
MessageBox.Show("SQL Exception occured...");
}
}
public void updateData()
{
dataAdapter.Update(table);
}
private int returnLastId()
{
return table.Rows.Count + 1;
}
public void deleteRows(IEnumerator rowColl, ref DataGridView dataGrid)
{
while (rowColl.MoveNext())
{
DataGridViewRow currRow = (DataGridViewRow)rowColl.Current;
dataGrid.Rows.Remove(currRow);
}
}
public void setRowReadOnlyRows(ref DataGridView dataGrid,int i)
{
dataGrid.Rows[i].ReadOnly = true;
dataGrid.Rows[i].DefaultCellStyle.BackColor = Color.LightGray;
}
public void setRowReadOnlyRows(ref DataGridView dataGrid)
{
for (int i = 0; i <= dataGrid.RowCount - 2; i++)
{
setRowReadOnlyRows(ref dataGrid, i);
}
}
}
}
No comments:
Post a Comment