3

currently i'm studying MySQL, sorry for my newbie question. Here my question

I have 2 class, the first one is Windows Form, and the other is just a class which I want contain all processes from SQL. here's my code in SQL Class

public void SampleQuery(string tablename)
    {
        DataTable dataTable;

        string query = "SELECT * FROM " + tablename;

        //Open connection
        if (this.OpenConnection() == true)
        {
            adapter = new MySqlDataAdapter(query,connection);

            dataTable =  new DataTable();
            adapter.Fill(dataTable);

            //close Connection
            this.CloseConnection();
            return;
        }

What I want to do is, show the data from SQL with DataGridView. Is it possible to code the datagridview in SQL Class above? or must be in Windows Form part?

if must be in windows form, how to throw the dataTable which is now hold all the data to Windows Form class?

thanks a lot for helping me. If my question sounds absurd just ask me.

user7116
  • 63,008
  • 17
  • 141
  • 172
Reza
  • 211
  • 6
  • 13

5 Answers5

2

You can use DataGrid DataSource property.

Sample code demontrate idea:

class MyForm : Form
{
   public MyForm()
   {
      InitializeComponent();
      DataAccess  dataAccess = new DataAccess();
      m_dataGrid.DataSource = DataAccess.SampleQuery("MyTable");
   }
}

class DataAccess 
{
   public DataTable SampleQuery(string tablename)
   {
      DataTable dataTable;
      // 
      // Your Code
      // ..
      return dataTable;
   }
}

I think is Form Load event handler is good place to load data (not constructor ;) as i show above). You may olso implement some caching (load datatable each time if form is open is not efficient:))

Kamil Lach
  • 4,519
  • 2
  • 19
  • 20
  • the first part in form class? and the second part in sql class? – Reza Nov 19 '11 at 10:55
  • Yes, MyForm is your Form class and DataAccess is your data access layer. – Kamil Lach Nov 19 '11 at 10:56
  • thanks alot guys...btw, why is my SampleQuery is red underlined and it says "not all paths code is returns a value" ? – Reza Nov 19 '11 at 11:07
  • Probobly because you return is inside if statement => you should return outside if. – Kamil Lach Nov 19 '11 at 11:09
  • yeah, i already tried that but the return dataTable says something like, use of unassigned variable. but at the top I already declare it. – Reza Nov 19 '11 at 11:12
  • Data Access is class (or group of class) that comunicates directly with your data storage (database) and your ui comunicate with that class not directly with database. When for example you change database engine (you change code only inside DAL class) UI is invariant. – Kamil Lach Nov 19 '11 at 11:42
1

Make your SampleQuery method return the dataTable:

public DataTable SampleQuery(string tablename) {
    // Fill dataTable...
    return dataTable
}

Then call the method in your form and bind the dataTable to the dataGridView:

dataGridView.DataSource = SampleQuery("MyTable");
Otiel
  • 18,404
  • 16
  • 78
  • 126
1

In this scenario, you would changethe method to return DataTable instead of nothing (void), and end with

return dataTable;

Then catch it when you call it:

DataGridView.BindingSource = yourDal.SimpleQuery("Foo");

Personally I would suggest class-based objects rather than DataTable, but DataTable will work.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

Specify the return type of SampleQuery method of DataTable.

//Instance method
public class Test
{
 public DataTable SampleQuery(string tablename)
 {
    DataTable dataTable=null;
    ....
    return dataTable;
 }
//or design a static method
 public static DataTable StaticSampleQuery(string tablename)
 {
    DataTable dataTable=null;
    ....
    return dataTable;
 }
}

In winform, use static or instance method.

//Instance method
Test obj=new Test();
dataGridView1.Datasource=obj.SampleQuery("tableName");

//Static method
dataGridView1.Datasource=Test.StaticSampleQuery("tableName");
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
1

return Datatable from sqlclass and assign that datatable as datasource of grid

     public DataTable SampleQuery(string tablename)
            {
                DataTable dataTable;

                string query = "SELECT * FROM " + tablename;

                //Open connection
                if (this.OpenConnection() == true)
                {
                    adapter = new MySqlDataAdapter(query,connection);

                    dataTable =  new DataTable();
                    adapter.Fill(dataTable);

                    //close Connection
                    this.CloseConnection();
                    return dataTable;
                }
              return dataTable;
            }

Assign datatable as datasource

dataGridView.DataSource = SampleQuery("tablename");
Nighil
  • 4,099
  • 7
  • 30
  • 56
  • should the datagridview call the samplequery with ("tablename")? since tablename is only the name of the table which the content already in dataTable – Reza Nov 19 '11 at 16:31