绑定数据问题 [英] Binding data problem
本文介绍了绑定数据问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是c#.net的初学者。我在将数据库(mysql)绑定到datagridview时遇到问题。这是我的代码
创建我没有任何问题的数据库的表格。
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.
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace DynamicSimulator_v2
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnNew_Click(object sender, EventArgs e)
{
SchemaName schemaForm = new SchemaName();
frmMetCon form2 = new frmMetCon();
if (schemaForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string connStr = "datasource=localhost;port=3306;username=root;password=root;";
string database = schemaForm.getData();
MySqlConnection conn = new MySqlConnection(connStr);
try
{
MySqlCommand command = conn.CreateCommand();
conn.Open();
command.CommandText = "DROP DATABASE IF EXISTS " + database;
command.ExecuteNonQuery();
command.CommandText = "CREATE DATABASE " + database;
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE " +database+
".Metabolites("+
"MetaboliteID VARCHAR(10) NOT NULL,"+
"Metabolite_Name VARCHAR(45) NULL," +
"ReactionTime INT NULL, " +
"PRIMARY KEY (MetaboliteID));";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO " + database +
".Metabolites " +
"(MetaboliteID)" +
"VALUES " +
"('met1');";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO " + database +
".Metabolites " +
"(MetaboliteID)" +
"VALUES " +
"('met2');";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO " + database +
".Metabolites " +
"(MetaboliteID)" +
"VALUES " +
"('met3');";
command.ExecuteNonQuery();
this.Hide();
form2.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conn.Close();
}
}
}
}
表格我我遇到问题
Form that I'm having a problem
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace DynamicSimulator_v2
{
public partial class frmMetCon : Form
{
public frmMetCon()
{
InitializeComponent();
}
private void cmbMet_SelectedIndexChanged(object sender, EventArgs e)
{
string connStr = "datasource=localhost;port=3306;username=root;password=root;";
MySqlConnection conn = new MySqlConnection(connStr);
bool hasSelection = (cmbMet.SelectedIndex >= 0);
SchemaName schemaForm = new SchemaName();
string database = schemaForm.getData();
if (hasSelection == true)
{
grpMetName.Visible = true;
try
{
string command;
MySqlDataAdapter sqlData = new MySqlDataAdapter();
DataTable dtable = new DataTable();
if (cmbMet.SelectedIndex == 2)
{
conn.Open();
command = "SELECT Metabolite_Name" +
"FROM " + database +
".Metabolites " +
"WHERE MetaboliteID IN ('met1', 'met2')";
sqlData = new MySqlDataAdapter(command, conn);
sqlData.Fill(dtable);
dbMetName.DataSource = dtable;
}
else if (cmbMet.SelectedIndex == 3)
{
conn.Open();
command = "SELECT Metabolite_Name" +
"FROM " + database +
".Metabolites " +
"WHERE MetaboliteID IN ('met1', 'met2', 'met3')";
sqlData = new MySqlDataAdapter(command, conn);
sqlData.Fill(dtable);
dbMetName.DataSource = dtable;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conn.Close();
}
}
}
}
表单用户输入并传递数据库名称
Form that user input and passing the database name
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DynamicSimulator_v2
{
public partial class SchemaName : Form
{
private string data;
public SchemaName()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Hide();
}
private void btnOK_Click(object sender, EventArgs e)
{
data=txtDB.Text;
this.Hide();
}
public string getData()
{
return data;
}
}
}
datagridview是dbMetName。 datagridview没有显示任何内容 - 只是一个普通的灰色面板
the datagridview is dbMetName. The datagridview does not shows anything - just a plain grey panel
推荐答案
代码对我来说很好。
您是否在线上设置了断点
The code looks good to me.
Did you put a break point on line
Quote:
dbMetName.DataSource = dtable;
dbMetName.DataSource = dtable;
并检查数据表中是否有任何记录?
and check whether you have any records in your datatable ?
请参阅此文章以了解如何设置断点。
http://msdn.microsoft.com/ en-us / library / ktf38f66(v = vs.90).aspx [ ^ ]
在VS中一个简单的方法就是点击左边吧,当您尝试上述选项
Refer this article to know how to put a breakpoint.
http://msdn.microsoft.com/en-us/library/ktf38f66(v=vs.90).aspx[^]
An easy way to do in VS is just click on the left bar, you will come to know how to do when you try the above option
这篇关于绑定数据问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文