C# 通过从组合框中选择任何表来填充datagridview
我使用以下代码在combobox中显示SQL db表的名称。现在,当我从组合框中单击这些表名时,我想让DataGridView填充该表的内容 我的数据库中有三个表C# 通过从组合框中选择任何表来填充datagridview,c#,sql-server,C#,Sql Server,我使用以下代码在combobox中显示SQL db表的名称。现在,当我从组合框中单击这些表名时,我想让DataGridView填充该表的内容 我的数据库中有三个表 private void Form1_Load(object sender, EventArgs e) { String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true"; Sql
private void Form1_Load(object sender, EventArgs e)
{
String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";
SqlConnection con = new SqlConnection(strConnection);
try
{
con.Open();
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = con;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "Select table_name from information_schema.tables";
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
DataTable dtRecord = new DataTable();
sqlDataAdap.Fill(dtRecord);
comboBox1.DataSource = dtRecord;
comboBox1.DisplayMember = "TABLE_NAME";
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void PopulateGridView(string tablename)
{
String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";
SqlConnection con = new SqlConnection(strConnection);
try
{
con.Open();
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = con;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "Select * from " + tablename;
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
DataTable dtRecord = new DataTable();
sqlDataAdap.Fill(dtRecord);
dataGridView1.DataSource = dtRecord;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedValue != null)
{
PopulateGridView(comboBox1.SelectedValue.ToString());
}
}
}
}
这是我的桌子
rahul sharma 12-1-2011
suresh chand 23-4-2012
prachi shukla 13-2-2011
siddharth malhotra 25-9-2012
saina chopra 12-8-2011
amit mehta 20-3-2012
订阅组合框的SelectedIndexChanged事件,并从组合框中检索所选表名。从那里,运行查询从表中检索记录,并将结果绑定到datagridview 样本:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";
SqlConnection con = new SqlConnection(strConnection);
con.Open();
string tableName = comboBox1.SelectedText;
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = con;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "Select * from " + tableName;
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
DataTable dtRecord = new DataTable();
sqlDataAdap.Fill(dtRecord);
dgv.DataSource = dtRecord;
con.Close();
}
您将在combobox上选择一个事件IndexChange并将表名传递给您的函数
private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
RebindGridView(ComboBox1.SelectedValue);
}
然后在您的函数中:
private void RebindGridView(string tablename)
{
String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";
SqlConnection con = new SqlConnection(strConnection);
try
{
con.Open();
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = con;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "Select * from " + tablename;
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
DataTable dtRecord = new DataTable();
sqlDataAdap.Fill(dtRecord);
gridview1.DataSource = dtRecords;
gridview1.DataBind();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
那么问题是什么呢?问题是wat将是用我从组合框中选择的表格内容填充我的dgv的代码谢谢..但是请你写代码..我是c的新手..请看我的编辑。此外,您可能希望通过创建用于从数据库检索记录的不同类来分隔数据层。它在sqlDataAdap.FilldtRecord;上显示了一个错误;。。。。“from”附近的语法不正确。请确保选择的组合框项不是空的。它可能会抛出该语法,因为组合框中的选定项为空。会出现什么错误?还可以看到我的编辑忘记了from和tablename之间的空格…现在它是正确的。我需要在哪里定义数据绑定?数据绑定是gridview的方法,您不需要定义它。分配数据源后,您需要绑定它。。。请参见此处抱歉,但我仍然不明白..我不擅长编程..请帮助抱歉,正确的错误描述是-'System.Windows.Forms.DataGridView'不包含'DataBind'的定义,并且没有扩展方法'DataBind'接受类型为'System.Windows.Forms.DataGridView'的第一个参数。是否缺少使用指令还是程序集引用?