×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

dataGridView + architectural tips for beginner

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
}
}

RE: dataGridView + architectural tips for beginner

first you need to have getWzList return your data

CODE

public DataTable getWzList(int month, int year) 

Then when you execute your query you need to store the data somewhere to return it

CODE

DataTable ldtResults = new DataTable();
SqlDataAdapter lSqlAdap = new SqlDataAdapter();
try
{
     lSqlAdap.SelectCommand = command;
     command.CommandText = selectQuery(month, year);
     lSqlAdap.Fill(ldtResults );
}
catch{...}
finally{... do your clean up of connection and objects ...}
return ldtResults; 

Now bind your grid to the data

CODE

private void button1_Click(object sender, EventArgs e)
{
      Access.accessDocument doc = new Access.accessDocument();
      DataTable ldtResults = doc.getWzList(1, 2012);
     DataGridControlID.DataSource = ldtResults ;
     DataGridControlID.DataBind();
} 

Give this a try


RE: dataGridView + architectural tips for beginner

(OP)
Hi ralphtrent,
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#

public DataTable getWzList(int month, int year)
        {
            connection.Open();

            command = connection.CreateCommand();
            command.Connection = connection;

            try
            {
                adapter = new SqlDataAdapter(selectWzListQuery(month, year), connection);

                DataTable table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                adapter.Fill(table);

                return table;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "UWAGA");
                return null;
            }
            finally
            {
                if (sqlread != null)
                    sqlread.Close();
                
                if (connection != null)
                    connection.Close();
            }

        } 

CODE --> C#

Access.accessDocument doc = new Access.accessDocument();
            
            dbGridView.DataSource = doc.getWzList((int)this.p_month.Value, (int)this.p_year.Value);

            DataGridViewCheckBoxColumn checkBox = new DataGridViewCheckBoxColumn();
            checkBox.HeaderText = "Zaznacz/Odznacz";
            checkBox.Name = "chkBox";
            checkBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dbGridView.Columns.Add(checkBox); 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close