从combox中选择值后如何从Database绑定DataGridView [英] How to bind DataGridView from Database after selecting value from combox
问题描述
我在C#中制作一个Windows窗体。我有一个DataGridView和一个组合框,我想要做的就是如果我在组合框中选择一个表,它应该在datagridview中显示结果。
我做过这样的事情:但是我得到这个错误:'System.Data.DataRowView'不包含定义
I am making a Windows Form in C#.I have a DataGridView and a combobox, all I want to do is if i select a table in the combobox, it should display me the results in the datagridview.
I have done something like this: BUT i' m getting this error : 'System.Data.DataRowView' does not contain a definition
private void cbTbl_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbTbl.SelectedValue != null)
{
// Sql Server Connection and statement if a value is selected
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection con = new SqlConnection(CS);
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = con;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = ("SELECT * FROM " + cbTbl.SelectedValue); // Dynamically
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
try
{
con.Open();
dt = new DataTable();
sqlDataAdap.Fill(dt);
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = dt;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我需要将SelectedItem强制转换为DataRowView才能获得来自这个DataRowView的价值但我不知道在哪里以及如何。
我用这种方法填充组合框:
I need to cast the SelectedItem into DataRowView in order to get a value from this DataRowView But i don't know where and how.
I'm filling the combobox with this method:
private void FillCombobox1()
{
// Sql Connection and statement
string C = (@"Data Source=");
SqlConnection con = new SqlConnection(C);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES");
try
{
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
//Fill combobox with data in DT
comboBox1.DataSource = dt;
//Display dbo Tables in cbAlt
comboBox1.DisplayMember = "TABLE_NAME";
comboBox1.ValueMember = "TABLE_NAME";
// Empty bzw. clear the combobox
comboBox1.SelectedIndex = -1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
推荐答案
嗯,这是一个常见的错误。填充组合框时,在绑定数据之前设置DisplayMember和ValueMember。像这样:
Well,that's a common error. When you fill the combobox, set the DisplayMember and ValueMember before binding the data. Like this:
//Display dbo Tables in cbAlt
comboBox1.DisplayMember = "TABLE_NAME";
comboBox1.ValueMember = "TABLE_NAME";
//Fill combobox with data in DT
comboBox1.DataSource = dt;
您遇到的问题是,当您使用comboBox1.DataSource = dt绑定数据时,会触发comboBox1.SelectedIndexChanged。但由于仍没有设置valueMember,传递给事件的是DataRowView,导致错误。
编辑
如果您想在将数据绑定到组合时阻止加载网格,请执行以下操作:
The problem you have is because when you bind the data with comboBox1.DataSource = dt, the comboBox1.SelectedIndexChanged is fired. But as the valueMember is still not set, what is passed to the event is a DataRowView,causing the error.
Edit
If you want to prevent to load the grid when you bind the data to the combo,do something like this:
bool firstLoad=true;
.
.
.
private void cbTbl_SelectedIndexChanged(object sender, EventArgs e)
{
if (!firstLoad)
{
if (cbTbl.SelectedValue != null)
{
// Sql Server Connection and statement if a value is selected
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection con = new SqlConnection(CS);
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = con;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = ("SELECT * FROM " + cbTbl.SelectedValue); // Dynamically
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
try
{
con.Open();
dt = new DataTable();
sqlDataAdap.Fill(dt);
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = dt;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
else
{
firstLoad=false;
}
}
也就是说,使用布尔变量来防止在组合框数据时加载数据网格视图绑定。
That is,use a boolean variable to prevent loading the datagrid view when the combobox data is binded.
这篇关于从combox中选择值后如何从Database绑定DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!