c sharp中的组合框和datagridview [英] combo box and datagridview in c sharp
本文介绍了c sharp中的组合框和datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个组合框和一个datagridview。我在我的数据库中加载了两个表格的组合框。我想选择从组合框生成的表名,并在gridview中显示表内容。
我卡住了我不知道如何填充gridview。有人可以帮忙吗?
I have a combo box and a datagridview. I load the combobox with 2 tables in my database. I want to select the table name which generate from the combobox and display the table content in the gridview.
I got stuck i don't know how to populate gridview. Can anybody help?
private void viewTableButton_Click(object sender, EventArgs e)
{
using (SqlConnection sqlConn = new SqlConnection("Data Source=;Initial Catalog=" + "Integrated Security=SSPI"))
{
SqlCommand sqlCmd = sqlConn.CreateCommand();
//using sys.tables to call all table in database
sqlCmd.CommandText = ("Select * From sys.tables");
sqlConn.Open();
SqlDataReader sqlDR = sqlCmd.ExecuteReader();
while (sqlDR.Read())
{
tablesCombobox.Items.Add((string)sqlDR[0]);
}
sqlDR.Dispose();
sqlCmd.Dispose();
for(int index = 0; index <= tablesCombobox.Selectedindex
{//What code i need to put in here to populate the gridview
//which selected from combobox
}
}
}
推荐答案
你需要采取datagidview说datagridview1和
适用以下
1)生成combobox_selectedindexchange事件并编写以下代码
You need to take a datagidview say datagridview1 and
apply the following
1)generate the combobox_selectedindexchange event and write the following code
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection sqlConn = new SqlConnection("Data Source=;Initial Catalog=" + "Integrated Security=SSPI");
SqlCommand sqlCmd = sqlConn.CreateCommand();
sqlCmd.CommandText = ("Select * From "+ comboBox1.SelectedText);
SqlDataAdapter da=new SqlDataAdapter(sqlCmd);
DataSet ds=new DataSet();
da.Fill(ds);
datagridview1.DataSource=ds.Tables[0];
}
private void viewTableButton_Click(object sender, EventArgs e)
{
using (SqlConnection sqlConn = new SqlConnection("Data Source=;Initial Catalog=" + "Integrated Security=SSPI"))
{
SqlCommand sqlCmd = sqlConn.CreateCommand();
//using sys.tables to call all table in database
sqlCmd.CommandText = ("Select * From sys.tables");
sqlConn.Open();
SqlDataReader sqlDR = sqlCmd.ExecuteReader();
while (sqlDR.Read())
{
tablesCombobox.Items.Add((string)sqlDR[0]);
}
sqlDR.Dispose();
sqlCmd.Dispose();
//Try This
if (comboBox1.SelectedIndex != -1)
{
sqlCmd.CommandText = ("Select * From "+ comboBox1.SelectedText);
SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
DataSet ds = new DataSet();
da.Fill(ds);
datagridview1.DataSource = ds.Tables[0];
}
}
}
试试这段代码------------
Try this code------------
private void DisplayTableData_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
Try
{
if(!string.isNullOrEmpty(cbTable.SelectedText))
{
con.ConnectionString="Database ConnectionString";
SqlCommand sqlCmd = new SqlCommand("Select * From " + cbTable.SelectedText, sqlCon);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand=cmd;
DataTable dt = new DataTable();
da.Fill(dt);
dgvTableData.DataSource = dt.DefaultView;
}
else
{
MessageBox.Show("No Table Selected...");
}
}
Catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这篇关于c sharp中的组合框和datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文