Using the DataGridView
Written by: maffelu , 2009-04-16 18:01:21
string connectionString = "Data source=" + txtHost.Text + ";"
+ "Initial Catalog=" + txtDatabase.Text + ";"
+ "Username=" + txtUsername.Text + ";"
+ "Password=" + txtPassword.Text + ";";
string ourCommandText = "SELECT * FROM " + txtTable.Text + ";";
MySqlDataAdapter myDataAdapter;
try {
myDataAdapter = new MySqlDataAdapter(ourCommandText, connectionString);
} catch (MySqlException ex) {
txtOutput.AppendText("Error:\r\n" + ex);
return;
}
MySqlCommandBuilder myCommandBuilder = new MySqlCommandBuilder(myDataAdapter);
DataTable myTable = new DataTable();
try {
myDataAdapter.Fill(myTable);
} catch (MySqlException ex) {
txtOutput.AppendText("Error:\r\n" + ex);
return;
}
dbBindingSource.DataSource = myTable;
myDatagridview.DataSource = dbBindingSource;
The Full Code
using System;
using System.Windows.Forms;
using System.Data;
using MySql.Data.MySqlClient;
namespace UsingDataGridView
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
void BtnConnectClick(object sender, EventArgs e)
{
//Create a connectionstring to use with the dataadapter
string connectionString = "Data source=" + txtHost.Text + ";"
+ "Initial Catalog=" + txtDatabase.Text + ";"
+ "Username=" + txtUsername.Text + ";"
+ "Password=" + txtPassword.Text + ";";
//Create a commandstring, select * from table
string ourCommandText = "SELECT * FROM " + txtTable.Text + ";";
//Create a dataadapter object using the 4th overload
txtOutput.AppendText("Creating DataAdapter...\r\n");
MySqlDataAdapter myDataAdapter;
try {
myDataAdapter = new MySqlDataAdapter(ourCommandText, connectionString);
} catch (MySqlException ex) {
txtOutput.AppendText("Error:\r\n" + ex);
return;
}
txtOutput.AppendText("DataAdapter successfully created!\r\n");
//Since we're doing this the quick way...
txtOutput.AppendText("Creating CommandBuilder...\r\n");
MySqlCommandBuilder myCommandBuilder = new MySqlCommandBuilder(myDataAdapter);
//We're gonna use a datatable since it's a winform
txtOutput.AppendText("Creating DataTable...\r\n");
DataTable myTable = new DataTable();
//Set the culture info to culture-independent
txtOutput.AppendText("Setting CultureInfo..\r\n");
myTable.Locale = System.Globalization.CultureInfo.InvariantCulture;
//Fill the datatable with what we read from the database
txtOutput.AppendText("Filling DataTable...\r\n");
try {
myDataAdapter.Fill(myTable);
} catch (MySqlException ex) {
txtOutput.AppendText("Error:\r\n" + ex);
return;
}
txtOutput.AppendText("DataTable successfully filled!\r\n");
//Set the bindingsource.datasource to the table with got from the database
txtOutput.AppendText("Setting the bindingsource DataSource...\r\n");
dbBindingSource.DataSource = myTable;
//Enter the data into the datagridview
txtOutput.AppendText("Setting the DataGridView DataSource...\r\n");
myDatagridview.DataSource = dbBindingSource;
}
}
}
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.