domlao
domlao

Reputation: 16029

Data Grid Control In C# Using MySQL Database

I'm developing a basic app in Windows OS using VS C# Express and MySQL as my database. Now, the purpose of my app is to just list all the profiles in a data grid in a form. Now my question is what data grid control in C# .net that I can use to list my profiles in my mysql database? And how to populate that grid control with the data from the database in the code?

I already have a working database. I can also query the database in the C# code.

Please advise.

Many thanks.

Upvotes: 0

Views: 2105

Answers (2)

Dmitry Romanov
Dmitry Romanov

Reputation: 14090

DataGridView is the Data Grid you want

There are several ways to fill it with data. The simplest way would be just to bind the data to DataGridView. See this article on it.

Binding is really easy. Lets say you fill your MySql results to a some enumerable of class Cat (this is a scenario of using some ORM). Binding is easy:

class Cat
{
   [DisplayName("Cats Name")]
   public string Name {get;set;}
   public string Likes {get; set;}
}

...
public void BindCatsToGrid(List<Cat> cats)
{
    bindingDataSource = new BindingDataSource();
    bindingDataSource.DataSource = cats;
    grid.DataSource = bindingDataSource;
}

But you can control rows and columns manually. Or, even, switch to so called 'virtual mode' for displaying the huge sets of data.

There is a good article about using DataGridView in different scenarios.

Upvotes: 1

SidAhmed
SidAhmed

Reputation: 2362

you can use a DataGridView to display you're data

string sql = "SELECT ProfilCode, ProfilName FROM Profils";
DataTable myTable = new DataTable();

using(MySqlConnection connection = new MySqlConnection(connectionString))
{
   using(MySqlDataAdapter adapter = new MySqlDataAdapter(sql, connection))
   {
      adapter.Fill(myTable);
   }
}

dataGridView.DataSource = myDataTable;

Good luck

Upvotes: 1

Related Questions