dataGridView + architectural tips for beginner
dataGridView + architectural tips for beginner
(OP)
Hi everybody,
first of all I`s like to point that I`m a C# beginner and I would like to learn from scrach using good practices. What I need to do is:
1. Query databese
2. Return results in dataGridView ( 1st or last column should contain a checkbox for further use ). Of course I can do this in one class ( like almost all tutorials ) but I would like to learn how to split and manage everything in this enviroment
So, what I`ve done so far:
1. I`ve added 3 folders: Access, Settings, Tools
2. In Tools
namespace Tools
{
class connectDB
{
protected SqlDataReader sqlread;
protected SqlDataAdapter sqladapter;
protected DataSet ds;
protected SqlConnection connection;
protected SqlCommand command;
}
}
3. In Settings
namespace Settings
{
class vars
{
public static string connect_string = "Server=ip;Database=myDbName;User Id=myUser;Password=myPass";
}
}
4. In Access
class accessDocument : Tools.connectDB
{
public accessDocument()
{
connection = new SqlConnection(Settings.vars.connect_string);
}
public void getWzList(int month, int year)
{
connection.Open();
command = connection.CreateCommand();
command.Connection = connection;
try
{
command.CommandText = selectQuery(month, year);
sqlread = command.ExecuteReader();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "UWAGA");
}
finally
{
if (connection != null)
{
connection.Close();
}
}
}
private string selectQuery(int month, int year)
{
string sqlStatement = "";
sqlStatement = "SELECT * " +
"FROM cdn.testTable " +
"WHERE Typ = 2001 and rok = " + year + " and MONTH(dbo.toDate(dataWyst, 0)) = " + month;
return sqlStatement;
}
}
and finally I`m trying to use it like this
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Access.accessDocument doc = new Access.accessDocument();
doc.getWzList(1, 2012);
// and I don`t know what to do, of course grid is empty
}
}
first of all I`s like to point that I`m a C# beginner and I would like to learn from scrach using good practices. What I need to do is:
1. Query databese
2. Return results in dataGridView ( 1st or last column should contain a checkbox for further use ). Of course I can do this in one class ( like almost all tutorials ) but I would like to learn how to split and manage everything in this enviroment
So, what I`ve done so far:
1. I`ve added 3 folders: Access, Settings, Tools
2. In Tools
namespace Tools
{
class connectDB
{
protected SqlDataReader sqlread;
protected SqlDataAdapter sqladapter;
protected DataSet ds;
protected SqlConnection connection;
protected SqlCommand command;
}
}
3. In Settings
namespace Settings
{
class vars
{
public static string connect_string = "Server=ip;Database=myDbName;User Id=myUser;Password=myPass";
}
}
4. In Access
class accessDocument : Tools.connectDB
{
public accessDocument()
{
connection = new SqlConnection(Settings.vars.connect_string);
}
public void getWzList(int month, int year)
{
connection.Open();
command = connection.CreateCommand();
command.Connection = connection;
try
{
command.CommandText = selectQuery(month, year);
sqlread = command.ExecuteReader();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "UWAGA");
}
finally
{
if (connection != null)
{
connection.Close();
}
}
}
private string selectQuery(int month, int year)
{
string sqlStatement = "";
sqlStatement = "SELECT * " +
"FROM cdn.testTable " +
"WHERE Typ = 2001 and rok = " + year + " and MONTH(dbo.toDate(dataWyst, 0)) = " + month;
return sqlStatement;
}
}
and finally I`m trying to use it like this
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Access.accessDocument doc = new Access.accessDocument();
doc.getWzList(1, 2012);
// and I don`t know what to do, of course grid is empty
}
}
RE: dataGridView + architectural tips for beginner
CODE
Then when you execute your query you need to store the data somewhere to return it
CODE
Now bind your grid to the data
CODE
Give this a try
RE: dataGridView + architectural tips for beginner
thank you for your answer. It was very useful,
I change my code concerning your tips trying to add checkboxex to each row -> succesfully.
Now I would like to know how to get the ID ( or ID`s ) of marked row ( id is returned from the query ).
CODE --> C#
CODE --> C#