[英]How to bind data in dataGridView from sql database using comboBox?
I am facing unique problem with my code. 我的代码面临独特的问题。 This is the code for showing tables in
comboBox1
its working fine. 这是用于在
comboBox1
显示表格的代码。
ComboBox is filled with school names and when a user select a school in that ComboBox, The students in that school will be shown in DataGridView Please help. ComboBox充满学校名称,当用户在该ComboBox中选择学校时,该学校的学生将显示在DataGridView中。请帮助。
Functionality of this application:
该应用程序的功能:
- Creating a table in sql database(schoolName).
在sql数据库(schoolName)中创建表。
- Selecting table(select School).
选择表格(选择学校)。
- Adding values in the school(student information).
在学校增加价值(学生信息)。
error:
错误:
- the datagrid in the bottom is not retrieving properly, its showing something else.
底部的数据网格未正确检索,显示了其他内容。
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\Login.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM INFORMATION_SCHEMA.TABLES";
SqlDataAdapter dbAdapter = new SqlDataAdapter(cmd);
DataTable dtRecords = new DataTable();
dbAdapter.Fill(dtRecords);
dataGridView1.DataSource = dtRecords; //dataGrid
comboBox1.DataSource = dtRecords;
comboBox1.DisplayMember = "TABLE_NAME";
con.Close();
First of all check this: http://msdn.microsoft.com/en-us/library/ms186224.aspx 首先检查一下: http : //msdn.microsoft.com/en-us/library/ms186224.aspx
The SQL query SELECT * FROM INFORMATION_SCHEMA.TABLES
returns information about the tables that exist in database not the rows of a specefic table SQL查询
SELECT * FROM INFORMATION_SCHEMA.TABLES
返回有关数据库中存在的表的信息,而不是特定表的行的信息
If you want to the information of that table you can rich that by writing a new query in SelectedIndexChanged
event of your ComboBox, something like that: 如果要获取该表的信息,可以通过在ComboBox的
SelectedIndexChanged
事件中编写新查询来丰富该信息,例如:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\Login.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM " + comboBox1.Text;
SqlDataAdapter dbAdapter = new SqlDataAdapter(cmd);
DataTable dtRecords = new DataTable();
dbAdapter.Fill(dtRecords);
dataGridView1.DataSource = dtRecords; //dataGrid
con.Close();
}
This code can fix this problem, but actually there is a bigger problem and It's about your data structure. 这段代码可以解决此问题,但是实际上存在一个更大的问题,它与您的数据结构有关。
This kind of code tells me there is a table for every school and that's not a normalized database. 这种代码告诉我每个学校都有一张桌子,而不是规范化的数据库。 Lets clear it by an example: Imagine that your manager want to move some students from their school to another school, in this data structure you have to move a record from a table to another table, first you have to find that record that point to our student and then save it in memory and then create a new record in destination table.
让我们通过一个例子来清除它:假设您的经理想要将一些学生从他们的学校转移到另一所学校,在这种数据结构中,您必须将一条记录从一个表移到另一个表,首先您必须找到指向该点的记录。我们的学生,然后将其保存在内存中,然后在目标表中创建新记录。 In this example:
在此示例中:
In other hand you can simply create table and named it schools
that have two main columns: ID
, Name
and then create a table and named it students
that have three main columns: ID
, SchoolID
, Name
every school have a unique id and every student have its own id related to its own school by SchoolID
column. 另一方面,您可以简单地创建表并将其命名为具有两个主要列的
schools
: ID
, Name
,然后创建一个表并将其命名为具有三个主要列的students
: ID
, SchoolID
, Name
每个学校都有唯一的ID以及每个学生通过SchoolID
列具有与自己学校相关的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.