By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,902 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,902 IT Pros & Developers. It's quick & easy.

Data Binding problem

P: 2
Im a beginner in c#.net. I'm having problem in binding the database (mysql) to datagridview. Here is my code

Form that creating the database which I do not have any problem.
Expand|Select|Wrap|Line Numbers
  1. using System.Data;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6.  
  7. using MySql.Data;
  8. using MySql.Data.MySqlClient;
  9.  
  10. namespace DynamicSimulator_v2
  11. {
  12.     public partial class frmMain : Form
  13.     {
  14.         public frmMain()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void btnExit_Click(object sender, EventArgs e)
  20.         {
  21.             Application.Exit();
  22.         }
  23.  
  24.         private void btnNew_Click(object sender, EventArgs e)
  25.         {
  26.             SchemaName schemaForm = new SchemaName();
  27.             frmMetCon form2 = new frmMetCon();
  28.  
  29.             if (schemaForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  30.             {
  31.               string connStr = "datasource=localhost;port=3306;username=root;password=root;";
  32.               string database = schemaForm.getData();
  33.               MySqlConnection conn = new MySqlConnection(connStr);
  34.                 try
  35.                  {
  36.                    MySqlCommand command = conn.CreateCommand();
  37.                    conn.Open();
  38.                    command.CommandText = "DROP DATABASE IF EXISTS " + database;
  39.                    command.ExecuteNonQuery();
  40.                    command.CommandText = "CREATE DATABASE " + database;
  41.                    command.ExecuteNonQuery();
  42.                    command.CommandText = "CREATE TABLE " +database+
  43.                                          ".Metabolites("+
  44.                                          "MetaboliteID VARCHAR(10) NOT NULL,"+
  45.                                          "Metabolite_Name VARCHAR(45) NULL," +
  46.                                          "ReactionTime INT NULL, " + 
  47.                                          "PRIMARY KEY (MetaboliteID));";
  48.                    command.ExecuteNonQuery();
  49.                    command.CommandText = "INSERT INTO " + database +
  50.                                          ".Metabolites " +
  51.                                          "(MetaboliteID)" +
  52.                                          "VALUES " +
  53.                                          "('met1');";
  54.                    command.ExecuteNonQuery();
  55.  
  56.                    command.CommandText = "INSERT INTO " + database +
  57.                                          ".Metabolites " +
  58.                                          "(MetaboliteID)" +
  59.                                          "VALUES " +
  60.                                          "('met2');";
  61.                    command.ExecuteNonQuery();
  62.  
  63.                    command.CommandText = "INSERT INTO " + database +
  64.                                          ".Metabolites " +
  65.                                          "(MetaboliteID)" +
  66.                                          "VALUES " +
  67.                                          "('met3');";
  68.                    command.ExecuteNonQuery();
  69.                    this.Hide();
  70.                    form2.Show();   
  71.                  }
  72.                  catch (Exception ex)
  73.                  {
  74.                    MessageBox.Show(ex.Message); 
  75.                  }
  76.                  conn.Close();
  77.  
  78.              }
  79.         }
  80.     }
  81. }
Form that I'm having a problem

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. using MySql.Data;
  11. using MySql.Data.MySqlClient;
  12.  
  13. namespace DynamicSimulator_v2
  14. {
  15.     public partial class frmMetCon : Form
  16.     {
  17.         public frmMetCon()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void cmbMet_SelectedIndexChanged(object sender, EventArgs e)
  23.         {
  24.             string connStr = "datasource=localhost;port=3306;username=root;password=root;";
  25.             MySqlConnection conn = new MySqlConnection(connStr);
  26.             bool hasSelection = (cmbMet.SelectedIndex >= 0);
  27.             SchemaName schemaForm = new SchemaName();
  28.             string database = schemaForm.getData();
  29.             if (hasSelection == true)
  30.             {
  31.              grpMetName.Visible = true;
  32.                 try
  33.                 {
  34.                   string command;
  35.                   MySqlDataAdapter sqlData = new MySqlDataAdapter();
  36.                   DataTable dtable = new DataTable();
  37.                   if (cmbMet.SelectedIndex == 2)
  38.                    {
  39.                     conn.Open();
  40.                     command = "SELECT Metabolite_Name" +
  41.                               "FROM " + database +
  42.                               ".Metabolites " +
  43.                               "WHERE MetaboliteID IN ('met1', 'met2')";
  44.                     sqlData = new MySqlDataAdapter(command, conn);
  45.                     sqlData.Fill(dtable);
  46.                     dbMetName.DataSource = dtable;
  47.  
  48.                    }
  49.                   else if (cmbMet.SelectedIndex == 3)
  50.                    {
  51.                     conn.Open();
  52.                     command = "SELECT Metabolite_Name" +
  53.                               "FROM " + database +
  54.                               ".Metabolites " +
  55.                               "WHERE MetaboliteID IN ('met1', 'met2', 'met3')";
  56.                     sqlData = new MySqlDataAdapter(command, conn);
  57.                     sqlData.Fill(dtable);
  58.                     dbMetName.DataSource = dtable;
  59.  
  60.                    }
  61.  
  62.                 }
  63.                 catch (Exception ex)
  64.                 {
  65.                     MessageBox.Show(ex.Message);
  66.                 }
  67.                 conn.Close();
  68.             }
  69.        }
  70.   }
  71. }
Form that user input and passing the database name

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace DynamicSimulator_v2
  11. {
  12.     public partial class SchemaName : Form
  13.     {
  14.         private string data;
  15.  
  16.         public SchemaName()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void btnCancel_Click(object sender, EventArgs e)
  22.         {
  23.             this.Hide();
  24.         }
  25.  
  26.         private void btnOK_Click(object sender, EventArgs e)
  27.         {
  28.             data=txtDB.Text;
  29.             this.Hide();
  30.         }
  31.  
  32.         public string getData()
  33.         {
  34.             return data;
  35.         }
  36.  
  37.     }
  38. }
the datagridview is dbMetName. The datagridview does not shows anything - just a plain grey panel
Oct 20 '13 #1
Share this Question
Share on Google+
1 Reply


pod
100+
P: 298
pod
Try adding the databind() call after the datasource is set
Expand|Select|Wrap|Line Numbers
  1. dbMetName.DataSource = dtable;
  2. dbMetName.DataBind();
Oct 21 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.